2020-11-13 14:34:53 +01:00
|
|
|
class Cron::CronJob < ApplicationJob
|
2020-03-28 16:45:16 +01:00
|
|
|
queue_as :cron
|
2020-04-01 15:26:46 +02:00
|
|
|
class_attribute :schedule_expression
|
2020-03-28 16:45:16 +01:00
|
|
|
|
|
|
|
class << self
|
2022-07-21 15:28:28 +02:00
|
|
|
def schedulable?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-03-28 16:45:16 +01:00
|
|
|
def schedule
|
|
|
|
remove if cron_expression_changed?
|
|
|
|
set(cron: cron_expression).perform_later if !scheduled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove
|
|
|
|
delayed_job.destroy if scheduled?
|
|
|
|
end
|
|
|
|
|
2020-04-01 15:26:46 +02:00
|
|
|
def display_schedule
|
|
|
|
pp "#{name}: #{schedule_expression} cron(#{cron_expression})"
|
|
|
|
end
|
|
|
|
|
2020-03-28 16:45:16 +01:00
|
|
|
def scheduled?
|
|
|
|
delayed_job.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def cron_expression_changed?
|
|
|
|
scheduled? && delayed_job.cron != cron_expression
|
|
|
|
end
|
|
|
|
|
|
|
|
def delayed_job
|
|
|
|
Delayed::Job
|
|
|
|
.where('handler LIKE ?', "%job_class: #{name}%")
|
|
|
|
.first
|
|
|
|
end
|
2020-04-01 15:26:46 +02:00
|
|
|
|
|
|
|
def cron_expression
|
|
|
|
Fugit.do_parse(schedule_expression, multi: :fail).to_cron_s
|
|
|
|
end
|
2020-03-28 16:45:16 +01:00
|
|
|
end
|
|
|
|
end
|