Recalculate the account after a Wise linked-balance processing failure

JA
6h ago
+241-19

Closes #6105

Fork pre-flight: jaysbeekay/sure#1060 (same branch, up/wise-linked-failure-sync, same commit).

Summary

When WiseItem::ProcessLinkedAccountJob fails while processing a newly linked balance, after the processor has committed transactions, the account is now recalculated, as on success. Before, the rescue only logged, so the booked entries had no materialised balances, no completed Sync for the family caches and no transfer matching until the next connection sync.

What changed

  • The job tracks its stage. After a processing failure the rescue queues account.sync_later once it has logged. The processor commits rows one by one, and a raise loses the signal of whether any were committed, so the job recalculates either way; without new rows the account sync re-materialises balances that are already right. sync_later joins a visible sync, so it does not duplicate one the connection's own sync scheduled.
  • After a jar-linking failure it does not recalculate: the account sync's transfer matching must not see legs the linker has not linked. The next connection sync links before it recalculates.
  • The success path's sync_later moves to the rescue's else branch, so a failure to schedule is not logged as a failed process and is not tried twice.
  • A failure to schedule is logged with a DebugLogEntry (provider wise, the balance's account provider, the error class), so it never turns a logged failure into a failed job. Nothing is scheduled when the account itself could not be read.
  • The ActiveRecord::Deadlocked branch is unchanged.

Non-test diff: +27 / -2 in app/jobs/wise_item/process_linked_account_job.rb.

Left out: making the processor transactional (the job comment explains why), retrying, the current balance after an update_balance! failure (a :reverse account sync does not write accounts.balance), a sync already running when the rows commit (the success path has the same gap), and WiseItem::Syncer Phase 3 calling link_jar_transfers! without a rescue.

Demo data

No demo data needed: no Wise data in the demo generator, and this is a failure path.

Migration notes

No migration.

Verification

  • Red first on main 7e8693344: "recalculates the account when processing fails after booking its transactions" failed with syncs.count didn't change by 1, but by 0; the updated "logs a processing failure for support instead of raising" with Expected: 1 Actual: 0. The deadlock test (now asserting no Sync), "does not recalculate the account when jar linking fails" and "a failure before the account is read" pass on main, as guards.
  • Red first on the first commit edae65b46: "does not recalculate the account when jar linking fails" (syncs changed by 1, expected 0); "a sync_later failure after success is logged once as a scheduling failure" (logged as "Failed to process"); "a sync_later failure after a processing failure is logged for support" (DebugLogEntry +1, expected +2).
  • Mutations, each red: recalculation after processing removed (both processing-failure tests); recalculation after a linking failure too (the linking test); the else recalculation removed (the success test and the success scheduling test); sync_later back in the body (scheduling test logs "Failed to process"); the stage never set (both processing-failure tests); recalculate's rescue removed (the scheduling-failure test raises); its DebugLogEntry removed (both scheduling tests); the Account#update! stub removed (first positive test, DebugLogEntry 0, expected 1).
  • bin/rails test test/jobs/wise_item test/models/wise_item test/models/wise_item_test.rb test/models/wise_account/processor_test.rb: 254 runs, 0 failures.
  • Review follow-up (07d803c26, 3d7c7b18b): a deadlock while scheduling the recalculation is re-raised so ApplicationJob's retry_on retries the job, on both the success and the processing-failure path; any other scheduling failure is still logged. The failure log's metadata names the stage that failed (loading, processing or linking). "retries a deadlock scheduling the recalculation" failed before with No enqueued job found; the stage tests failed with Actual: nil.
  • Full bin/rails test on 3d7c7b18b: 20282 runs, 0 failures, 8 errors (all ReleaseNotesWorkflowTest: node is missing from the local test image), 9 skips.
  • rubocop, erb_lint, Brakeman: clean.

Blast radius

The job is queued only from WiseItem#sync_linked_account_later, which WiseItemsController#complete_account_setup, #link_accounts and #link_existing_account call. Nothing else calls the job or reads its result. account.sync_later is the call the success path already makes. #5278 adds categorize_card_refunds! before sync_later inside the same begin, so a failure there is covered by this rescue too; #4740 changes only the header comment.


Summary by cubic

Closes we-promise/sure#6105.

When WiseItem::ProcessLinkedAccountJob fails after the processor has booked transactions for a newly linked Wise balance, the account is now recalculated as on success. The failure path previously only logged, so committed entries went without materialised balances, a completed Sync, or transfer matching until the next connection sync.

  • The job tracks whether it is processing or linking. A processing failure links the connection's transfers before recalculating, so the account sync's transfer matching never sees unlinked legs; a linking failure skips the recalculation.
  • The success path's sync_later moves to the rescue's else branch, so a scheduling failure is logged as such and not tried twice.
  • A deadlock is now re-raised for ApplicationJob's retry wherever it occurs, including inside InterbalanceLinker#link_entries, which no longer counts a deadlocked pair as failed and recalculates over unlinked legs; other failures are logged with a DebugLogEntry so a logged failure never becomes a failed job.

Summary by CodeRabbit

  • Bug Fixes
    • Account balances are refreshed after successful processing and when processing has committed changes before an error occurs.
    • Failures during account loading or transfer linking are logged without triggering an account refresh.
    • Non-deadlock processing errors no longer prevent transfer linking.
    • Deadlocks during processing, transfer linking, or account-sync scheduling trigger a retry; other scheduling failures are logged separately.