process_scheduled_jobs.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class BackgroundServices
  3. class Service
  4. class ProcessScheduledJobs < Service
  5. SLEEP_AFTER_JOB_START = 1.second
  6. SLEEP_AFTER_LOOP = 10.seconds
  7. attr_reader :jobs_started
  8. def initialize
  9. super
  10. @jobs_started = Concurrent::Hash.new
  11. end
  12. def launch
  13. loop do
  14. Rails.logger.info 'ProcessScheduledJobs running...'
  15. run_jobs
  16. sleep SLEEP_AFTER_LOOP
  17. end
  18. end
  19. private
  20. def run_jobs
  21. scope.each do |job|
  22. result = Manager.new(job, jobs_started).run
  23. sleep SLEEP_AFTER_JOB_START if result
  24. end
  25. end
  26. def scope
  27. # changes in sub threads will not update the rails
  28. # cache so we need to be sure that the scheduler get
  29. # updated last_run values, so they don't run all the time
  30. # https://github.com/zammad/zammad/issues/4167
  31. Scheduler.clear_query_caches_for_current_thread
  32. Scheduler.where(active: true).reorder(prio: :asc)
  33. end
  34. end
  35. end
  36. end