Skip to content

Commit 8133de4

Browse files
d-csTrigger.dev RepoOps
authored andcommitted
feat(webapp,run-store): add the outflowEnabled flag and its decision, and define the outflow's mark and stream (unwired)
Mono-RevId: ac19e02f6dc6a0b184c980ae654c18d808e61a5f
1 parent 8e28774 commit 8133de4

7 files changed

Lines changed: 1356 additions & 10 deletions

File tree

‎apps/webapp/app/v3/featureFlags.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const FEATURE_FLAG = {
8585
computeOnly: "computeOnly",
8686
auditLogsRecording: "auditLogsRecording",
8787
hasAuditLogsAccess: "hasAuditLogsAccess",
88+
outflowEnabled: "outflowEnabled",
8889
} as const;
8990

9091
// Mirrors the core worker-queue enums; defined here because this module is imported client-side.
@@ -303,6 +304,10 @@ export const FeatureFlagCatalog = {
303304
// on the Roles page. Independent of recording. Org override wins over the global value; off by
304305
// default. Strict z.boolean(), as for recording: the string "false" must not show the log.
305306
[FEATURE_FLAG.hasAuditLogsAccess]: z.boolean(),
307+
// Whether the outflow moves this org's completed runs from MemoryDB to the archive. It moves them only
308+
// when the archive's and the read path's flags are on as well. Org override wins over the global value;
309+
// off by default. Strict z.boolean(): coercion reads the string "false" as true.
310+
[FEATURE_FLAG.outflowEnabled]: z.boolean(),
306311
};
307312

308313
export type FeatureFlagKey = keyof typeof FeatureFlagCatalog;

