From fb238ff7ac37358e67d2c67a61dc12cbc672e120 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 7 Nov 2023 07:26:29 +0100 Subject: [PATCH] =?UTF-8?q?tech(refactor):=20deplace=20l'echelonnement=20d?= =?UTF-8?q?es=20taches=20cron=20li=C3=A9s=20a=20l'expiration=20dans=20un?= =?UTF-8?q?=20module=20d=C3=A9di=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../expired_dossiers_brouillon_deletion_job.rb | 2 +- ...ed_dossiers_en_construction_deletion_job.rb | 2 +- .../expired_dossiers_termine_deletion_job.rb | 2 +- .../expired_prefilled_dossiers_deletion_job.rb | 2 +- app/jobs/cron/expired_users_deletion_job.rb | 2 +- app/services/expired.rb | 18 ++++++++++++++++++ spec/lib/tasks/jobs_spec.rb | 8 ++++++++ 7 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 app/services/expired.rb create mode 100644 spec/lib/tasks/jobs_spec.rb diff --git a/app/jobs/cron/expired_dossiers_brouillon_deletion_job.rb b/app/jobs/cron/expired_dossiers_brouillon_deletion_job.rb index 7c13ae623..cc8812352 100644 --- a/app/jobs/cron/expired_dossiers_brouillon_deletion_job.rb +++ b/app/jobs/cron/expired_dossiers_brouillon_deletion_job.rb @@ -1,5 +1,5 @@ class Cron::ExpiredDossiersBrouillonDeletionJob < Cron::CronJob - self.schedule_expression = "every day at 10 pm" + self.schedule_expression = Expired.schedule_at(self) def perform(*args) Expired::DossiersDeletionService.new.process_expired_dossiers_brouillon diff --git a/app/jobs/cron/expired_dossiers_en_construction_deletion_job.rb b/app/jobs/cron/expired_dossiers_en_construction_deletion_job.rb index 4de43fe6d..36ad0fb96 100644 --- a/app/jobs/cron/expired_dossiers_en_construction_deletion_job.rb +++ b/app/jobs/cron/expired_dossiers_en_construction_deletion_job.rb @@ -1,5 +1,5 @@ class Cron::ExpiredDossiersEnConstructionDeletionJob < Cron::CronJob - self.schedule_expression = "every day at 3 pm" + self.schedule_expression = Expired.schedule_at(self) def perform(*args) Expired::DossiersDeletionService.new.process_expired_dossiers_en_construction diff --git a/app/jobs/cron/expired_dossiers_termine_deletion_job.rb b/app/jobs/cron/expired_dossiers_termine_deletion_job.rb index b97ce35e2..3e69cb788 100644 --- a/app/jobs/cron/expired_dossiers_termine_deletion_job.rb +++ b/app/jobs/cron/expired_dossiers_termine_deletion_job.rb @@ -1,5 +1,5 @@ class Cron::ExpiredDossiersTermineDeletionJob < Cron::CronJob - self.schedule_expression = "every day at 7 am" + self.schedule_expression = Expired.schedule_at(self) def perform(*args) Expired::DossiersDeletionService.new.process_expired_dossiers_termine diff --git a/app/jobs/cron/expired_prefilled_dossiers_deletion_job.rb b/app/jobs/cron/expired_prefilled_dossiers_deletion_job.rb index 383e35adf..d825ba742 100644 --- a/app/jobs/cron/expired_prefilled_dossiers_deletion_job.rb +++ b/app/jobs/cron/expired_prefilled_dossiers_deletion_job.rb @@ -1,5 +1,5 @@ class Cron::ExpiredPrefilledDossiersDeletionJob < Cron::CronJob - self.schedule_expression = "every day at 3:00" + self.schedule_expression = Expired.schedule_at(self) def perform Dossier.prefilled.state_brouillon.where(user_id: nil, updated_at: ..5.days.ago).destroy_all diff --git a/app/jobs/cron/expired_users_deletion_job.rb b/app/jobs/cron/expired_users_deletion_job.rb index 395f8ccdb..351519cc2 100644 --- a/app/jobs/cron/expired_users_deletion_job.rb +++ b/app/jobs/cron/expired_users_deletion_job.rb @@ -1,5 +1,5 @@ class Cron::ExpiredUsersDeletionJob < Cron::CronJob - self.schedule_expression = "every day at 11 pm" + self.schedule_expression = Expired.schedule_at(self) def perform(*args) return if ENV['EXPIRE_USER_DELETION_JOB_LIMIT'].blank? diff --git a/app/services/expired.rb b/app/services/expired.rb new file mode 100644 index 000000000..dc9ab19ef --- /dev/null +++ b/app/services/expired.rb @@ -0,0 +1,18 @@ +module Expired + def self.schedule_at(caller) + case caller.name + when 'Cron::ExpiredPrefilledDossiersDeletionJob' + "every day at 3 am" + when 'Cron::ExpiredDossiersTermineDeletionJob' + "every day at 7 am" + when 'Cron::ExpiredDossiersBrouillonDeletionJob' + "every day at 10 pm" + when 'Cron::ExpiredUsersDeletionJob' + "every day at 11 pm" + when 'Cron::ExpiredDossiersEnConstructionDeletionJob' + "every day at 3 pm" + else + raise 'please, check the schedule to avoid too much email at the same time' + end + end +end \ No newline at end of file diff --git a/spec/lib/tasks/jobs_spec.rb b/spec/lib/tasks/jobs_spec.rb new file mode 100644 index 000000000..799b00002 --- /dev/null +++ b/spec/lib/tasks/jobs_spec.rb @@ -0,0 +1,8 @@ +describe 'jobs' do + describe 'schedule' do + subject { Rake::Task['jobs:schedule'].invoke } + it 'runs' do + expect { subject }.not_to raise_error + end + end +end