Overload the finished_at

This commit is contained in:
Lisa Durand 2023-02-01 14:59:37 +01:00
parent d0a131dc55
commit 120fbb0ef7
5 changed files with 26 additions and 39 deletions

View file

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

View file

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

View file

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

View file

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

View file

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