has_delayed_job_monitoring_compatibilty.rb 984 B

1234567891011121314151617181920212223
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class ApplicationJob
  3. module HasDelayedJobMonitoringCompatibilty
  4. extend ActiveSupport::Concern
  5. included do
  6. # We (currently) rely on Delayed::Job#attempts to check for stuck backends
  7. # e.g. in the MonitoringController.
  8. # This is a workaround to sync ActiveJob#executions to Delayed::Job#attempts
  9. # until we resolve this dependency.
  10. after_enqueue do |job|
  11. # skip update of `attempts` attribute if job wasn't queued because of ActiveJobLock
  12. # (another job with same lock key got queued before this job could be retried)
  13. next if job.provider_job_id.blank?
  14. # update the column right away without loading Delayed::Job record
  15. # see: https://stackoverflow.com/a/34264580
  16. Delayed::Job.where(id: job.provider_job_id).update_all(attempts: job.executions) # rubocop:disable Rails/SkipsModelValidations
  17. end
  18. end
  19. end
  20. end