Complete every chunk of a capture, not just the last

apply_next! returned only the final part of the capture it applied, so
the drain completed downstream work for that chunk alone. The recovery
sweep then found the earlier chunks still pending and ran the whole
family fan-out a second time, inside the same job run — measured at two
fan-outs for one two-chunk capture, which defeats the batching this
branch is for.

It now returns every part it applied and the job concatenates their ids,
so a capture is completed as a unit. The sweep keeps its own job.

The new test reports "expected exactly once, invoked twice" against the
previous return value.

Reported by CodeRabbit on #3678.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BP2oKbsBexNUUx2M7oiqH6
This commit is contained in:
Claude
2026-09-23 06:46:18 +00:00
parent b970c236ae
commit 3b3ece14e7
4 changed files with 34 additions and 7 deletions
+6 -4
View File
@@ -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
+6 -1
View File
@@ -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
+20
View File
@@ -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,
+2 -2
View File
@@ -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