From 8f8b63ac6acd8f9ce80276b1060dfae674e6b840 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 18 Apr 2024 11:18:53 +0200 Subject: [PATCH 1/3] fix: load the dols day by day --- app/jobs/cron/operations_signature_job.rb | 16 ++++---- .../cron/operations_signature_job_spec.rb | 40 +++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 spec/jobs/cron/operations_signature_job_spec.rb diff --git a/app/jobs/cron/operations_signature_job.rb b/app/jobs/cron/operations_signature_job.rb index 605cea333..5decb0a8f 100644 --- a/app/jobs/cron/operations_signature_job.rb +++ b/app/jobs/cron/operations_signature_job.rb @@ -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 diff --git a/spec/jobs/cron/operations_signature_job_spec.rb b/spec/jobs/cron/operations_signature_job_spec.rb new file mode 100644 index 000000000..94ce99f82 --- /dev/null +++ b/spec/jobs/cron/operations_signature_job_spec.rb @@ -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 From a10a59b2657ebda6af0d40d307514493ef1be64e Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 18 Apr 2024 11:19:51 +0200 Subject: [PATCH 2/3] perf: only load id digest --- app/jobs/cron/operations_signature_job.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/jobs/cron/operations_signature_job.rb b/app/jobs/cron/operations_signature_job.rb index 5decb0a8f..6985ac040 100644 --- a/app/jobs/cron/operations_signature_job.rb +++ b/app/jobs/cron/operations_signature_job.rb @@ -7,6 +7,7 @@ class Cron::OperationsSignatureJob < Cron::CronJob while start_date < last_midnight operations = DossierOperationLog + .select(:id, :digest) .where(executed_at: start_date...start_date.tomorrow, bill_signature: nil) BillSignatureService.sign_operations(operations, start_date) if operations.present? From 2745be5672a093311daa1032735259f531aa2ba7 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 18 Apr 2024 11:28:38 +0200 Subject: [PATCH 3/3] chore: remove old code --- app/services/bill_signature_service.rb | 8 ----- spec/services/bill_signature_service_spec.rb | 34 -------------------- 2 files changed, 42 deletions(-) diff --git a/app/services/bill_signature_service.rb b/app/services/bill_signature_service.rb index 7badb3399..685355e7b 100644 --- a/app/services/bill_signature_service.rb +++ b/app/services/bill_signature_service.rb @@ -1,12 +1,4 @@ class BillSignatureService - def self.grouped_unsigned_operation_until(date) - date = date.in_time_zone - unsigned_operations = DossierOperationLog - .where(bill_signature: nil) - .where('executed_at < ?', date) - unsigned_operations.group_by { |e| e.executed_at.to_date } - end - def self.sign_operations(operations, day) return unless Certigna::API.enabled? bill = BillSignature.build_with_operations(operations, day) diff --git a/spec/services/bill_signature_service_spec.rb b/spec/services/bill_signature_service_spec.rb index e9a0866ac..7f4f8c081 100644 --- a/spec/services/bill_signature_service_spec.rb +++ b/spec/services/bill_signature_service_spec.rb @@ -1,38 +1,4 @@ describe BillSignatureService do - describe ".grouped_unsigned_operation_until" do - subject { BillSignatureService.grouped_unsigned_operation_until(date).length } - - let(:date) { Time.zone.now.beginning_of_day } - - context "when operations of several days need to be signed" do - before do - create :dossier_operation_log, executed_at: 3.days.ago - create :dossier_operation_log, executed_at: 2.days.ago - create :dossier_operation_log, executed_at: 1.day.ago - end - - it { is_expected.to eq 3 } - end - - context "when operations on a single day need to be signed" do - before do - create :dossier_operation_log, executed_at: 1.day.ago - create :dossier_operation_log, executed_at: 1.day.ago - end - - it { is_expected.to eq 1 } - end - - context "when there are no operations to be signed" do - before do - create :dossier_operation_log, created_at: 1.day.ago, bill_signature: build(:bill_signature, :with_signature, :with_serialized) - create :dossier_operation_log, created_at: 1.day.from_now - end - - it { is_expected.to eq 0 } - end - end - describe ".sign_operations" do let(:date) { Date.today }