has_custom_logging.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ApplicationJob
  3. module HasCustomLogging
  4. extend ActiveSupport::Concern
  5. # ActiveJob default logging is to verbose in default setups.
  6. # Therefore we overwrite the default `ActiveJob::Logging::LogSubscriber` with a custom version.
  7. # This is currently done in an initializer because Rails 5.2 does not support detaching subscribers.
  8. # The custom version comes with two changes:
  9. # - Don't log info level lines
  10. # - Log (info) that an ActiveJob was not enqueued in case there is already one queued with the same ActiveJobLock
  11. class LogSubscriber < ActiveJob::LogSubscriber
  12. # ATTENTION: Uncomment this line to enable info logging again
  13. def info(...); end
  14. def enqueue(event)
  15. super if job_enqueued?(event)
  16. end
  17. def enqueue_at(event)
  18. super if job_enqueued?(event)
  19. end
  20. private
  21. def job_enqueued?(event)
  22. job = event.payload[:job]
  23. # having a provider_job_id means that the job was enqueued
  24. return true if job.provider_job_id.present?
  25. # we're only interested to log not enqueued lock-jobs for now
  26. return false if !job.is_a?(HasActiveJobLock)
  27. info do
  28. "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}'."
  29. end
  30. # don't log regular enqueue log lines
  31. false
  32. end
  33. end
  34. end
  35. end
  36. ApplicationJob::HasCustomLogging::LogSubscriber.attach_to :active_job