make cron_job work with both delayed_job and sidekiq

This commit is contained in:
simon lehericey 2024-04-04 16:38:23 +02:00
parent e06fb54655
commit 42af5a9149
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5

View file

@ -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