add schedulable? class method to cron jobs

This commit is contained in:
Christophe Robillard 2022-07-21 15:28:28 +02:00
parent bed6167010
commit 0e3e6eb62a
4 changed files with 30 additions and 0 deletions

View file

@ -3,6 +3,10 @@ class Cron::CronJob < ApplicationJob
class_attribute :schedule_expression
class << self
def schedulable?
true
end
def schedule
remove if cron_expression_changed?
set(cron: cron_expression).perform_later if !scheduled?

View file

@ -15,4 +15,8 @@ class Cron::Datagouv::ExportAndPublishDemarchesPubliquesJob < Cron::CronJob
FileUtils.rm(gzip_filepath)
end
end
def self.schedulable?
ENV.fetch('OPENDATA_ENABLED', nil) == 'enabled'
end
end

View file

@ -0,0 +1,7 @@
RSpec.describe Cron::Datagouv::ExportAndPublishDemarchesPubliquesJob, type: :job do
describe '#schedulable?' do
it 'is schedulable by default' do
expect(Cron::CronJob.schedulable?).to be_truthy
end
end
end

View file

@ -25,4 +25,19 @@ RSpec.describe Cron::Datagouv::ExportAndPublishDemarchesPubliquesJob, type: :job
expect(Dir.glob("*demarches.json.gz", base: 'tmp').empty?).to be_truthy
end
end
describe '#schedulable?' do
context "when ENV['OPENDATA_ENABLED'] == 'enabled'" do
it 'is schedulable' do
ENV['OPENDATA_ENABLED'] = 'enabled'
expect(Cron::Datagouv::ExportAndPublishDemarchesPubliquesJob.schedulable?).to be_truthy
end
end
context "when ENV['OPENDATA_ENABLED'] != 'enabled'" do
it 'is schedulable' do
ENV['OPENDATA_ENABLED'] = nil
expect(Cron::Datagouv::ExportAndPublishDemarchesPubliquesJob.schedulable?).to be_falsy
end
end
end
end