diff --git a/Gemfile b/Gemfile index b8b8e1e10..47bf1af2a 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 4189299fa..9894c3dc0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app/jobs/cron/cron_job.rb b/app/jobs/cron/cron_job.rb index 16d5c1a73..2938024e0 100644 --- a/app/jobs/cron/cron_job.rb +++ b/app/jobs/cron/cron_job.rb @@ -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 diff --git a/config/initializers/transition_to_sidekiq.rb b/config/initializers/transition_to_sidekiq.rb index 8c12e200e..90f69319c 100644 --- a/config/initializers/transition_to_sidekiq.rb +++ b/config/initializers/transition_to_sidekiq.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 6939fb529..bc65dfb59 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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