make cron_job work with both delayed_job and sidekiq
This commit is contained in:
parent
e06fb54655
commit
42af5a9149
1 changed files with 23 additions and 4 deletions
|
@ -9,11 +9,18 @@ class Cron::CronJob < ApplicationJob
|
||||||
|
|
||||||
def schedule
|
def schedule
|
||||||
remove if cron_expression_changed?
|
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
|
end
|
||||||
|
|
||||||
def remove
|
def remove
|
||||||
delayed_job.destroy if scheduled?
|
enqueued_cron_job.destroy if scheduled?
|
||||||
end
|
end
|
||||||
|
|
||||||
def display_schedule
|
def display_schedule
|
||||||
|
@ -21,11 +28,23 @@ class Cron::CronJob < ApplicationJob
|
||||||
end
|
end
|
||||||
|
|
||||||
def scheduled?
|
def scheduled?
|
||||||
delayed_job.present?
|
enqueued_cron_job.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def cron_expression_changed?
|
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
|
end
|
||||||
|
|
||||||
def delayed_job
|
def delayed_job
|
||||||
|
|
Loading…
Reference in a new issue