demarches-normaliennes/app/jobs/cron/cron_job.rb

42 lines
863 B
Ruby
Raw Normal View History

class Cron::CronJob < ApplicationJob
2020-03-28 16:45:16 +01:00
queue_as :cron
class_attribute :schedule_expression
2020-03-28 16:45:16 +01:00
class << self
def schedulable?
true && ENV['CRON_JOBS_DISABLED'].blank?
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
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
def cron_expression
Fugit.do_parse(schedule_expression, multi: :fail).to_cron_s
end
2020-03-28 16:45:16 +01:00
end
end