Add base cron job

This commit is contained in:
Paul Chavard 2020-03-28 16:45:16 +01:00
parent 33bf2fe109
commit 5005c54891
4 changed files with 50 additions and 15 deletions

View file

@ -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

29
app/jobs/cron_job.rb Normal file
View file

@ -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

View file

@ -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

8
lib/tasks/jobs.rake Normal file
View file

@ -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