From d20114f9783324daef446ecf9548dc3545643d20 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 8 Mar 2023 13:48:44 +0100 Subject: [PATCH 1/6] chore(sentry): use "procedure" tag everywhere for a better debug experience --- app/jobs/export_job.rb | 2 +- app/services/serializer_service.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/jobs/export_job.rb b/app/jobs/export_job.rb index 3884f97d7..a3c581095 100644 --- a/app/jobs/export_job.rb +++ b/app/jobs/export_job.rb @@ -4,7 +4,7 @@ class ExportJob < ApplicationJob discard_on ActiveRecord::RecordNotFound before_perform do |job| - Sentry.set_tags(procedure_id: job.arguments.first.procedure.id) + Sentry.set_tags(procedure: job.arguments.first.procedure.id) end def perform(export) diff --git a/app/services/serializer_service.rb b/app/services/serializer_service.rb index bd7442a28..c569038da 100644 --- a/app/services/serializer_service.rb +++ b/app/services/serializer_service.rb @@ -10,7 +10,7 @@ class SerializerService def self.dossiers(procedure) Sentry.with_scope do |scope| - scope.set_tags(procedure_id: procedure.id) + scope.set_tags(procedure: procedure.id) data = execute_query('serializeDossiers', { number: procedure.id }) data && data['demarche']['dossiers'] From 6b87c290cb6c981f74d2a875b8963e587ebde8c4 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 8 Mar 2023 15:31:10 +0100 Subject: [PATCH 2/6] fix(cron): PurgeOldEmailEvent in batches --- app/jobs/cron/purge_old_email_event_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/jobs/cron/purge_old_email_event_job.rb b/app/jobs/cron/purge_old_email_event_job.rb index b748a0fea..5de61c5c0 100644 --- a/app/jobs/cron/purge_old_email_event_job.rb +++ b/app/jobs/cron/purge_old_email_event_job.rb @@ -2,6 +2,6 @@ class Cron::PurgeOldEmailEventJob < Cron::CronJob self.schedule_expression = "every week at 3:00" def perform - EmailEvent.outdated.destroy_all + EmailEvent.outdated.in_batches.destroy_all end end From 86f15b8c0543fd065975f8ae726bc43d2f7ce3f6 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 8 Mar 2023 18:31:34 +0100 Subject: [PATCH 3/6] chore(job): config delayed job for long-living export jobs --- app/jobs/application_job.rb | 4 ++++ app/jobs/export_job.rb | 4 ++++ config/initializers/delayed_job.rb | 4 ++++ 3 files changed, 12 insertions(+) create mode 100644 config/initializers/delayed_job.rb diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index 691c33e40..cd19f09c6 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -21,6 +21,10 @@ class ApplicationJob < ActiveJob::Base ENV.fetch("MAX_ATTEMPTS_JOBS", DEFAULT_MAX_ATTEMPTS_JOBS).to_i end + def max_run_time + 4.hours # decrease run time by default + end + def request_id @request_id ||= Current.request_id end diff --git a/app/jobs/export_job.rb b/app/jobs/export_job.rb index a3c581095..86ce5fefb 100644 --- a/app/jobs/export_job.rb +++ b/app/jobs/export_job.rb @@ -7,6 +7,10 @@ class ExportJob < ApplicationJob Sentry.set_tags(procedure: job.arguments.first.procedure.id) end + def max_run_time + Export::MAX_DUREE_GENERATION + end + def perform(export) return if export.generated? diff --git a/config/initializers/delayed_job.rb b/config/initializers/delayed_job.rb new file mode 100644 index 000000000..88d09dd26 --- /dev/null +++ b/config/initializers/delayed_job.rb @@ -0,0 +1,4 @@ +# Set max_run_time at the highest job duration we want, +# then at job level we'll decrease this value to a lower value +# except for ExportJob. +Delayed::Worker.max_run_time = 16.hours # same as Export::MAX_DUREE_GENERATION but we can't yet use this constant here From 9e6d06fd9c8c256f1520d855dd1bd47226332c0a Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 8 Mar 2023 18:45:02 +0100 Subject: [PATCH 4/6] fix(job): attempt to fix a pg timeout int Cron::NotifyDraftNotSubmittedJob --- app/models/dossier.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index d2a446f9d..5a1ab913e 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -1352,7 +1352,6 @@ class Dossier < ApplicationRecord def self.notify_draft_not_submitted brouillon_near_procedure_closing_date - .includes(:user) .find_each do |dossier| DossierMailer.notify_brouillon_not_submitted(dossier).deliver_later end From 697aeeb348daf8a18cfd8a9cfc92cf3555fba30b Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Thu, 9 Mar 2023 15:18:14 +0100 Subject: [PATCH 5/6] refactor(job): DossieRebaseJob in new queue so they don't interfere with others MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ces jobs sont empilés par milliers d'un coup et peuvent mettre des dizaines de minutes à s'écouler. On ne veut pas que ça interfère avec d'autres jobs, notamment les scans antivirus qui, si retardés, empilent des "poll" pour connaitre le status du fichier. --- app/jobs/dossier_rebase_job.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/jobs/dossier_rebase_job.rb b/app/jobs/dossier_rebase_job.rb index 916d9d1e1..50d1c7d36 100644 --- a/app/jobs/dossier_rebase_job.rb +++ b/app/jobs/dossier_rebase_job.rb @@ -1,4 +1,6 @@ class DossierRebaseJob < ApplicationJob + queue_as :low_priority # they are massively enqueued, so don't interfere with others especially antivirus + # If by the time the job runs the Dossier has been deleted, ignore the rebase discard_on ActiveRecord::RecordNotFound From 4288f28ac810a3d49e18cd332e9066705b8fd3d6 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Thu, 9 Mar 2023 15:37:13 +0100 Subject: [PATCH 6/6] chore(job): same max_run_time between ArchiveCreationJob and ExportJob --- app/jobs/archive_creation_job.rb | 8 ++++++++ app/models/archive.rb | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/jobs/archive_creation_job.rb b/app/jobs/archive_creation_job.rb index 8fec78ef6..7c00a60f3 100644 --- a/app/jobs/archive_creation_job.rb +++ b/app/jobs/archive_creation_job.rb @@ -1,6 +1,14 @@ class ArchiveCreationJob < ApplicationJob queue_as :archives + before_perform do |job| + Sentry.set_tags(procedure: job.arguments.first.id) + end + + def max_run_time + Archive::MAX_DUREE_GENERATION + end + def perform(procedure, archive, administrateur_or_instructeur) archive.compute_with_safe_stale_for_purge do ProcedureArchiveService diff --git a/app/models/archive.rb b/app/models/archive.rb index e6230b8b7..0c7bd17cf 100644 --- a/app/models/archive.rb +++ b/app/models/archive.rb @@ -14,7 +14,7 @@ class Archive < ApplicationRecord include TransientModelsWithPurgeableJobConcern RETENTION_DURATION = 4.days - MAX_DUREE_GENERATION = 24.hours + MAX_DUREE_GENERATION = 16.hours MAX_SIZE = 100.gigabytes has_and_belongs_to_many :groupe_instructeurs