demarches-normaliennes/app/jobs/cron/cron_job.rb
mfo 08ec45443d
BREAKING(sidekiq.queues): rationalize queues. now have critical, default, low
Dear instances, make sure to update your sidekiq processes. Indead, we are adopting a new strategy for daemons that process our background
jobs.

Now their is only 3 queues on sidekiq (still, we keep archive/export on delayed job for now).

The big idea : allow any worker to deal with any queue (capacity mgmt) :
- critical: must be procesed now
- default: will be processed asap
- low: do it after everything else

FYI: we had 50% of our server capacity on reserved queues. Leading to
bottle neck processing (reserved queues/worker not processing other
queues). We HAD TO fix it. Sorry for the inconvenience

TBH, this is an idea of Colin Darie <colin@darie.eu>. I'm just pushing
it forward.

Co-Authored-By: Colin Darie <colin@darie.eu>
2024-09-24 21:29:11 +02:00

62 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class Cron::CronJob < ApplicationJob
queue_as :default
class_attribute :schedule_expression
class << self
def schedulable?
ENV['CRON_JOBS_DISABLED'].blank?
end
def schedule
remove if cron_expression_changed?
if !scheduled?
if queue_adapter_name == "sidekiq"
Sidekiq::Cron::Job.create(name: name, cron: cron_expression, class: name)
else
set(cron: cron_expression).perform_later
end
end
end
def remove
enqueued_cron_job.destroy if scheduled?
end
def display_schedule
pp "#{name}: #{schedule_expression} cron(#{cron_expression})"
end
def scheduled?
enqueued_cron_job.present?
end
def cron_expression_changed?
scheduled? && enqueued_cron_job.cron != cron_expression
end
def enqueued_cron_job
if queue_adapter_name == "sidekiq"
sidekiq_cron_job
else
delayed_job
end
end
def sidekiq_cron_job
Sidekiq::Cron::Job.find(name)
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