Skip to content

feat(billing): record ended and skipped subscription sync results - #5548

Merged
turip merged 2 commits into
mainfrom
feat/subscription-sync-terminal-states
Nov 16, 2026
Merged

turip merged 2 commits into
mainfrom
feat/subscription-sync-terminal-states

Conversation

@turip

@turip turip commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Overview

Record subscription_ended after normal billing reconciliation succeeds for a subscription whose ActiveTo has passed and whose requested AsOf covers that end. A future cancellation request or a historical horizon does not establish final reconciliation.

By default, subscriptions whose ActiveTo is more than 30 days before the worker clock record subscription_skipped without loading the subscription view or billing artifacts, changing legacy scheduling, or collecting pending lines. Exactly 30 days old remains eligible. Subscription-deletion and customer-deleted-before-start cleanup retain precedence and still reconcile an empty target regardless of age.

Adds the independent ForceResyncOldSubscriptions input and sync option. CLI --f retains its billing-deadline override; new --force-all enables both overrides. Lookback and customer/namespace selection still apply, including the existing exclusion of completed subscription-deletion cleanup.

Notes for reviewer

  • Ended results commit with billing changes and legacy scheduling. An intentional skipped outcome acknowledges the observed revision's age-policy decision, not completed billing reconciliation. Both paths preserve newer current revisions; dry runs and failed transactions record no result.
  • Subscription hooks and further periodic suppression using v2 state remain separate follow-up work.
  • Tests exercise both legacy invoicing and the real charges/ledger stack: end/horizon and 30-day boundaries, forced resync replacing a skip, independent force inputs and lookback selection, deletion precedence, dry runs, result-write rollback, and skipping view/artifact reads after acquiring the billing lock.
  • Verified after rebasing onto origin/main: PostgreSQL-backed subscription-sync tests, scoped fast lint including the CLI, Ent regeneration, and Atlas schema diff. The enum values use the existing string column; Atlas requires no SQL migration.

Summary by CodeRabbit

  • New Features
    • Added a --force-all option to resynchronize older subscriptions, while retaining the existing -f behavior. Lookback settings still limit which subscriptions are selected.
    • Sync results now distinguish subscriptions successfully reconciled after ending from those skipped because they ended more than 30 days ago.
  • Documentation
    • Clarified sync outcomes, resynchronization options, and how skipped subscriptions affect billing reconciliation and pending-line collection.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no blocking issues were found.

Summary

Adds recorded outcomes for ended subscriptions and subscriptions skipped by the 30-day age policy.

  • Subscription sync records ended results and skips very old billing work.
  • Operators can force old-subscription resyncs without changing lookback filters.

No actionable issues were found in the changes since the previous review or the full-PR rule check.

Reviews (2) · Last reviewed commit: "refactor(billing): shorten terminal sync..." · Reviewed by Greptile

@turip
turip requested a review from a team as a code owner November 16, 2026 15:19
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The subscription sync service now records ended and too_old outcomes. The reconciler and all command can enable resynchronization beyond the 30-day age cutoff. The lookback still limits selection, and deletion cleanup takes precedence over the age cutoff.

Changes: Subscription synchronization

Layer / File(s) Summary
Record ended and skipped outcomes
openmeter/billing/worker/subscriptionsync/syncstatev2.go, openmeter/billing/worker/subscriptionsync/service.go, openmeter/billing/worker/subscriptionsync/service/sync.go, openmeter/billing/worker/subscriptionsync/reconciliaton_test.go, openmeter/billing/worker/subscriptionsync/service/sync_lock_test.go
The sync service adds ended and too_old outcomes. It records ended when the worker clock and sync horizon cover ActiveTo. It skips subscriptions whose ActiveTo is more than 30 days before the worker clock unless forced resync or deletion cleanup applies. Tests cover cutoff behavior, cleanup precedence, dry runs, revision capture, and failed result writes.
Expose forced old-subscription resync
openmeter/billing/worker/subscriptionsync/reconciler/reconciler.go, cmd/jobs/billing/subscriptionsync/sync.go, openmeter/billing/worker/subscriptionsync/reconciliaton_test.go, openmeter/billing/worker/subscriptionsync/README.md
The reconciler forwards the resync option when enabled. The all command adds --force-all; the existing -f still enables only Force. Documentation describes the lookback limit, cleanup exclusion, and recorded outcomes.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant ReconcilerAll
  participant ReconcileSubscription
  participant SyncByID
  participant SubscriptionSyncService
  participant RecordSyncResultV2
  ReconcilerAll->>ReconcileSubscription: pass subscription and sync options
  ReconcileSubscription->>SyncByID: pass subscription ID, options, and clock time
  SyncByID->>SubscriptionSyncService: synchronize subscription
  SubscriptionSyncService->>RecordSyncResultV2: record ended or too-old outcome
Loading

Possibly related PRs

  • openmeterio/openmeter#5516: Adds the deleted-subscription v2 sync state and completed-deletion query used by the cleanup behavior in this change.
  • openmeterio/openmeter#5538: Adds revision fields and under-lock revision acknowledgment used when recording skipped outcomes.
  • openmeterio/openmeter#5545: Adds the v2 result-write contract and revision conflict behavior used for ended and skipped outcomes.

Suggested reviewers: tothandras, galexihu, chrisgacsal

Merge Risk: Minimal · up to 64199

This change adds ended and skipped outcomes to subscription sync, along with an option to force resync of old subscriptions. No actionable merge-blocking risk was found, and the change appears ready to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: subscription sync now records ended and skipped outcomes. The raw summary shows new ended and too_old v2 states, the age-cutoff skip, and recording of these results. T…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: subscription sync now records ended and skipped outcomes. The raw summary shows new ended and too_old v2 states, the age-cutoff skip, and recording of these results. The title does not mention the new force-all flag, but it does not need to cover every detail. It uses the conventional feat(billing) prefix and is specific enough for a teammate scanning history.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 3c8c722 and 64199c6.

Files ignored due to path filters: 2

  • openmeter/ent/db/migrate/schema.go is excluded by !**/ent/db/**
  • openmeter/ent/db/subscriptionsyncstatev2/subscriptionsyncstatev2.go is excluded by !**/ent/db/**
Files selected for processing: 5

  • openmeter/billing/worker/subscriptionsync/README.md
  • openmeter/billing/worker/subscriptionsync/reconciliaton_test.go
  • openmeter/billing/worker/subscriptionsync/service/sync.go
  • openmeter/billing/worker/subscriptionsync/service/sync_lock_test.go
  • openmeter/billing/worker/subscriptionsync/syncstatev2.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@turip turip added the kind/feature New feature or request label Nov 16, 2026 — with ChatGPT Codex Connector
@turip turip added release-note/bug-fix Release note: Bug Fixes and removed kind/feature New feature or request labels Nov 16, 2026
@turip
turip enabled auto-merge (squash) November 16, 2026 16:13
@turip
turip merged commit f305c47 into main Nov 16, 2026
37 of 38 checks passed
@turip
turip deleted the feat/subscription-sync-terminal-states branch November 16, 2026 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants