change the way we set the finished_at on batch operation job to fix alert display bug

This commit is contained in:
Lisa Durand 2023-01-27 10:32:38 +01:00
parent b93b8a7230
commit d0a131dc55
5 changed files with 39 additions and 26 deletions

View file

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

View file

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

View file

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

View file

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

View file

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