process_delayed_jobs.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class BackgroundServices
  3. class Service
  4. class ProcessDelayedJobs < Service
  5. SLEEP_IF_EMPTY = 4.seconds
  6. def self.max_workers
  7. 16
  8. end
  9. def self.pre_launch
  10. start_time = Time.zone.now
  11. CleanupAction.cleanup_delayed_jobs(start_time)
  12. ImportJob.cleanup_import_jobs(start_time)
  13. end
  14. def launch
  15. loop do
  16. break if BackgroundServices.shutdown_requested
  17. result = nil
  18. realtime = Benchmark.realtime do
  19. Rails.logger.debug { "*** worker thread, #{::Delayed::Job.count} in queue" }
  20. # ::Delayed::Worker#stop? is monkey patched by config/initializers/delayed_worker_stop.rb
  21. # to ensure an early exit even during work_off().
  22. result = ::Delayed::Worker.new.work_off
  23. end
  24. process_results(result, realtime)
  25. end
  26. end
  27. private
  28. def process_results(result, realtime)
  29. count = result.sum
  30. if count.zero?
  31. interruptible_sleep SLEEP_IF_EMPTY
  32. Rails.logger.debug { '*** worker thread loop' }
  33. else
  34. Rails.logger.debug { format("*** #{count} jobs processed at %<jps>.4f j/s, %<failed>d failed ...\n", jps: count / realtime, failed: result.last) }
  35. end
  36. end
  37. end
  38. end
  39. end