DEV: Remove unnecessary thread in Jobs::Base::JobInstrumenter (#30179)

In `Jobs::Base::JobInstrumenter.raw_log`, we were creating an instance
of `Queue` and then pushing messages to the queue before popping it off
the queue in a thread. However, this complexity is not necessary when
we can just write directly to the logger without much overhead. This is
how all logging is done in other parts of the app as well.
This commit is contained in:
Alan Guo Xiang Tan 2024-12-10 06:29:46 +08:00 committed by GitHub
parent b0a3017871
commit 1670ffe82d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -44,6 +44,7 @@ module Jobs
class JobInstrumenter class JobInstrumenter
def initialize(job_class:, opts:, db:, jid:) def initialize(job_class:, opts:, db:, jid:)
return unless enabled? return unless enabled?
self.class.mutex.synchronize do self.class.mutex.synchronize do
@data = {} @data = {}
@ -75,6 +76,7 @@ module Jobs
def stop(exception:) def stop(exception:)
return unless enabled? return unless enabled?
self.class.mutex.synchronize do self.class.mutex.synchronize do
profile = MethodProfiler.stop profile = MethodProfiler.stop
@ -102,30 +104,15 @@ module Jobs
end end
def self.raw_log(message) def self.raw_log(message)
@@logger ||=
begin begin
f = File.open "#{Rails.root}/log/sidekiq.log", "a" logger << message
f.sync = true rescue => e
Logger.new f Discourse.warn_exception(e, message: "Exception encountered while logging Sidekiq job")
end
@@log_queue ||= Queue.new
if !defined?(@@log_thread) || !@@log_thread.alive?
@@log_thread =
Thread.new do
loop do
@@logger << @@log_queue.pop
rescue Exception => e
Discourse.warn_exception(
e,
message: "Exception encountered while logging Sidekiq job",
)
end
end end
end end
@@log_queue.push(message) def self.logger
@@logger ||= Logger.new("#{Rails.root}/log/sidekiq.log")
end end
def current_duration def current_duration
@ -259,6 +246,7 @@ module Jobs
requeued = true requeued = true
return return
end end
parent_thread = Thread.current parent_thread = Thread.current
cluster_concurrency_redis_key = self.class.cluster_concurrency_redis_key cluster_concurrency_redis_key = self.class.cluster_concurrency_redis_key
@ -343,6 +331,7 @@ module Jobs
keepalive_thread.join keepalive_thread.join
self.class.clear_cluster_concurrency_lock! self.class.clear_cluster_concurrency_lock!
end end
ActiveRecord::Base.connection_handler.clear_active_connections! ActiveRecord::Base.connection_handler.clear_active_connections!
end end
end end