diff --git a/app/jobs/financekit_inbox_job.rb b/app/jobs/financekit_inbox_job.rb index 1f1edcc4b9..22d8e3a297 100644 --- a/app/jobs/financekit_inbox_job.rb +++ b/app/jobs/financekit_inbox_job.rb @@ -6,13 +6,15 @@ class FinancekitInboxJob < ApplicationJob items.find_each do |item| next unless Financekit.enabled?(item.family) - # Ids only: a drained capture still holds its payload bytes. + # Ids only: a drained capture still holds its payload bytes. Every part of + # a multi-chunk capture is collected, so none is left for the sweep below + # to fan out a second time in this same run. applied_ids = [] Financekit::MAX_QUEUED.times do - batch = Financekit::Processor.new(item).apply_next! - break unless batch + parts = Financekit::Processor.new(item).apply_next! + break if parts.blank? - applied_ids << batch.id + applied_ids.concat(parts.map(&:id)) end Financekit::Downstream.new(item, FinancekitBatch.where(id: applied_ids)).perform! end diff --git a/app/models/financekit/processor.rb b/app/models/financekit/processor.rb index e4324aca9e..be0a873644 100644 --- a/app/models/financekit/processor.rb +++ b/app/models/financekit/processor.rb @@ -3,8 +3,12 @@ class Financekit::Processor @item = item end + # Returns every part of the capture it applied, so the caller can complete + # them together. Returning only the last chunk left the earlier ones without + # downstream work, and the recovery sweep then repeated the fan-out for them. def apply_next! batch = nil + applied = nil @item.with_lock do @item.require_publisher! batch = @item.financekit_batches.find_by(generation: @item.generation, @@ -39,8 +43,9 @@ class Financekit::Processor @item.update!(next_sequence: last.sequence + 1, predecessor_digest: last.payload_digest, last_imported_at: applied_at, last_captured_at: last.captured_at) batch = last + applied = capture end - batch + applied rescue ActiveRecord::RecordInvalid => error # Retryable. RecordInvalid covers ordinary races — a concurrent balance # observation or conflict insert, an entry validation a later attempt diff --git a/test/models/financekit/inbox_test.rb b/test/models/financekit/inbox_test.rb index c52a6fd4e0..f7af34a04d 100644 --- a/test/models/financekit/inbox_test.rb +++ b/test/models/financekit/inbox_test.rb @@ -207,6 +207,26 @@ class Financekit::InboxTest < ActiveSupport::TestCase assert_not_nil @item.reload.last_downstream_at end + test "a multi-chunk capture fans downstream work out once, not once per chunk" do + capture_id = SecureRandom.uuid + first_payload = financekit_payload(events: []) + first_payload.merge!("capture_id" => capture_id, "chunk_index" => 0, "chunk_count" => 2) + first, first_raw = accept_batch(first_payload) + second_payload = financekit_payload(sequence: 2, predecessor_digest: Digest::SHA256.hexdigest(first_raw), + events: []) + second_payload.merge!("capture_id" => capture_id, "chunk_index" => 1, "chunk_count" => 2) + second, = accept_batch(second_payload) + + # Completing only the last chunk left the earlier ones for the recovery + # sweep, which then repeated the whole fan-out inside the same job run. + Family.any_instance.expects(:auto_match_transfers!).once.returns(nil) + + FinancekitInboxJob.perform_now(@item.id) + + assert_not_nil first.reload.downstream_completed_at + assert_not_nil second.reload.downstream_completed_at + end + test "the sweep recovers a backlog without materializing batch rows" do first = accept_and_apply second = accept_and_apply(financekit_payload(sequence: 2, predecessor_digest: first.payload_digest, diff --git a/test/support/financekit_test_helper.rb b/test/support/financekit_test_helper.rb index 50420de527..6673a57962 100644 --- a/test/support/financekit_test_helper.rb +++ b/test/support/financekit_test_helper.rb @@ -121,9 +121,9 @@ module FinancekitTestHelper def accept_and_apply(payload = financekit_payload, item: @item) batch, = accept_batch(payload, item: item) applied = Financekit::Processor.new(item).apply_next! - assert applied + assert applied.present? # apply_next! no longer fans out on its own; the job batches a whole drain. - Financekit::Downstream.new(item, FinancekitBatch.where(id: applied.id)).perform! + Financekit::Downstream.new(item, FinancekitBatch.where(id: applied.map(&:id))).perform! batch.reload end end