demarches-normaliennes/app/jobs/cron/cron_job.rb
2023-12-14 17:12:19 +01:00

41 lines
863 B
Ruby

class Cron::CronJob < ApplicationJob
queue_as :cron
class_attribute :schedule_expression
class << self
def schedulable?
true && ENV['CRON_JOBS_DISABLED'].blank?
end
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
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
end
end