From 6dc1f3e2c49a3a0de5b15d5fc106dc24b68f1a50 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 1 Apr 2020 15:26:46 +0200 Subject: [PATCH] Use human readable expressions instead of cron gibberish https://github.com/floraison/fugit#fugitnat --- Gemfile | 1 + Gemfile.lock | 7 +++++++ .../administrateur_activate_before_expiration_job.rb | 2 +- app/jobs/auto_archive_procedure_job.rb | 2 +- app/jobs/cron_job.rb | 10 +++++++++- app/jobs/declarative_procedures_job.rb | 2 +- app/jobs/discarded_dossiers_deletion_job.rb | 2 +- app/jobs/discarded_procedures_deletion_job.rb | 2 +- app/jobs/expired_dossiers_deletion_job.rb | 2 +- app/jobs/find_dubious_procedures_job.rb | 2 +- app/jobs/instructeur_email_notification_job.rb | 2 +- app/jobs/notify_draft_not_submitted_job.rb | 2 +- app/jobs/operations_signature_job.rb | 2 +- app/jobs/purge_stale_exports_job.rb | 2 +- app/jobs/purge_unattached_blobs_job.rb | 2 +- app/jobs/update_administrateur_usage_statistics_job.rb | 2 +- app/jobs/warn_expiring_dossiers_job.rb | 2 +- app/jobs/weekly_overview_job.rb | 2 +- lib/tasks/jobs.rake | 7 +++++++ 19 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Gemfile b/Gemfile index 92790bce2..55ba93826 100644 --- a/Gemfile +++ b/Gemfile @@ -28,6 +28,7 @@ gem 'dotenv-rails', require: 'dotenv/rails-now' # dotenv should always be loaded gem 'flipper' gem 'flipper-active_record' gem 'flipper-ui' +gem 'fugit' gem 'geocoder' gem 'gon' gem 'graphiql-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 2e4aafdf1..3f246ea16 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -203,6 +203,8 @@ GEM http_parser.rb (~> 0.6.0) erubi (1.9.0) erubis (2.7.0) + et-orbi (1.2.4) + tzinfo ethon (0.11.0) ffi (>= 1.3.0) eventmachine (1.2.7) @@ -235,6 +237,9 @@ GEM fog-json (>= 1.0) ipaddress (>= 0.8) formatador (0.2.5) + fugit (1.3.3) + et-orbi (~> 1.1, >= 1.1.8) + raabro (~> 1.1) geocoder (1.6.0) globalid (0.4.2) activesupport (>= 4.2.0) @@ -452,6 +457,7 @@ GEM puma (3.12.4) pundit (2.0.1) activesupport (>= 3.0.0) + raabro (1.1.6) rack (2.2.2) rack-attack (6.0.0) rack (>= 1.0, < 3) @@ -746,6 +752,7 @@ DEPENDENCIES flipper flipper-active_record flipper-ui + fugit geocoder gon graphiql-rails diff --git a/app/jobs/administrateur_activate_before_expiration_job.rb b/app/jobs/administrateur_activate_before_expiration_job.rb index 4b8f01beb..958ed9635 100644 --- a/app/jobs/administrateur_activate_before_expiration_job.rb +++ b/app/jobs/administrateur_activate_before_expiration_job.rb @@ -1,5 +1,5 @@ class AdministrateurActivateBeforeExpirationJob < CronJob - self.cron_expression = "0 8 * * *" + self.schedule_expression = "every day at 8 am" def perform(*args) Administrateur diff --git a/app/jobs/auto_archive_procedure_job.rb b/app/jobs/auto_archive_procedure_job.rb index 25a63d787..d8611b291 100644 --- a/app/jobs/auto_archive_procedure_job.rb +++ b/app/jobs/auto_archive_procedure_job.rb @@ -1,5 +1,5 @@ class AutoArchiveProcedureJob < CronJob - self.cron_expression = "* * * * *" + self.schedule_expression = "every 1 minute" def perform(*args) Procedure.publiees.where("auto_archive_on <= ?", Time.zone.today).each do |procedure| diff --git a/app/jobs/cron_job.rb b/app/jobs/cron_job.rb index 13b735e1d..adbc74dd4 100644 --- a/app/jobs/cron_job.rb +++ b/app/jobs/cron_job.rb @@ -1,6 +1,6 @@ class CronJob < ApplicationJob queue_as :cron - class_attribute :cron_expression + class_attribute :schedule_expression class << self def schedule @@ -12,6 +12,10 @@ class CronJob < ApplicationJob delayed_job.destroy if scheduled? end + def display_schedule + pp "#{name}: #{schedule_expression} cron(#{cron_expression})" + end + def scheduled? delayed_job.present? end @@ -25,5 +29,9 @@ class CronJob < ApplicationJob .where('handler LIKE ?', "%job_class: #{name}%") .first end + + def cron_expression + Fugit.do_parse(schedule_expression, multi: :fail).to_cron_s + end end end diff --git a/app/jobs/declarative_procedures_job.rb b/app/jobs/declarative_procedures_job.rb index 6f50d5ffa..af2bb395c 100644 --- a/app/jobs/declarative_procedures_job.rb +++ b/app/jobs/declarative_procedures_job.rb @@ -1,5 +1,5 @@ class DeclarativeProceduresJob < CronJob - self.cron_expression = "* * * * *" + self.schedule_expression = "every 1 minute" def perform(*args) Procedure.declarative.find_each(&:process_dossiers!) diff --git a/app/jobs/discarded_dossiers_deletion_job.rb b/app/jobs/discarded_dossiers_deletion_job.rb index 6b4b2d232..6b8e780d5 100644 --- a/app/jobs/discarded_dossiers_deletion_job.rb +++ b/app/jobs/discarded_dossiers_deletion_job.rb @@ -1,5 +1,5 @@ class DiscardedDossiersDeletionJob < CronJob - self.cron_expression = "0 7 * * *" + self.schedule_expression = "every day at 2 am" def perform(*args) Dossier.discarded_brouillon_expired.destroy_all diff --git a/app/jobs/discarded_procedures_deletion_job.rb b/app/jobs/discarded_procedures_deletion_job.rb index 8ea745e89..5361a422f 100644 --- a/app/jobs/discarded_procedures_deletion_job.rb +++ b/app/jobs/discarded_procedures_deletion_job.rb @@ -1,5 +1,5 @@ class DiscardedProceduresDeletionJob < CronJob - self.cron_expression = "0 7 * * *" + self.schedule_expression = "every day at 1 am" def perform(*args) Procedure.discarded_expired.find_each do |procedure| diff --git a/app/jobs/expired_dossiers_deletion_job.rb b/app/jobs/expired_dossiers_deletion_job.rb index 48d244b03..6adb8c39e 100644 --- a/app/jobs/expired_dossiers_deletion_job.rb +++ b/app/jobs/expired_dossiers_deletion_job.rb @@ -1,5 +1,5 @@ class ExpiredDossiersDeletionJob < CronJob - self.cron_expression = "0 7 * * *" + self.schedule_expression = "every day at 7 am" def perform(*args) ExpiredDossiersDeletionService.process_expired_dossiers_brouillon diff --git a/app/jobs/find_dubious_procedures_job.rb b/app/jobs/find_dubious_procedures_job.rb index 044d563ad..b3a06a63e 100644 --- a/app/jobs/find_dubious_procedures_job.rb +++ b/app/jobs/find_dubious_procedures_job.rb @@ -1,5 +1,5 @@ class FindDubiousProceduresJob < CronJob - self.cron_expression = "0 0 * * *" + self.schedule_expression = "every day at midnight" FORBIDDEN_KEYWORDS = [ 'NIR', 'NIRPP', 'race', 'religion', diff --git a/app/jobs/instructeur_email_notification_job.rb b/app/jobs/instructeur_email_notification_job.rb index 7c442cd6c..5b49576bf 100644 --- a/app/jobs/instructeur_email_notification_job.rb +++ b/app/jobs/instructeur_email_notification_job.rb @@ -1,5 +1,5 @@ class InstructeurEmailNotificationJob < CronJob - self.cron_expression = "0 10 * * MON-FRI" + self.schedule_expression = "from monday through friday at 10 am" def perform(*args) NotificationService.send_instructeur_email_notification diff --git a/app/jobs/notify_draft_not_submitted_job.rb b/app/jobs/notify_draft_not_submitted_job.rb index f4ceebcdd..2fbfe382f 100644 --- a/app/jobs/notify_draft_not_submitted_job.rb +++ b/app/jobs/notify_draft_not_submitted_job.rb @@ -1,5 +1,5 @@ class NotifyDraftNotSubmittedJob < CronJob - self.cron_expression = "0 7 * * *" + self.schedule_expression = "from monday through friday at 7 am" def perform(*args) Dossier.notify_draft_not_submitted diff --git a/app/jobs/operations_signature_job.rb b/app/jobs/operations_signature_job.rb index ffb98bf9b..2ad62da43 100644 --- a/app/jobs/operations_signature_job.rb +++ b/app/jobs/operations_signature_job.rb @@ -1,5 +1,5 @@ class OperationsSignatureJob < CronJob - self.cron_expression = "0 6 * * *" + self.schedule_expression = "every day at 6 am" def perform(*args) last_midnight = Time.zone.today.beginning_of_day diff --git a/app/jobs/purge_stale_exports_job.rb b/app/jobs/purge_stale_exports_job.rb index ff8389c1d..1f3480752 100644 --- a/app/jobs/purge_stale_exports_job.rb +++ b/app/jobs/purge_stale_exports_job.rb @@ -1,5 +1,5 @@ class PurgeStaleExportsJob < CronJob - self.cron_expression = "*/5 * * * *" + self.schedule_expression = "every 5 minutes" def perform Export.stale.destroy_all diff --git a/app/jobs/purge_unattached_blobs_job.rb b/app/jobs/purge_unattached_blobs_job.rb index 961b4542e..6ca57ba9f 100644 --- a/app/jobs/purge_unattached_blobs_job.rb +++ b/app/jobs/purge_unattached_blobs_job.rb @@ -1,5 +1,5 @@ class PurgeUnattachedBlobsJob < CronJob - self.cron_expression = "0 0 * * *" + self.schedule_expression = "every day at midnight" def perform(*args) ActiveStorage::Blob.unattached diff --git a/app/jobs/update_administrateur_usage_statistics_job.rb b/app/jobs/update_administrateur_usage_statistics_job.rb index a0959c59c..d873ee362 100644 --- a/app/jobs/update_administrateur_usage_statistics_job.rb +++ b/app/jobs/update_administrateur_usage_statistics_job.rb @@ -1,5 +1,5 @@ class UpdateAdministrateurUsageStatisticsJob < CronJob - self.cron_expression = "0 10 * * *" + self.schedule_expression = "every day at 10 am" def perform AdministrateurUsageStatisticsService.new.update_administrateurs diff --git a/app/jobs/warn_expiring_dossiers_job.rb b/app/jobs/warn_expiring_dossiers_job.rb index eec03825e..b1d099c8d 100644 --- a/app/jobs/warn_expiring_dossiers_job.rb +++ b/app/jobs/warn_expiring_dossiers_job.rb @@ -1,5 +1,5 @@ class WarnExpiringDossiersJob < CronJob - self.cron_expression = "0 0 1 * *" + self.schedule_expression = "every 1 month at midnight" def perform(*args) expiring, expired = Dossier diff --git a/app/jobs/weekly_overview_job.rb b/app/jobs/weekly_overview_job.rb index 5af0dbf07..e1e17541f 100644 --- a/app/jobs/weekly_overview_job.rb +++ b/app/jobs/weekly_overview_job.rb @@ -1,5 +1,5 @@ class WeeklyOverviewJob < CronJob - self.cron_expression = "0 7 * * MON" + self.schedule_expression = "every monday at 7 am" def perform(*args) # Feature flipped to avoid mails in staging due to unprocessed dossier diff --git a/lib/tasks/jobs.rake b/lib/tasks/jobs.rake index 4f937bc44..aac51c291 100644 --- a/lib/tasks/jobs.rake +++ b/lib/tasks/jobs.rake @@ -5,4 +5,11 @@ namespace :jobs do Dir.glob(glob).each { |f| require f } CronJob.subclasses.each(&:schedule) end + + desc 'Display schedule for all cron jobs' + task display_schedule: :environment do + glob = Rails.root.join('app', 'jobs', '**', '*_job.rb') + Dir.glob(glob).each { |f| require f } + CronJob.subclasses.each(&:display_schedule) + end end