diff options
Diffstat (limited to 'lib/gitlab/memory/reporter.rb')
-rw-r--r-- | lib/gitlab/memory/reporter.rb | 30 |
1 files changed, 21 insertions, 9 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 |