Safe, auditable single-subscription and bulk reconciliation runner supporting read-only dry-run drift inspection, explicit write confirmation, stable keyset batch pagination, bounded concurrency, global cross-worker rate limiting, and strict actor/tenant/Stripe Connect isolation.
Used programmatically or via mix ash_billing.reconcile.
Summary
Functions
Formats a reconciliation batch report as human-readable text.
Runs a single-subscription or filtered bulk reconciliation pass.
Types
@type batch_report() :: %{ mode: mode(), total: non_neg_integer(), drifted: non_neg_integer(), reconciled: non_neg_integer(), unchanged: non_neg_integer(), skipped: non_neg_integer(), failed: non_neg_integer(), pages_processed: non_neg_integer(), results: [item_result()] }
@type mode() :: :dry_run | :execute
Functions
@spec format_text(batch_report()) :: String.t()
Formats a reconciliation batch report as human-readable text.
@spec run(keyword()) :: {:ok, batch_report()} | {:error, term()}
Runs a single-subscription or filtered bulk reconciliation pass.
Defaults to mode: :dry_run (read-only). Writing to PostgreSQL requires both
mode: :execute and confirm?: true.
All selectors and isolation options are strictly validated before any database
query or outbound provider HTTP request is executed. Invalid filters never fall
back to an unfiltered scan; only an explicit all?: true authorizes an
unfiltered sweep (still strictly scoped by tenant and Stripe Connect isolation).
Required Options
:subscription_resource- The Ash subscription resource module.:domain- The Ash domain module.
Selection Options (at least one required)
:subscription_id- Remoteprovider_subscription_id(e.g."sub_123").:id- Local subscription primary key.:older_than_hours- Strictly positive number (> 0); selects records wherelast_reconciled_atisnilor older thanNhours.:stale_before-%DateTime{}cutoff forlast_reconciled_at.:statuses- Non-empty list of valid status atoms/strings to filter (e.g.[:active, :past_due]).:missing_quantity?- Whentrue, selects subscriptions wherequantityisnil(fails if the resource lacks:quantity).:all?- Explicit opt-in to scan all matching subscriptions without a narrower filter.
Safety, Isolation & Throughput Options
:mode-:dry_run(default) or:execute.:confirm?- Must betruewhenmode: :execute. Defaults tofalse.:authorize?- Defaults totrueso unauthorized actors cannot inspect or repair subscriptions. Passauthorize?: falseonly for trusted system jobs.:actor- Ash actor used for policy checks on reads and reconciliation writes.:tenant- Required for multitenant resources; rejected on non-multitenant resources.:stripe_account- Optional Stripe Connect account ID forwarded to provider calls and enforced at the database layer.:stripe_account_attribute- Optional attribute name (atom or string) on the subscription resource storing the Stripe Connect account ID. Auto-detected if:stripe_account_idor:stripe_accountexists on the resource.:stripe_account_scope- Pass:tenant("tenant") to explicitly opt into bulk Connect reconciliation on a multitenant resource without a dedicated Connect account column (when tenants map 1:1 to Connect accounts).:provider- Optional provider module override.:customer_resource- Optional customer resource module override.:reset_event_cursor?- Forwarded toAshBilling.reconcile_subscription/2in:executemode.:page_size- Keyset pagination batch size (1..500, default50). Records are fetched and processed one page at a time.:limit- Optional maximum number of subscriptions to process.:concurrency- Max concurrent workers per batch (1..10, default2).:rate_limit_ms- Global minimum interval in milliseconds enforced across all concurrent workers before issuing provider requests (default0).:req_opts- Outbound HTTPReqoptions (defaults to bounded timeouts withretry: false).