Merge pull request #10268 from demarches-simplifiees/sidekiq_cron

Tech: déplace les cron job sur sidekiq
This commit is contained in:
LeSim 2024-04-08 08:32:06 +00:00 committed by GitHub
commit 12406643d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 32 additions and 6 deletions

View file

@ -91,6 +91,7 @@ gem 'sentry-ruby'
gem 'sentry-sidekiq'
gem 'sib-api-v3-sdk'
gem 'sidekiq'
gem 'sidekiq-cron'
gem 'skylight'
gem 'spreadsheet_architect'
gem 'strong_migrations' # lint database migrations

View file

@ -721,6 +721,10 @@ GEM
connection_pool (>= 2.3.0)
rack (>= 2.2.4)
redis-client (>= 0.19.0)
sidekiq-cron (1.12.0)
fugit (~> 1.8)
globalid (>= 1.0.1)
sidekiq (>= 6)
simple_xlsx_reader (1.0.4)
nokogiri
rubyzip
@ -973,6 +977,7 @@ DEPENDENCIES
shoulda-matchers
sib-api-v3-sdk
sidekiq
sidekiq-cron
simple_xlsx_reader
skylight
spreadsheet_architect

View file

@ -9,11 +9,18 @@ class Cron::CronJob < ApplicationJob
def schedule
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
def remove
delayed_job.destroy if scheduled?
enqueued_cron_job.destroy if scheduled?
end
def display_schedule
@ -21,11 +28,23 @@ class Cron::CronJob < ApplicationJob
end
def scheduled?
delayed_job.present?
enqueued_cron_job.present?
end
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
def delayed_job

View file

@ -1,6 +1,6 @@
sidekiq_enabled = ENV.has_key?('REDIS_SIDEKIQ_SENTINELS') || ENV.has_key?('REDIS_URL')
SIDEKIQ_ENABLED = ENV.has_key?('REDIS_SIDEKIQ_SENTINELS') || ENV.has_key?('REDIS_URL')
if Rails.env.production? && sidekiq_enabled
if Rails.env.production? && SIDEKIQ_ENABLED
ActiveSupport.on_load(:after_initialize) do
class ActiveStorage::PurgeJob < ActiveStorage::BaseJob
self.queue_adapter = :sidekiq

View file

@ -1,4 +1,5 @@
require 'sidekiq/web'
require 'sidekiq/cron/web'
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html