diff --git a/lib/gitlab/memory/watchdog.rb b/lib/gitlab/memory/watchdog.rb index 25af5bd781a0a62de6f2214e2643f53f16ceee1c..3682d6e4462c9093ad296ffa3003818b508696c8 100644 --- a/lib/gitlab/memory/watchdog.rb +++ b/lib/gitlab/memory/watchdog.rb @@ -68,12 +68,11 @@ def call monitor if Feature.enabled?(:gitlab_memory_watchdog, type: :ops) 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 @@ def stop(reason: nil) def monitor if monitors.empty? - stop(reason: 'monitors are not configured') + stop_working(reason: 'monitors are not configured') return end @@ -106,7 +105,7 @@ def strike_exceeded_callback(monitor_name, monitor_payload) 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 @@ def log_labels(extra = {}) memwd_sleep_time_s: sleep_time_seconds ) end + + def stop_working(reason:) + return unless @alive + + @stop_reason = reason + @alive = false + end end end end diff --git a/spec/lib/gitlab/memory/watchdog_spec.rb b/spec/lib/gitlab/memory/watchdog_spec.rb index 668ea36d420f0452e29f93541312ac09c1c1c1bc..8a4e6589301c4733c7c63b587c3820fec9e77d2f 100644 --- a/spec/lib/gitlab/memory/watchdog_spec.rb +++ b/spec/lib/gitlab/memory/watchdog_spec.rb @@ -98,7 +98,8 @@ def self.name expect(reporter).to receive(:stopped).once .with( memwd_handler_class: handler.class.name, - memwd_sleep_time_s: sleep_time_seconds + memwd_sleep_time_s: sleep_time_seconds, + memwd_reason: 'background task stopped' ) watchdog.call