AshBilling.Reconciler (ash_billing v0.7.0)

Copy Markdown View Source

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

batch_report()

@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()]
}

item_result()

@type item_result() :: %{
  id: term(),
  provider_subscription_id: String.t(),
  status: :drifted | :reconciled | :unchanged | :skipped | :failed,
  changed_fields: [atom()],
  diff: %{optional(atom()) => %{local: term(), remote: term()}},
  error: String.t() | nil
}

mode()

@type mode() :: :dry_run | :execute

Functions

format_text(report)

@spec format_text(batch_report()) :: String.t()

Formats a reconciliation batch report as human-readable text.

run(opts)

@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 - Remote provider_subscription_id (e.g. "sub_123").
  • :id - Local subscription primary key.
  • :older_than_hours - Strictly positive number (> 0); selects records where last_reconciled_at is nil or older than N hours.
  • :stale_before - %DateTime{} cutoff for last_reconciled_at.
  • :statuses - Non-empty list of valid status atoms/strings to filter (e.g. [:active, :past_due]).
  • :missing_quantity? - When true, selects subscriptions where quantity is nil (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 be true when mode: :execute. Defaults to false.
  • :authorize? - Defaults to true so unauthorized actors cannot inspect or repair subscriptions. Pass authorize?: false only 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_id or :stripe_account exists 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 to AshBilling.reconcile_subscription/2 in :execute mode.
  • :page_size - Keyset pagination batch size (1..500, default 50). 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, default 2).
  • :rate_limit_ms - Global minimum interval in milliseconds enforced across all concurrent workers before issuing provider requests (default 0).
  • :req_opts - Outbound HTTP Req options (defaults to bounded timeouts with retry: false).