‎apps/webapp/app/v3/runOpsMigration/track1-baseline.json‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,14 @@
201201
},
202202
{
203203
"file": "apps/webapp/app/v3/services/cancelTaskRun.server.ts",
204-
"line": 47,
204+
"line": 52,
205205
"owner": "legacy-teardown",
206206
"reason": "cancel of a historical engine V1 run, on the legacy database",
207207
"findings": ["i/write TaskRun.taskRun"]
208208
},
209209
{
210210
"file": "apps/webapp/app/v3/services/cancelTaskRun.server.ts",
211-
"line": 56,
211+
"line": 61,
212212
"owner": "legacy-teardown",
213213
"reason": "cancel of a historical engine V1 run, on the legacy database",
214214
"findings": ["i/write TaskRun.taskRun"]
@@ -222,14 +222,14 @@
222222
},
223223
{
224224
"file": "internal-packages/run-engine/src/engine/systems/executionSnapshotSystem.ts",
225-
"line": 148,
225+
"line": 149,
226226
"owner": "legacy-teardown",
227227
"reason": "fallback when no run store is passed; only tests reach it",
228228
"findings": ["iv/read _completedWaitpoints.$queryRaw"]
229229
},
230230
{
231231
"file": "internal-packages/run-engine/src/engine/systems/executionSnapshotSystem.ts",
232-
"line": 173,
232+
"line": 174,
233233
"owner": "legacy-teardown",
234234
"reason": "fallback when no run store is passed; only tests reach it",
235235
"findings": [
@@ -239,28 +239,28 @@
239239
},
240240
{
241241
"file": "internal-packages/run-engine/src/engine/systems/executionSnapshotSystem.ts",
242-
"line": 198,
242+
"line": 199,
243243
"owner": "legacy-teardown",
244244
"reason": "fallback when no run store is passed; only tests reach it",
245245
"findings": ["i/read Waitpoint.waitpoint"]
246246
},
247247
{
248248
"file": "internal-packages/run-engine/src/engine/systems/executionSnapshotSystem.ts",
249-
"line": 282,
249+
"line": 283,
250250
"owner": "legacy-teardown",
251251
"reason": "fallback when no run store is passed; only tests reach it",
252252
"findings": ["i/read TaskRunExecutionSnapshot.taskRunExecutionSnapshot"]
253253
},
254254
{
255255
"file": "internal-packages/run-engine/src/engine/systems/executionSnapshotSystem.ts",
256-
"line": 378,
256+
"line": 379,
257257
"owner": "legacy-teardown",
258258
"reason": "fallback when no run store is passed; only tests reach it",
259259
"findings": ["i/read TaskRunExecutionSnapshot.taskRunExecutionSnapshot"]
260260
},
261261
{
262262
"file": "internal-packages/run-engine/src/engine/systems/executionSnapshotSystem.ts",
263-
"line": 399,
263+
"line": 400,
264264
"owner": "legacy-teardown",
265265
"reason": "fallback when no run store is passed; only tests reach it",
266266
"findings": ["i/read TaskRunExecutionSnapshot.taskRunExecutionSnapshot"]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import type { PrismaClient } from "@trigger.dev/database";
2+
import { logger } from "~/services/logger.server";
3+
import { BoundedTtlCache } from "~/services/realtime/boundedTtlCache";
4+
import { FEATURE_FLAG, FeatureFlagCatalog } from "../featureFlags";
5+
6+
// The per-organization flag cache of the snapshot store rollout: its default lifetime and its cap.
7+
const FLAGS_CACHE_TTL_MS = 5_000;
8+
const FLAGS_CACHE_MAX_ENTRIES = 10_000;
9+
// The audit recording flag's default lookup timeout.
10+
const LOOKUP_TIMEOUT_MS = 5_000;
11+
12+
const LOOKUP_FAILED = Symbol("lookupFailed");
13+
14+
type ParentDecision = (
15+
organizationId: string,
16+
organizationFlags: unknown
17+
) => boolean | Promise<boolean>;
18+
19+
/**
20+
* Whether the outflow may move an organization's runs: `outflowEnabled` (organization override, then
21+
* the global value, then off) and both parent decisions. One cached lookup serves all three; never rejects.
22+
* A lookup that takes longer than `lookupTimeoutMs` (default 5 seconds) counts as a failed one.
23+
*/
24+
export function createOutflowDecision(deps: {
25+
readGlobalFlags: () => { outflowEnabled?: boolean } | undefined;
26+
prisma: Pick<PrismaClient, "organization">;
27+
archiveStoreEnabled: ParentDecision;
28+
archiveReadPathEnabled: ParentDecision;
29+
lookupTimeoutMs?: number;
30+
}): (organizationId: string) => Promise<boolean> {
31+
const lookupTimeoutMs = deps.lookupTimeoutMs ?? LOOKUP_TIMEOUT_MS;
32+
// Holds the lookup's promise from when it starts, so concurrent calls share it.
33+
const cache = new BoundedTtlCache<Promise<unknown>>(FLAGS_CACHE_TTL_MS, FLAGS_CACHE_MAX_ENTRIES);
34+
35+
async function lookUp(organizationId: string): Promise<unknown> {
36+
// A silent connection can hold the query far past any driver timeout. The query keeps
37+
// running; only the decision stops waiting for it.
38+
let timer: NodeJS.Timeout | undefined;
39+
const timedOut = new Promise<never>((_, reject) => {
40+
timer = setTimeout(
41+
() => reject(new Error(`timed out after ${lookupTimeoutMs} ms`)),
42+
lookupTimeoutMs
43+
);
44+
});
45+
try {
46+
const row = await Promise.race([
47+
deps.prisma.organization.findFirst({
48+
where: { id: organizationId },
49+
select: { featureFlags: true },
50+
}),
51+
timedOut,
52+
]);
53+
return row?.featureFlags ?? null;
54+
} catch (error) {
55+
// Cached like an answer, so an outage costs one read and one line per org per lifetime.
56+
logger.error("Outflow flag decision: failed to read the organization's flags, not moving", {
57+
organizationId,
58+
error: error instanceof Error ? error.message : String(error),
59+
});
60+
return LOOKUP_FAILED;
61+
} finally {
62+
clearTimeout(timer);
63+
}
64+
}
65+
66+
function organizationFlags(organizationId: string): Promise<unknown> {
67+
const cached = cache.get(organizationId);
68+
if (cached !== undefined) return cached;
69+
70+
const flags = lookUp(organizationId);
71+
cache.set(organizationId, flags);
72+
void flags.then((result) => {
73+
if (result !== LOOKUP_FAILED) return;
74+
// A failure's lifetime starts when it settles, else a timeout as long as the lifetime is never
75+
// served. A newer lookup that has replaced this entry keeps its place. An entry that is gone
76+
// comes back only into a cache with room: one evicted at capacity must not evict another
77+
// organization's live entry. Reading an expired entry deletes it, which makes that room.
78+
const current = cache.get(organizationId);
79+
if (current === flags || (current === undefined && cache.size < FLAGS_CACHE_MAX_ENTRIES)) {
80+
cache.set(organizationId, flags);
81+
}
82+
});
83+
return flags;
84+
}
85+
86+
return async (organizationId) => {
87+
const flags = await organizationFlags(organizationId);
88+
if (flags === LOOKUP_FAILED) return false;
89+
90+
try {
91+
const value =
92+
typeof flags === "object" && flags !== null && "outflowEnabled" in flags
93+
? flags.outflowEnabled
94+
: undefined;
95+
const parsed = FeatureFlagCatalog[FEATURE_FLAG.outflowEnabled].safeParse(value);
96+
const enabled = parsed.success
97+
? parsed.data
98+
: (deps.readGlobalFlags()?.outflowEnabled ?? false);
99+
if (!enabled) return false;
100+
101+
if (!(await deps.archiveStoreEnabled(organizationId, flags))) return false;
102+
return await deps.archiveReadPathEnabled(organizationId, flags);
103+
} catch (error) {
104+
logger.error("Outflow flag decision failed, not moving", {
105+
organizationId,
106+
error: error instanceof Error ? error.message : String(error),
107+
});
108+
return false;
109+
}
110+
};
111+
}

0 commit comments

Comments
 (0)