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`).
This commit is contained in:
Bianca Nenciu 2024-08-29 12:23:27 +03:00 committed by GitHub
parent c760b30190
commit 95b09dd777
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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