workaround_active_job_logging.rb 1.1 KB

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