Maintenance: Reduce logging (disable info) of ActiveJob.

This commit is contained in:
Thorsten Eckel 2021-05-19 13:59:17 +00:00
parent c69b691699
commit bde3452f9b
4 changed files with 65 additions and 1 deletions

View file

@ -1,6 +1,7 @@
class ApplicationJob < ActiveJob::Base
include ApplicationJob::HasDelayedJobMonitoringCompatibilty
include ApplicationJob::HasQueuingPriority
include ApplicationJob::HasCustomLogging
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked

View file

@ -0,0 +1,46 @@
class ApplicationJob
module HasCustomLogging
extend ActiveSupport::Concern
# ActiveJob default logging is to verbose in default setups.
# Therefore we overwrite the default `ActiveJob::Logging::LogSubscriber` with a custom version.
# This is currently done in an initializer because Rails 5.2 does not support detaching subscribers.
# The custom version comes with two changes:
# - Don't log info level lines
# - Log (info) that an ActiveJob was not enqueued in case there is already one queued with the same ActiveJobLock
class LogSubscriber < ActiveJob::Logging::LogSubscriber
# ATTENTION: Uncomment this line to enable info logging again
def info(*); end
def enqueue(event)
super if job_enqueued?(event)
end
def enqueue_at(event)
super if job_enqueued?(event)
end
private
def job_enqueued?(event)
job = event.payload[:job]
# having a provider_job_id means that the job was enqueued
return true if job.provider_job_id.present?
# we're only interested to log not enqueued lock-jobs for now
return false if !job.is_a?(HasActiveJobLock)
info do
"Won't enqueue #{job.class.name} (Job ID: #{job.job_id}) to #{queue_name(event)}" + args_info(job) + " because of already existing Job with Lock Key '#{job.lock_key}'."
end
# don't log regular enqueue log lines
false
end
end
end
end
ApplicationJob::HasCustomLogging::LogSubscriber.attach_to :active_job

View file

@ -120,7 +120,6 @@ module HasActiveJobLock
end
def existing_active_job_lock!
logger.info "Won't enqueue #{self.class.name} (Job ID: #{job_id}) because of already existing job with lock key '#{lock_key}'."
throw :abort
end
end

View file

@ -0,0 +1,18 @@
# This is a workaround because `ActiveSupport::Subscriber` supports only `attach_to` but not `detach_from` in Rails 5.2.
# `detach_from` was added with Rails 6.0: https://github.com/rails/rails/commit/ca19b7f5d86aa590077766cbe8006f952b6d4296
# Once Rails 6.0 is used `ActiveJob::Logging::LogSubscriber.detach_from :active_job` needs to be added to `app/jobs/application_job.rb` instead.
ActiveSupport.on_load(:active_job) do
# gather all `ActiveJob::Logging::LogSubscriber` event subscribers
subscribers = ActiveSupport::Notifications.notifier.instance_variable_get(:@subscribers).select do |subscriber|
subscriber.instance_variable_get(:@delegate).instance_of?(ActiveJob::Logging::LogSubscriber)
end
# remove gathered event subscribers in a dedicated step to not work on iterating array
subscribers.each do |subscriber|
ActiveSupport::Notifications.notifier.unsubscribe(subscriber)
end
# remove whole `ActiveJob::Logging::LogSubscriber` subscriber reference
ActiveSupport::Subscriber.subscribers.delete_if { |subscriber| subscriber.instance_of?(ActiveJob::Logging::LogSubscriber) }
end