From d07e9d1bb5523a011614c5a8fdc83f25dced0bc9 Mon Sep 17 00:00:00 2001
From: Nikola Milojevic <nmilojevic@gitlab.com>
Date: Wed, 21 Dec 2022 09:47:49 +0000
Subject: [PATCH] Fix watchdog stop logging

This handles a case where the reason field
was missing from logs.
---
 lib/gitlab/memory/watchdog.rb           | 18 ++++++++++++------
 spec/lib/gitlab/memory/watchdog_spec.rb |  3 ++-
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/lib/gitlab/memory/watchdog.rb b/lib/gitlab/memory/watchdog.rb
index 25af5bd781a0a..3682d6e4462c9 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 668ea36d420f0..8a4e6589301c4 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
-- 
GitLab