From 120fbb0ef7abe69d329a3b6e71d30c1b00cece73 Mon Sep 17 00:00:00 2001 From: Lisa Durand Date: Wed, 1 Feb 2023 14:59:37 +0100 Subject: [PATCH] Overload the finished_at --- app/jobs/batch_operation_process_one_job.rb | 12 -------- app/models/batch_operation.rb | 5 ++++ .../dossiers/batch_alert_component_spec.rb | 30 ++++++++----------- .../batch_operation_process_one_job_spec.rb | 9 ------ spec/models/batch_operation_spec.rb | 9 ++++++ 5 files changed, 26 insertions(+), 39 deletions(-) diff --git a/app/jobs/batch_operation_process_one_job.rb b/app/jobs/batch_operation_process_one_job.rb index 1d829ddd3..32870ac57 100644 --- a/app/jobs/batch_operation_process_one_job.rb +++ b/app/jobs/batch_operation_process_one_job.rb @@ -1,12 +1,6 @@ class BatchOperationProcessOneJob < ApplicationJob retry_on StandardError, attempts: 1 # default 5, for now no retryable behavior - after_perform do |job| - if called_for_last_time?(job) - job.arguments.first.touch(:finished_at) - end - end - def perform(batch_operation, dossier) dossier = batch_operation.dossiers_safe_scope.find(dossier.id) begin @@ -23,10 +17,4 @@ class BatchOperationProcessOneJob < ApplicationJob rescue ActiveRecord::RecordNotFound dossier.update_column(:batch_operation_id, nil) end - - private - - def called_for_last_time?(job) - job.arguments.first.dossiers.count.zero? - end end diff --git a/app/models/batch_operation.rb b/app/models/batch_operation.rb index fe442b568..2bfed5a09 100644 --- a/app/models/batch_operation.rb +++ b/app/models/batch_operation.rb @@ -95,6 +95,7 @@ class BatchOperation < ApplicationRecord def track_processed_dossier(success, dossier) dossiers.delete(dossier) touch(:run_at) if called_for_first_time? + touch(:finished_at) if success dossier_operation(dossier).done! @@ -139,6 +140,10 @@ class BatchOperation < ApplicationRecord dossier_operations.error.present? end + def finished_at + dossiers.empty? ? run_at : nil + end + private def dossier_operation(dossier) diff --git a/spec/components/dossiers/batch_alert_component_spec.rb b/spec/components/dossiers/batch_alert_component_spec.rb index 0815d3af2..1963ad5e9 100644 --- a/spec/components/dossiers/batch_alert_component_spec.rb +++ b/spec/components/dossiers/batch_alert_component_spec.rb @@ -27,8 +27,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - BatchOperationProcessOneJob.perform_now(batch_operation, dossier) - BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) + batch_operation.track_processed_dossier(true, dossier) + batch_operation.track_processed_dossier(true, dossier_2) batch_operation.reload } @@ -42,7 +42,6 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do before { batch_operation.track_processed_dossier(false, dossier) batch_operation.track_processed_dossier(true, dossier_2) - batch_operation.touch(:finished_at) batch_operation.reload } @@ -82,8 +81,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - BatchOperationProcessOneJob.perform_now(batch_operation, dossier) - BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) + batch_operation.track_processed_dossier(true, dossier) + batch_operation.track_processed_dossier(true, dossier_2) batch_operation.reload } @@ -97,7 +96,6 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do before { batch_operation.track_processed_dossier(false, dossier) batch_operation.track_processed_dossier(true, dossier_2) - batch_operation.touch(:finished_at) batch_operation.reload } @@ -137,8 +135,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - BatchOperationProcessOneJob.perform_now(batch_operation, dossier) - BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) + batch_operation.track_processed_dossier(true, dossier) + batch_operation.track_processed_dossier(true, dossier_2) batch_operation.reload } @@ -152,7 +150,6 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do before { batch_operation.track_processed_dossier(false, dossier) batch_operation.track_processed_dossier(true, dossier_2) - batch_operation.touch(:finished_at) batch_operation.reload } @@ -192,8 +189,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - BatchOperationProcessOneJob.perform_now(batch_operation, dossier) - BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) + batch_operation.track_processed_dossier(true, dossier) + batch_operation.track_processed_dossier(true, dossier_2) batch_operation.reload } @@ -207,7 +204,6 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do before { batch_operation.track_processed_dossier(false, dossier) batch_operation.track_processed_dossier(true, dossier_2) - batch_operation.touch(:finished_at) batch_operation.reload } @@ -247,8 +243,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - BatchOperationProcessOneJob.perform_now(batch_operation, dossier) - BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) + batch_operation.track_processed_dossier(true, dossier) + batch_operation.track_processed_dossier(true, dossier_2) batch_operation.reload } @@ -262,7 +258,6 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do before { batch_operation.track_processed_dossier(false, dossier) batch_operation.track_processed_dossier(true, dossier_2) - batch_operation.touch(:finished_at) batch_operation.reload } @@ -302,8 +297,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - BatchOperationProcessOneJob.perform_now(batch_operation, dossier) - BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) + batch_operation.track_processed_dossier(true, dossier) + batch_operation.track_processed_dossier(true, dossier_2) batch_operation.reload } @@ -317,7 +312,6 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do before { batch_operation.track_processed_dossier(false, dossier) batch_operation.track_processed_dossier(true, dossier_2) - batch_operation.touch(:finished_at) batch_operation.reload } diff --git a/spec/jobs/batch_operation_process_one_job_spec.rb b/spec/jobs/batch_operation_process_one_job_spec.rb index 8b63e2dcf..24946cd09 100644 --- a/spec/jobs/batch_operation_process_one_job_spec.rb +++ b/spec/jobs/batch_operation_process_one_job_spec.rb @@ -23,15 +23,6 @@ describe BatchOperationProcessOneJob, type: :job do expect(batch_operation.dossier_operations.error.pluck(:dossier_id)).to eq([dossier_job.id]) end - it 'sets finished_at when it is the last job' do - BatchOperationProcessOneJob.new(batch_operation, batch_operation.dossiers.second).perform_now - BatchOperationProcessOneJob.new(batch_operation, batch_operation.dossiers.last).perform_now - expect { subject.perform_now } - .to change { batch_operation.finished_at } - .from(nil) - .to(anything) - end - context 'when operation is "archiver"' do it 'archives the dossier in the batch' do expect { subject.perform_now } diff --git a/spec/models/batch_operation_spec.rb b/spec/models/batch_operation_spec.rb index f3021bee9..8120c122c 100644 --- a/spec/models/batch_operation_spec.rb +++ b/spec/models/batch_operation_spec.rb @@ -98,6 +98,15 @@ describe BatchOperation, type: :model do .not_to change { batch_operation.reload.run_at } end end + + context 'when it is the last job' do + it 'sets finished_at' do + expect { batch_operation.track_processed_dossier(true, dossier) } + .to change { batch_operation.reload.finished_at } + .from(nil) + .to(anything) + end + end end describe '#dossiers_safe_scope (with archiver)' do