diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-18 22:00:14 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-01-18 22:00:14 +0300 |
commit | 05f0ebba3a2c8ddf39e436f412dc2ab5bf1353b2 (patch) | |
tree | 11d0f2a6ec31c7793c184106cedc2ded3d9a2cc5 /lib/gitlab/memory | |
parent | ec73467c23693d0db63a797d10194da9e72a74af (diff) |
Add latest changes from gitlab-org/gitlab@15-8-stable-eev15.8.0-rc42
Diffstat (limited to 'lib/gitlab/memory')
-rw-r--r-- | lib/gitlab/memory/reporter.rb | 30 | ||||
-rw-r--r-- | lib/gitlab/memory/watchdog.rb | 18 |
2 files changed, 33 insertions, 15 deletions
diff --git a/lib/gitlab/memory/reporter.rb b/lib/gitlab/memory/reporter.rb index 710c89c6216..5effafc9f5b 100644 --- a/lib/gitlab/memory/reporter.rb +++ b/lib/gitlab/memory/reporter.rb @@ -3,6 +3,8 @@ module Gitlab module Memory class Reporter + COMPRESS_CMD = %w[gzip --fast].freeze + attr_reader :reports_path def initialize(reports_path: nil, logger: Gitlab::AppLogger) @@ -67,29 +69,39 @@ module Gitlab report_file = file_name(report) tmp_file_path = File.join(tmp_dir, report_file) + write_heap_dump_file(report, tmp_file_path) + + File.join(@reports_path, report_file).tap do |report_file_path| + FileUtils.mv(tmp_file_path, report_file_path) + end + end + + def write_heap_dump_file(report, path) io_r, io_w = IO.pipe + err_r, err_w = IO.pipe pid = nil - File.open(tmp_file_path, 'wb') do |file| + status = nil + File.open(path, 'wb') do |file| extras = { in: io_r, out: file, - err: $stderr + err: err_w } - pid = Process.spawn('gzip', '--fast', **extras) + pid = Process.spawn(*COMPRESS_CMD, **extras) io_r.close + err_w.close report.run(io_w) io_w.close - Process.waitpid(pid) + _, status = Process.wait2(pid) end - File.join(@reports_path, report_file).tap do |report_file_path| - FileUtils.mv(tmp_file_path, report_file_path) - end + errors = err_r.read&.strip + err_r.close + raise StandardError, "exit #{status.exitstatus}: #{errors}" if !status&.success? && errors.present? ensure - [io_r, io_w].each(&:close) - + [io_r, io_w, err_r, err_w].each(&:close) # Make sure we don't leave any running processes behind. Gitlab::ProcessManagement.signal(pid, :KILL) if pid end diff --git a/lib/gitlab/memory/watchdog.rb b/lib/gitlab/memory/watchdog.rb index aac70a2f6aa..c94dbed1d46 100644 --- a/lib/gitlab/memory/watchdog.rb +++ b/lib/gitlab/memory/watchdog.rb @@ -68,12 +68,11 @@ module Gitlab monitor end - event_reporter.stopped(log_labels(memwd_reason: @reason).compact) + event_reporter.stopped(log_labels(memwd_reason: @stop_reason).compact) end - def stop(reason: nil) - @reason = reason - @alive = false + def stop + stop_working(reason: 'background task stopped') end private @@ -84,7 +83,7 @@ module Gitlab def monitor if monitors.empty? - stop(reason: 'monitors are not configured') + stop_working(reason: 'monitors are not configured') return end @@ -106,7 +105,7 @@ module Gitlab Gitlab::Memory::Reports::HeapDump.enqueue! - stop(reason: 'successfully handled') if handler.call + stop_working(reason: 'successfully handled') if handler.call end def handler @@ -123,6 +122,13 @@ module Gitlab memwd_sleep_time_s: sleep_time_seconds ) end + + def stop_working(reason:) + return unless @alive + + @stop_reason = reason + @alive = false + end end end end |