fix: load the dols day by day

This commit is contained in:
simon lehericey 2024-04-18 11:18:53 +02:00
parent efc1fafaab
commit 8f8b63ac6a
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
2 changed files with 49 additions and 7 deletions

View file

@ -2,14 +2,16 @@ class Cron::OperationsSignatureJob < Cron::CronJob
self.schedule_expression = "every day at 6 am"
def perform(*args)
start_date = DossierOperationLog.where(bill_signature: nil).order(:executed_at).pick(:executed_at).beginning_of_day
last_midnight = Time.zone.today.beginning_of_day
operations_by_day = BillSignatureService.grouped_unsigned_operation_until(last_midnight)
operations_by_day.each do |day, operations|
begin
BillSignatureService.sign_operations(operations, day)
rescue
raise # let errors show up on Sentry and delayed_jobs
end
while start_date < last_midnight
operations = DossierOperationLog
.where(executed_at: start_date...start_date.tomorrow, bill_signature: nil)
BillSignatureService.sign_operations(operations, start_date) if operations.present?
start_date = start_date.tomorrow
end
end
end

View file

@ -0,0 +1,40 @@
RSpec.describe Cron::OperationsSignatureJob, type: :job do
describe 'perform' do
subject { Cron::OperationsSignatureJob.perform_now }
let(:today) { Time.zone.parse('2018-01-05 00:06:00') }
before do
travel_to(today)
allow(BillSignatureService).to receive(:sign_operations)
end
context "with dol without signature executed_at two_days_ago" do
let(:two_days_ago_00_30) { Time.zone.parse('2018-01-03 00:30:00') }
let(:two_days_ago_00_00) { Time.zone.parse('2018-01-03 00:00:00') }
let(:one_day_ago_00_30) { Time.zone.parse('2018-01-04 00:30:00') }
let(:one_day_ago_00_00) { Time.zone.parse('2018-01-04 00:00:00') }
let!(:dol_1) { create(:dossier_operation_log, executed_at: two_days_ago_00_30) }
let!(:dol_2) { create(:dossier_operation_log, executed_at: one_day_ago_00_30) }
before { subject }
it do
expect(BillSignatureService).to have_received(:sign_operations).exactly(2).times
expect(BillSignatureService).to have_received(:sign_operations).with([dol_1], two_days_ago_00_00)
expect(BillSignatureService).to have_received(:sign_operations).with([dol_2], one_day_ago_00_00)
end
end
context "with dol without signature executed_at today past midnight" do
let(:today_00_01) { Time.zone.parse('2018-01-05 00:00:01') }
let!(:dol) { create(:dossier_operation_log, executed_at: today_00_01) }
before { subject }
it { expect(BillSignatureService).not_to have_received(:sign_operations) }
end
end
end