From 95b09dd77702b49ea08d99f30ee5ac94053d7079 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Thu, 29 Aug 2024 12:23:27 +0300 Subject: [PATCH] DEV: Log live slots of Sidekiq jobs (#28600) Introduce a new log line for Sidekiq jobs that consume more than `DISCOURSE_LIVE_SLOTS_SIDEKIQ_LIMIT` live slots. This is useful to track down jobs that may leak memory. This is enabled only when Sidekiq's job instrumenter is enabled (set `DISCOURSE_LOG_SIDEKIQ` to `1`). --- app/jobs/base.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/jobs/base.rb b/app/jobs/base.rb index 419f1f58ad5..3d064048acb 100644 --- a/app/jobs/base.rb +++ b/app/jobs/base.rb @@ -136,12 +136,27 @@ module Jobs @data["@timestamp"] = Time.now @data["duration"] = current_duration if @data["status"] == "pending" self.class.raw_log("#{@data.to_json}\n") + + if live_slots_limit > 0 && @data["live_slots_start"].present? && + @data["live_slots_finish"].present? + live_slots = @data["live_slots_finish"] - @data["live_slots_start"] + + if live_slots >= live_slots_limit + Rails.logger.warn( + "Sidekiq Job '#{@data["job_name"]}' allocated #{live_slots} objects in the heap: #{@data.inspect}", + ) + end + end end def enabled? ENV["DISCOURSE_LOG_SIDEKIQ"] == "1" end + def live_slots_limit + @live_slots_limit ||= ENV["DISCOURSE_LIVE_SLOTS_SIDEKIQ_LIMIT"].to_i + end + def self.mutex @@mutex ||= Mutex.new end