Skip to content

Commit 13693c6

Browse files
committed
Keep seat reconciliation alive after the response
1 parent 3fd28a5 commit 13693c6

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

‎apps/cloud/src/extensions/billing/member-seats.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// ---------------------------------------------------------------------------
44

55
import { Effect } from "effect";
6+
import { waitUntil } from "cloudflare:workers";
67

78
import { WorkOSClient } from "../../auth/workos";
89
import { AutumnService } from "./service";
@@ -38,16 +39,16 @@ export const reportMemberSeats = (
3839
/**
3940
* Fork `reportMemberSeats` off the calling request, mirroring how execution
4041
* tracking is forked: billing must never stall or fail a user-facing
41-
* request. Only boot-scoped services are captured (WorkOS + Autumn — no
42-
* request-scoped resources), so the forked fiber cannot outlive anything it
43-
* depends on.
42+
* request. Cloudflare owns the promise through waitUntil, so the recount can
43+
* finish after the response. Only boot-scoped WorkOS and Autumn services are
44+
* captured.
4445
*/
4546
export const forkReportMemberSeats = (
4647
organizationId: string,
4748
): Effect.Effect<void, never, WorkOSClient | AutumnService> =>
4849
Effect.gen(function* () {
4950
const ctx = yield* Effect.context<WorkOSClient | AutumnService>();
5051
yield* Effect.sync(() => {
51-
Effect.runForkWith(ctx)(reportMemberSeats(organizationId));
52+
waitUntil(Effect.runPromiseWith(ctx)(reportMemberSeats(organizationId)));
5253
});
5354
});

‎apps/cloud/test-stubs/cloudflare-workers.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
// from `process.env` into this stub at import time. Without this bridge
88
// the test DbService would dial the default postgres port instead of the
99
// PGlite socket server started by `scripts/test-globalsetup.ts`.
10+
import { vi } from "vitest";
11+
12+
export const waitUntil = vi.fn<(work: Promise<unknown>) => void>();
13+
1014
export const env: Record<string, unknown> = {
1115
DATABASE_URL: process.env.DATABASE_URL,
1216
WORKOS_API_KEY: process.env.WORKOS_API_KEY,

0 commit comments

Comments
 (0)