process_delayed_jobs.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. result = nil
  17. realtime = Benchmark.realtime do
  18. Rails.logger.debug { "*** worker thread, #{::Delayed::Job.count} in queue" }
  19. result = ::Delayed::Worker.new.work_off
  20. end
  21. process_results(result, realtime)
  22. end
  23. end
  24. private
  25. def process_results(result, realtime)
  26. count = result.sum
  27. if count.zero?
  28. sleep SLEEP_IF_EMPTY
  29. Rails.logger.debug { '*** worker thread loop' }
  30. else
  31. Rails.logger.debug { format("*** #{count} jobs processed at %<jps>.4f j/s, %<failed>d failed ...\n", jps: count / realtime, failed: result.last) }
  32. end
  33. end
  34. end
  35. end
  36. end