diff --git a/app/jobs/batch_operation_process_one_job.rb b/app/jobs/batch_operation_process_one_job.rb index 32870ac57..1d829ddd3 100644 --- a/app/jobs/batch_operation_process_one_job.rb +++ b/app/jobs/batch_operation_process_one_job.rb @@ -1,6 +1,12 @@ 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 @@ -17,4 +23,10 @@ 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 0b713bca9..fe442b568 100644 --- a/app/models/batch_operation.rb +++ b/app/models/batch_operation.rb @@ -95,7 +95,6 @@ class BatchOperation < ApplicationRecord def track_processed_dossier(success, dossier) dossiers.delete(dossier) touch(:run_at) if called_for_first_time? - touch(:finished_at) if called_for_last_time?(dossier) if success dossier_operation(dossier).done! @@ -124,10 +123,6 @@ class BatchOperation < ApplicationRecord run_at.nil? end - def called_for_last_time?(dossier_to_ignore) - dossiers.count.zero? - end - def total_count dossier_operations.size end diff --git a/spec/components/dossiers/batch_alert_component_spec.rb b/spec/components/dossiers/batch_alert_component_spec.rb index 1963ad5e9..0815d3af2 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 { - batch_operation.track_processed_dossier(true, dossier) - batch_operation.track_processed_dossier(true, dossier_2) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) batch_operation.reload } @@ -42,6 +42,7 @@ 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 } @@ -81,8 +82,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - batch_operation.track_processed_dossier(true, dossier) - batch_operation.track_processed_dossier(true, dossier_2) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) batch_operation.reload } @@ -96,6 +97,7 @@ 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 } @@ -135,8 +137,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - batch_operation.track_processed_dossier(true, dossier) - batch_operation.track_processed_dossier(true, dossier_2) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) batch_operation.reload } @@ -150,6 +152,7 @@ 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 } @@ -189,8 +192,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - batch_operation.track_processed_dossier(true, dossier) - batch_operation.track_processed_dossier(true, dossier_2) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) batch_operation.reload } @@ -204,6 +207,7 @@ 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 } @@ -243,8 +247,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - batch_operation.track_processed_dossier(true, dossier) - batch_operation.track_processed_dossier(true, dossier_2) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) batch_operation.reload } @@ -258,6 +262,7 @@ 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 } @@ -297,8 +302,8 @@ RSpec.describe Dossiers::BatchAlertComponent, type: :component do context 'finished and success' do before { - batch_operation.track_processed_dossier(true, dossier) - batch_operation.track_processed_dossier(true, dossier_2) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier) + BatchOperationProcessOneJob.perform_now(batch_operation, dossier_2) batch_operation.reload } @@ -312,6 +317,7 @@ 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 24946cd09..8b63e2dcf 100644 --- a/spec/jobs/batch_operation_process_one_job_spec.rb +++ b/spec/jobs/batch_operation_process_one_job_spec.rb @@ -23,6 +23,15 @@ 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 8120c122c..f3021bee9 100644 --- a/spec/models/batch_operation_spec.rb +++ b/spec/models/batch_operation_spec.rb @@ -98,15 +98,6 @@ 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