diff --git a/app/workers/cron/cron_job.rb b/app/workers/cron/cron_job.rb index 3275bd4e82..9e3dce5284 100644 --- a/app/workers/cron/cron_job.rb +++ b/app/workers/cron/cron_job.rb @@ -46,10 +46,16 @@ module Cron end end + def schedule_registered_jobs! + registered_jobs.each do |job_class| + job_class.ensure_scheduled! + end + end + ## # Ensure the job is scheduled unless it is already def ensure_scheduled! - # Ensure scheduled only onced + # Ensure scheduled only once return if scheduled? Rails.logger.info { "Scheduling #{name} recurrent background job." } @@ -65,13 +71,15 @@ module Cron ## # Is there a job scheduled? def scheduled? - delayed_job.present? + delayed_job_query.exists? end def delayed_job - Delayed::Job - .where('handler LIKE ?', "%job_class: #{name}%") - .first + delayed_job_query.first + end + + def delayed_job_query + Delayed::Job.where('handler LIKE ?', "%job_class: #{name}%") end end end diff --git a/lib/tasks/cron.rake b/lib/tasks/cron.rake index 8ff41392cf..10d016e69a 100644 --- a/lib/tasks/cron.rake +++ b/lib/tasks/cron.rake @@ -34,6 +34,6 @@ namespace 'openproject:cron' do desc 'Ensure the cron-like background jobs are actively scheduled' task schedule: [:environment] do - ::Cron::CronJob.registered_jobs.each(&:ensure_scheduled!) + ::Cron::CronJob.schedule_registered_jobs! end end