From 42af5a9149382fbe5655bb36d7fddd9bcb82823a Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 4 Apr 2024 16:38:23 +0200 Subject: [PATCH] make cron_job work with both delayed_job and sidekiq --- app/jobs/cron/cron_job.rb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/app/jobs/cron/cron_job.rb b/app/jobs/cron/cron_job.rb index 16d5c1a73..2938024e0 100644 --- a/app/jobs/cron/cron_job.rb +++ b/app/jobs/cron/cron_job.rb @@ -9,11 +9,18 @@ class Cron::CronJob < ApplicationJob def schedule remove if cron_expression_changed? - set(cron: cron_expression).perform_later if !scheduled? + + if !scheduled? + if SIDEKIQ_ENABLED + Sidekiq::Cron::Job.create(name: name, cron: cron_expression, class: name) + else + set(cron: cron_expression).perform_later + end + end end def remove - delayed_job.destroy if scheduled? + enqueued_cron_job.destroy if scheduled? end def display_schedule @@ -21,11 +28,23 @@ class Cron::CronJob < ApplicationJob end def scheduled? - delayed_job.present? + enqueued_cron_job.present? end def cron_expression_changed? - scheduled? && delayed_job.cron != cron_expression + scheduled? && enqueued_cron_job.cron != cron_expression + end + + def enqueued_cron_job + if SIDEKIQ_ENABLED + sidekiq_cron_job + else + delayed_job + end + end + + def sidekiq_cron_job + Sidekiq::Cron::Job.find(name) end def delayed_job