diff --git a/README.md b/README.md index e60b47848..10bd59d5d 100644 --- a/README.md +++ b/README.md @@ -65,22 +65,9 @@ L'application tourne à l'adresse `http://localhost:3000`. En local, un utilisateur de test est créé automatiquement, avec les identifiants `test@exemple.fr`/`this is a very complicated password !`. (voir [db/seeds.rb](https://github.com/betagouv/demarches-simplifiees.fr/blob/dev/db/seeds.rb)) -### Programmation des jobs +### Programmation des tâches récurrentes - AutoArchiveProcedureJob.set(cron: "* * * * *").perform_later - WeeklyOverviewJob.set(cron: "0 7 * * MON").perform_later - DeclarativeProceduresJob.set(cron: "* * * * *").perform_later - UpdateAdministrateurUsageStatisticsJob.set(cron: "0 10 * * *").perform_later - FindDubiousProceduresJob.set(cron: "0 0 * * *").perform_later - Administrateurs::ActivateBeforeExpirationJob.set(cron: "0 8 * * *").perform_later - WarnExpiringDossiersJob.set(cron: "0 0 1 * *").perform_later - InstructeurEmailNotificationJob.set(cron: "0 10 * * MON-FRI").perform_later - PurgeUnattachedBlobsJob.set(cron: "0 0 * * *").perform_later - OperationsSignatureJob.set(cron: "0 6 * * *").perform_later - ExpiredDossiersDeletionJob.set(cron: "0 7 * * *").perform_later - PurgeStaleExportsJob.set(cron: "*/5 * * * *").perform_later - NotifyDraftNotSubmittedJob.set(cron: "0 7 * * *").perform_later - DiscardedDossiersDeletionJob.set(cron: "0 7 * * *").perform_later + rails jobs:schedule ### Voir les emails envoyés en local diff --git a/app/jobs/cron_job.rb b/app/jobs/cron_job.rb new file mode 100644 index 000000000..13b735e1d --- /dev/null +++ b/app/jobs/cron_job.rb @@ -0,0 +1,29 @@ +class CronJob < ApplicationJob + queue_as :cron + class_attribute :cron_expression + + class << self + def schedule + remove if cron_expression_changed? + set(cron: cron_expression).perform_later if !scheduled? + end + + def remove + delayed_job.destroy if scheduled? + end + + def scheduled? + delayed_job.present? + end + + def cron_expression_changed? + scheduled? && delayed_job.cron != cron_expression + end + + def delayed_job + Delayed::Job + .where('handler LIKE ?', "%job_class: #{name}%") + .first + end + end +end diff --git a/config/deploy.rb b/config/deploy.rb index 5e7b2062e..fed78c0d9 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -64,6 +64,16 @@ namespace :after_party do end end +namespace :jobs_schedule do + desc "Run jobs_schedule tasks." + task :run do + command %{ + echo "-----> Running jobs_schedule" + #{echo_cmd %[bundle exec rake jobs:schedule]} + } + end +end + namespace :service do desc "Restart puma" task :restart_puma do @@ -123,4 +133,5 @@ task :post_deploy do command 'cd /home/ds/current' invoke :'after_party:run' + invoke :'jobs_schedule:run' end diff --git a/lib/tasks/jobs.rake b/lib/tasks/jobs.rake new file mode 100644 index 000000000..4f937bc44 --- /dev/null +++ b/lib/tasks/jobs.rake @@ -0,0 +1,8 @@ +namespace :jobs do + desc 'Schedule all cron jobs' + task schedule: :environment do + glob = Rails.root.join('app', 'jobs', '**', '*_job.rb') + Dir.glob(glob).each { |f| require f } + CronJob.subclasses.each(&:schedule) + end +end