@dudousxd/nestjs-durable-store-prisma 0.25.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +474 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +22 -1
- package/dist/index.d.ts +22 -1
- package/dist/index.js +475 -3
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/prisma/schema.prisma +13 -0
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { StateStore, WorkflowRun, RunStatus, RetentionPolicy,
|
|
1
|
+
import { StateStore, StepResultSettlement, StepCheckpoint, WorkflowRun, RunStatus, RetentionPolicy, StepSettlement, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow as BufferedSignalRow$1 } from '@dudousxd/nestjs-durable-core';
|
|
2
2
|
|
|
3
3
|
interface RunRow {
|
|
4
4
|
id: string;
|
|
@@ -87,6 +87,11 @@ interface Delegate<Row> {
|
|
|
87
87
|
* not model rows, so the result type is stated at the call site rather than derived from `Row`. */
|
|
88
88
|
groupBy(args: Args): Promise<Record<string, unknown>[]>;
|
|
89
89
|
}
|
|
90
|
+
interface SignalIdRow {
|
|
91
|
+
token: string;
|
|
92
|
+
signalId: string;
|
|
93
|
+
expiresAt: bigint;
|
|
94
|
+
}
|
|
90
95
|
interface ScheduleRow {
|
|
91
96
|
id: string;
|
|
92
97
|
namespace: string;
|
|
@@ -108,6 +113,8 @@ interface DurablePrismaTx {
|
|
|
108
113
|
durableBufferedEvent: Delegate<BufferedEventRow>;
|
|
109
114
|
/** Only needed for persisted schedules (`engine.schedules`) — add the `DurableSchedule` model. */
|
|
110
115
|
durableSchedule?: Delegate<ScheduleRow>;
|
|
116
|
+
/** Only needed for signal dedupe (`signal(..., { id })`) — add the `DurableSignalId` model. */
|
|
117
|
+
durableSignalId?: Delegate<SignalIdRow>;
|
|
111
118
|
}
|
|
112
119
|
interface DurablePrismaClient extends DurablePrismaTx {
|
|
113
120
|
/** Prisma's interactive-transaction form: runs `fn` with a tx-scoped client and commits on resolve. */
|
|
@@ -125,6 +132,12 @@ interface DurablePrismaClient extends DurablePrismaTx {
|
|
|
125
132
|
*/
|
|
126
133
|
declare class PrismaStateStore implements StateStore {
|
|
127
134
|
private readonly db;
|
|
135
|
+
readonly settleResultsIfPending?: (results: StepResultSettlement[]) => Promise<(StepCheckpoint | null)[]>;
|
|
136
|
+
readonly insertCheckpointIfAbsent?: (checkpoint: StepCheckpoint) => Promise<boolean>;
|
|
137
|
+
readonly tryLockRunAndGet?: (runId: string, owner: string, leaseUntilMs: number, nowMs: number) => Promise<WorkflowRun | null>;
|
|
138
|
+
readonly claimPendingRuns?: (limit: number, owner: string, leaseUntilMs: number, nowMs: number, namespace?: string) => Promise<WorkflowRun[]>;
|
|
139
|
+
/** Raw single-statement writes on Postgres/MySQL (Prisma wraps `updateMany` in a transaction). */
|
|
140
|
+
private readonly fast?;
|
|
128
141
|
constructor(db: DurablePrismaClient);
|
|
129
142
|
tryAdmitSingleton(runId: string, tag: string, workflow: string, limit: number, retryWakeAt?: number): Promise<boolean>;
|
|
130
143
|
createRun(run: WorkflowRun): Promise<void>;
|
|
@@ -214,6 +227,14 @@ declare class PrismaStateStore implements StateStore {
|
|
|
214
227
|
takeBufferedSignal(token: string): Promise<{
|
|
215
228
|
payload: unknown;
|
|
216
229
|
} | null>;
|
|
230
|
+
takeOldestBufferedSignal(tokens: string[]): Promise<{
|
|
231
|
+
token: string;
|
|
232
|
+
payload: unknown;
|
|
233
|
+
} | null>;
|
|
234
|
+
private signalIds;
|
|
235
|
+
claimSignalId(token: string, id: string, expiresAt: number, nowMs: number): Promise<boolean>;
|
|
236
|
+
releaseSignalId(token: string, id: string): Promise<void>;
|
|
237
|
+
deleteExpiredSignalIds(nowMs: number, limit: number): Promise<number>;
|
|
217
238
|
listBufferedSignals(tokenPrefix: string, afterId: string | undefined, limit: number): Promise<BufferedSignalRow$1[]>;
|
|
218
239
|
deleteBufferedSignals(ids: string[]): Promise<void>;
|
|
219
240
|
bufferEvent(input: {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { StateStore, WorkflowRun, RunStatus, RetentionPolicy,
|
|
1
|
+
import { StateStore, StepResultSettlement, StepCheckpoint, WorkflowRun, RunStatus, RetentionPolicy, StepSettlement, OwnedLeaseReclaim, RunQuery, RunFacetQuery, RunFacetRow, RunValueAxis, RunValueFacetOptions, RunValueFacetRow, ScheduleRecord, ScheduleQuery, CheckpointStat, CheckpointQuery, SignalWaiter, ChildWait, BufferedSignalRow as BufferedSignalRow$1 } from '@dudousxd/nestjs-durable-core';
|
|
2
2
|
|
|
3
3
|
interface RunRow {
|
|
4
4
|
id: string;
|
|
@@ -87,6 +87,11 @@ interface Delegate<Row> {
|
|
|
87
87
|
* not model rows, so the result type is stated at the call site rather than derived from `Row`. */
|
|
88
88
|
groupBy(args: Args): Promise<Record<string, unknown>[]>;
|
|
89
89
|
}
|
|
90
|
+
interface SignalIdRow {
|
|
91
|
+
token: string;
|
|
92
|
+
signalId: string;
|
|
93
|
+
expiresAt: bigint;
|
|
94
|
+
}
|
|
90
95
|
interface ScheduleRow {
|
|
91
96
|
id: string;
|
|
92
97
|
namespace: string;
|
|
@@ -108,6 +113,8 @@ interface DurablePrismaTx {
|
|
|
108
113
|
durableBufferedEvent: Delegate<BufferedEventRow>;
|
|
109
114
|
/** Only needed for persisted schedules (`engine.schedules`) — add the `DurableSchedule` model. */
|
|
110
115
|
durableSchedule?: Delegate<ScheduleRow>;
|
|
116
|
+
/** Only needed for signal dedupe (`signal(..., { id })`) — add the `DurableSignalId` model. */
|
|
117
|
+
durableSignalId?: Delegate<SignalIdRow>;
|
|
111
118
|
}
|
|
112
119
|
interface DurablePrismaClient extends DurablePrismaTx {
|
|
113
120
|
/** Prisma's interactive-transaction form: runs `fn` with a tx-scoped client and commits on resolve. */
|
|
@@ -125,6 +132,12 @@ interface DurablePrismaClient extends DurablePrismaTx {
|
|
|
125
132
|
*/
|
|
126
133
|
declare class PrismaStateStore implements StateStore {
|
|
127
134
|
private readonly db;
|
|
135
|
+
readonly settleResultsIfPending?: (results: StepResultSettlement[]) => Promise<(StepCheckpoint | null)[]>;
|
|
136
|
+
readonly insertCheckpointIfAbsent?: (checkpoint: StepCheckpoint) => Promise<boolean>;
|
|
137
|
+
readonly tryLockRunAndGet?: (runId: string, owner: string, leaseUntilMs: number, nowMs: number) => Promise<WorkflowRun | null>;
|
|
138
|
+
readonly claimPendingRuns?: (limit: number, owner: string, leaseUntilMs: number, nowMs: number, namespace?: string) => Promise<WorkflowRun[]>;
|
|
139
|
+
/** Raw single-statement writes on Postgres/MySQL (Prisma wraps `updateMany` in a transaction). */
|
|
140
|
+
private readonly fast?;
|
|
128
141
|
constructor(db: DurablePrismaClient);
|
|
129
142
|
tryAdmitSingleton(runId: string, tag: string, workflow: string, limit: number, retryWakeAt?: number): Promise<boolean>;
|
|
130
143
|
createRun(run: WorkflowRun): Promise<void>;
|
|
@@ -214,6 +227,14 @@ declare class PrismaStateStore implements StateStore {
|
|
|
214
227
|
takeBufferedSignal(token: string): Promise<{
|
|
215
228
|
payload: unknown;
|
|
216
229
|
} | null>;
|
|
230
|
+
takeOldestBufferedSignal(tokens: string[]): Promise<{
|
|
231
|
+
token: string;
|
|
232
|
+
payload: unknown;
|
|
233
|
+
} | null>;
|
|
234
|
+
private signalIds;
|
|
235
|
+
claimSignalId(token: string, id: string, expiresAt: number, nowMs: number): Promise<boolean>;
|
|
236
|
+
releaseSignalId(token: string, id: string): Promise<void>;
|
|
237
|
+
deleteExpiredSignalIds(nowMs: number, limit: number): Promise<number>;
|
|
217
238
|
listBufferedSignals(tokenPrefix: string, afterId: string | undefined, limit: number): Promise<BufferedSignalRow$1[]>;
|
|
218
239
|
deleteBufferedSignals(ids: string[]): Promise<void>;
|
|
219
240
|
bufferEvent(input: {
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
4
|
// src/prisma-state-store.ts
|
|
5
|
-
import { LIFECYCLE_REGISTRY_WORKFLOW, RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, SINGLETON_LOCK_CHUNK, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, retentionKeeps, runValueFacetsFromRuns, singletonAdmissionDenialPatch, singletonAdmissionTags, singletonClaimantIds } from "@dudousxd/nestjs-durable-core";
|
|
5
|
+
import { LIFECYCLE_REGISTRY_WORKFLOW, RUN_VALUE_FACET_SCAN, SINGLETON_ACTIVE_STATUSES, SINGLETON_LOCK_CHUNK, TERMINAL_RUN_STATUSES, attributePredicateOperands, axisIsRunColumn, clearTerminalSingletonAdmission, mergeRunFacetRows, mergeRunValueFacetRows, normalizeAttributeRows, parseDuration, retentionKeeps, runValueFacetsFromRuns, settleResultsWithPlainCalls, singletonAdmissionDenialPatch, singletonAdmissionTags, singletonClaimantIds } from "@dudousxd/nestjs-durable-core";
|
|
6
6
|
var RETENTION_EXCLUDED = [
|
|
7
7
|
LIFECYCLE_REGISTRY_WORKFLOW
|
|
8
8
|
];
|
|
@@ -18,8 +18,28 @@ var PrismaStateStore = class {
|
|
|
18
18
|
__name(this, "PrismaStateStore");
|
|
19
19
|
}
|
|
20
20
|
db;
|
|
21
|
+
// The round-trip-saving optional methods, bound on Postgres and MySQL (see `PrismaRoundTrips`);
|
|
22
|
+
// absent on SQLite, where the engine's read-then-write calls are used.
|
|
23
|
+
settleResultsIfPending;
|
|
24
|
+
insertCheckpointIfAbsent;
|
|
25
|
+
tryLockRunAndGet;
|
|
26
|
+
claimPendingRuns;
|
|
27
|
+
/** Raw single-statement writes on Postgres/MySQL (Prisma wraps `updateMany` in a transaction). */
|
|
28
|
+
fast;
|
|
21
29
|
constructor(db) {
|
|
22
30
|
this.db = db;
|
|
31
|
+
const provider = db._activeProvider;
|
|
32
|
+
const raw = typeof db.$queryRawUnsafe === "function";
|
|
33
|
+
const dialect = raw && provider === "postgresql" ? "postgres" : raw && provider === "mysql" ? "mysql" : void 0;
|
|
34
|
+
if (!dialect) return;
|
|
35
|
+
const fast = new PrismaRoundTrips(db, dialect, this);
|
|
36
|
+
this.fast = fast;
|
|
37
|
+
this.insertCheckpointIfAbsent = (cp) => fast.insertCheckpointIfAbsent(cp);
|
|
38
|
+
this.settleResultsIfPending = (results) => fast.settleResultsIfPending(results);
|
|
39
|
+
this.claimPendingRuns = (limit, owner, until, now, ns) => fast.claimPendingRuns(limit, owner, until, now, ns);
|
|
40
|
+
if (dialect === "postgres") {
|
|
41
|
+
this.tryLockRunAndGet = (runId, owner, until, now) => fast.tryLockRunAndGet(runId, owner, until, now);
|
|
42
|
+
}
|
|
23
43
|
}
|
|
24
44
|
async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
|
|
25
45
|
const where = {
|
|
@@ -117,7 +137,8 @@ var PrismaStateStore = class {
|
|
|
117
137
|
}
|
|
118
138
|
};
|
|
119
139
|
const data = toRunPatch(patch);
|
|
120
|
-
const
|
|
140
|
+
const raw = Object.keys(data).length ? this.fast?.updateRunWhere(runId, expectedStatuses, patch) : void 0;
|
|
141
|
+
const count = raw ? await raw : Object.keys(data).length ? (await this.db.durableWorkflowRun.updateMany({
|
|
121
142
|
where,
|
|
122
143
|
data
|
|
123
144
|
})).count : await this.db.durableWorkflowRun.count({
|
|
@@ -412,7 +433,7 @@ var PrismaStateStore = class {
|
|
|
412
433
|
seen.add(key);
|
|
413
434
|
firsts.push(i);
|
|
414
435
|
});
|
|
415
|
-
const dialect = rawSettleDialect(this.db);
|
|
436
|
+
const dialect = firsts.length === 1 ? void 0 : rawSettleDialect(this.db);
|
|
416
437
|
if (!dialect) {
|
|
417
438
|
const settle = /* @__PURE__ */ __name(async (db) => {
|
|
418
439
|
for (const i of firsts) {
|
|
@@ -581,6 +602,10 @@ var PrismaStateStore = class {
|
|
|
581
602
|
return result.count === 1;
|
|
582
603
|
}
|
|
583
604
|
async releaseRunLock(runId, owner) {
|
|
605
|
+
if (this.fast) {
|
|
606
|
+
await this.fast.releaseRunLock(runId, owner);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
584
609
|
await this.db.durableWorkflowRun.updateMany({
|
|
585
610
|
where: owner === void 0 ? {
|
|
586
611
|
id: runId
|
|
@@ -1235,6 +1260,112 @@ var PrismaStateStore = class {
|
|
|
1235
1260
|
payload: row.payload ?? void 0
|
|
1236
1261
|
};
|
|
1237
1262
|
}
|
|
1263
|
+
async takeOldestBufferedSignal(tokens) {
|
|
1264
|
+
if (tokens.length === 0) return null;
|
|
1265
|
+
const unique = [
|
|
1266
|
+
...new Set(tokens)
|
|
1267
|
+
];
|
|
1268
|
+
for (; ; ) {
|
|
1269
|
+
const row = await this.db.durableBufferedSignal.findFirst({
|
|
1270
|
+
where: {
|
|
1271
|
+
token: {
|
|
1272
|
+
in: unique
|
|
1273
|
+
}
|
|
1274
|
+
},
|
|
1275
|
+
orderBy: {
|
|
1276
|
+
id: "asc"
|
|
1277
|
+
}
|
|
1278
|
+
});
|
|
1279
|
+
if (!row) return null;
|
|
1280
|
+
const { count } = await this.db.durableBufferedSignal.deleteMany({
|
|
1281
|
+
where: {
|
|
1282
|
+
id: row.id
|
|
1283
|
+
}
|
|
1284
|
+
});
|
|
1285
|
+
if (count === 1) return {
|
|
1286
|
+
token: row.token,
|
|
1287
|
+
payload: row.payload ?? void 0
|
|
1288
|
+
};
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
signalIds() {
|
|
1292
|
+
const delegate = this.db.durableSignalId;
|
|
1293
|
+
if (!delegate) {
|
|
1294
|
+
throw new Error("signal dedupe (`signal(..., { id })`) needs the DurableSignalId model \u2014 copy it from prisma/schema.prisma into your schema and re-run `prisma generate`");
|
|
1295
|
+
}
|
|
1296
|
+
return delegate;
|
|
1297
|
+
}
|
|
1298
|
+
async claimSignalId(token, id, expiresAt, nowMs) {
|
|
1299
|
+
const ids = this.signalIds();
|
|
1300
|
+
for (; ; ) {
|
|
1301
|
+
try {
|
|
1302
|
+
await ids.create({
|
|
1303
|
+
data: {
|
|
1304
|
+
token,
|
|
1305
|
+
signalId: id,
|
|
1306
|
+
expiresAt: BigInt(expiresAt)
|
|
1307
|
+
}
|
|
1308
|
+
});
|
|
1309
|
+
return true;
|
|
1310
|
+
} catch (err) {
|
|
1311
|
+
if (err?.code !== "P2002") throw err;
|
|
1312
|
+
}
|
|
1313
|
+
const { count } = await ids.updateMany({
|
|
1314
|
+
where: {
|
|
1315
|
+
token,
|
|
1316
|
+
signalId: id,
|
|
1317
|
+
expiresAt: {
|
|
1318
|
+
lte: BigInt(nowMs)
|
|
1319
|
+
}
|
|
1320
|
+
},
|
|
1321
|
+
data: {
|
|
1322
|
+
expiresAt: BigInt(expiresAt)
|
|
1323
|
+
}
|
|
1324
|
+
});
|
|
1325
|
+
if (count === 1) return true;
|
|
1326
|
+
if (await ids.count({
|
|
1327
|
+
where: {
|
|
1328
|
+
token,
|
|
1329
|
+
signalId: id
|
|
1330
|
+
}
|
|
1331
|
+
}) > 0) return false;
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
async releaseSignalId(token, id) {
|
|
1335
|
+
await this.signalIds().deleteMany({
|
|
1336
|
+
where: {
|
|
1337
|
+
token,
|
|
1338
|
+
signalId: id
|
|
1339
|
+
}
|
|
1340
|
+
});
|
|
1341
|
+
}
|
|
1342
|
+
async deleteExpiredSignalIds(nowMs, limit) {
|
|
1343
|
+
const ids = this.signalIds();
|
|
1344
|
+
const expired = {
|
|
1345
|
+
lte: BigInt(nowMs)
|
|
1346
|
+
};
|
|
1347
|
+
const rows = await ids.findMany({
|
|
1348
|
+
where: {
|
|
1349
|
+
expiresAt: expired
|
|
1350
|
+
},
|
|
1351
|
+
select: {
|
|
1352
|
+
token: true,
|
|
1353
|
+
signalId: true
|
|
1354
|
+
},
|
|
1355
|
+
take: limit
|
|
1356
|
+
});
|
|
1357
|
+
if (rows.length === 0) return 0;
|
|
1358
|
+
const { count } = await ids.deleteMany({
|
|
1359
|
+
where: {
|
|
1360
|
+
expiresAt: expired,
|
|
1361
|
+
OR: rows.map((r) => ({
|
|
1362
|
+
token: r.token,
|
|
1363
|
+
signalId: r.signalId
|
|
1364
|
+
}))
|
|
1365
|
+
}
|
|
1366
|
+
});
|
|
1367
|
+
return count;
|
|
1368
|
+
}
|
|
1238
1369
|
async listBufferedSignals(tokenPrefix, afterId, limit) {
|
|
1239
1370
|
const rows = await this.db.durableBufferedSignal.findMany({
|
|
1240
1371
|
where: {
|
|
@@ -1716,6 +1847,346 @@ function fromScheduleRow(row) {
|
|
|
1716
1847
|
};
|
|
1717
1848
|
}
|
|
1718
1849
|
__name(fromScheduleRow, "fromScheduleRow");
|
|
1850
|
+
function fromRawRow(raw) {
|
|
1851
|
+
const row = {};
|
|
1852
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
1853
|
+
row[key.replace(/_([a-z])/g, (_, c) => c.toUpperCase())] = value;
|
|
1854
|
+
}
|
|
1855
|
+
return row;
|
|
1856
|
+
}
|
|
1857
|
+
__name(fromRawRow, "fromRawRow");
|
|
1858
|
+
var SETTLED_CHECKPOINT_COLUMNS = "run_id, seq, name, kind, step_id, status, output, error, events, attempts, worker_group, parallel_group, wake_at, enqueued_at, started_at, finished_at";
|
|
1859
|
+
var PrismaRoundTrips = class PrismaRoundTrips2 {
|
|
1860
|
+
static {
|
|
1861
|
+
__name(this, "PrismaRoundTrips");
|
|
1862
|
+
}
|
|
1863
|
+
db;
|
|
1864
|
+
store;
|
|
1865
|
+
pg;
|
|
1866
|
+
constructor(db, dialect, store) {
|
|
1867
|
+
this.db = db;
|
|
1868
|
+
this.store = store;
|
|
1869
|
+
this.pg = dialect === "postgres";
|
|
1870
|
+
}
|
|
1871
|
+
get raw() {
|
|
1872
|
+
return this.db;
|
|
1873
|
+
}
|
|
1874
|
+
async tryLockRunAndGet(runId, owner, leaseUntilMs, nowMs) {
|
|
1875
|
+
const rows = await this.raw.$queryRawUnsafe(`UPDATE "durable_workflow_runs" SET locked_by = $1, locked_until = ($2::timestamptz AT TIME ZONE 'UTC') WHERE id = $3 AND (locked_until IS NULL OR locked_until <= ($4::timestamptz AT TIME ZONE 'UTC')) RETURNING *`, owner, new Date(leaseUntilMs).toISOString(), runId, new Date(nowMs).toISOString());
|
|
1876
|
+
const [row] = rows;
|
|
1877
|
+
return row ? fromRunRow(fromRawRow(row)) : null;
|
|
1878
|
+
}
|
|
1879
|
+
/** A parameter placeholder for `kind` at position `n` (1-based), in this dialect. */
|
|
1880
|
+
param(kind, n) {
|
|
1881
|
+
if (this.pg) {
|
|
1882
|
+
const p = `$${n}`;
|
|
1883
|
+
if (kind === "json") return `${p}::jsonb`;
|
|
1884
|
+
if (kind === "date") return `(${p}::timestamptz AT TIME ZONE 'UTC')`;
|
|
1885
|
+
if (kind === "bigint") return `${p}::bigint`;
|
|
1886
|
+
if (kind === "int") return `${p}::integer`;
|
|
1887
|
+
return p;
|
|
1888
|
+
}
|
|
1889
|
+
return kind === "json" ? "CAST(? AS JSON)" : "?";
|
|
1890
|
+
}
|
|
1891
|
+
/** A value of `kind` as the raw statement binds it (dates as UTC, JSON as text). */
|
|
1892
|
+
value(kind, v) {
|
|
1893
|
+
if (v == null) return null;
|
|
1894
|
+
if (kind === "json") return JSON.stringify(v);
|
|
1895
|
+
if (kind === "date") {
|
|
1896
|
+
const iso = (v instanceof Date ? v : new Date(v)).toISOString();
|
|
1897
|
+
return this.pg ? iso : iso.replace("T", " ").replace("Z", "");
|
|
1898
|
+
}
|
|
1899
|
+
return v;
|
|
1900
|
+
}
|
|
1901
|
+
q(name) {
|
|
1902
|
+
return this.pg ? `"${name}"` : `\`${name}\``;
|
|
1903
|
+
}
|
|
1904
|
+
async insertCheckpointIfAbsent(checkpoint) {
|
|
1905
|
+
const columns = [
|
|
1906
|
+
[
|
|
1907
|
+
"run_id",
|
|
1908
|
+
"text",
|
|
1909
|
+
checkpoint.runId
|
|
1910
|
+
],
|
|
1911
|
+
[
|
|
1912
|
+
"seq",
|
|
1913
|
+
"int",
|
|
1914
|
+
checkpoint.seq
|
|
1915
|
+
],
|
|
1916
|
+
[
|
|
1917
|
+
"name",
|
|
1918
|
+
"text",
|
|
1919
|
+
checkpoint.name
|
|
1920
|
+
],
|
|
1921
|
+
[
|
|
1922
|
+
"kind",
|
|
1923
|
+
"text",
|
|
1924
|
+
checkpoint.kind
|
|
1925
|
+
],
|
|
1926
|
+
[
|
|
1927
|
+
"step_id",
|
|
1928
|
+
"text",
|
|
1929
|
+
checkpoint.stepId
|
|
1930
|
+
],
|
|
1931
|
+
[
|
|
1932
|
+
"status",
|
|
1933
|
+
"text",
|
|
1934
|
+
checkpoint.status
|
|
1935
|
+
],
|
|
1936
|
+
[
|
|
1937
|
+
"input",
|
|
1938
|
+
"json",
|
|
1939
|
+
checkpoint.input
|
|
1940
|
+
],
|
|
1941
|
+
[
|
|
1942
|
+
"output",
|
|
1943
|
+
"json",
|
|
1944
|
+
checkpoint.output
|
|
1945
|
+
],
|
|
1946
|
+
[
|
|
1947
|
+
"error",
|
|
1948
|
+
"json",
|
|
1949
|
+
checkpoint.error
|
|
1950
|
+
],
|
|
1951
|
+
[
|
|
1952
|
+
"events",
|
|
1953
|
+
"json",
|
|
1954
|
+
checkpoint.events
|
|
1955
|
+
],
|
|
1956
|
+
[
|
|
1957
|
+
"attempts",
|
|
1958
|
+
"int",
|
|
1959
|
+
checkpoint.attempts
|
|
1960
|
+
],
|
|
1961
|
+
[
|
|
1962
|
+
"worker_group",
|
|
1963
|
+
"text",
|
|
1964
|
+
checkpoint.workerGroup
|
|
1965
|
+
],
|
|
1966
|
+
[
|
|
1967
|
+
"parallel_group",
|
|
1968
|
+
"text",
|
|
1969
|
+
checkpoint.parallelGroup
|
|
1970
|
+
],
|
|
1971
|
+
[
|
|
1972
|
+
"wake_at",
|
|
1973
|
+
"bigint",
|
|
1974
|
+
checkpoint.wakeAt
|
|
1975
|
+
],
|
|
1976
|
+
[
|
|
1977
|
+
"enqueued_at",
|
|
1978
|
+
"date",
|
|
1979
|
+
checkpoint.enqueuedAt ?? checkpoint.startedAt
|
|
1980
|
+
],
|
|
1981
|
+
[
|
|
1982
|
+
"started_at",
|
|
1983
|
+
"date",
|
|
1984
|
+
checkpoint.startedAt
|
|
1985
|
+
],
|
|
1986
|
+
[
|
|
1987
|
+
"finished_at",
|
|
1988
|
+
"date",
|
|
1989
|
+
checkpoint.finishedAt
|
|
1990
|
+
]
|
|
1991
|
+
];
|
|
1992
|
+
const names = columns.map(([name]) => this.q(name)).join(", ");
|
|
1993
|
+
const marks = columns.map(([, kind], i) => this.param(kind, i + 1)).join(", ");
|
|
1994
|
+
const values = columns.map(([, kind, v]) => this.value(kind, v));
|
|
1995
|
+
const insert = `INSERT INTO ${this.q("durable_step_checkpoints")} (${names}) VALUES (${marks})`;
|
|
1996
|
+
if (this.pg) {
|
|
1997
|
+
const rows = await this.raw.$queryRawUnsafe(`${insert} ON CONFLICT (run_id, seq) DO NOTHING RETURNING seq`, ...values);
|
|
1998
|
+
return rows.length === 1;
|
|
1999
|
+
}
|
|
2000
|
+
try {
|
|
2001
|
+
await this.raw.$executeRawUnsafe(insert, ...values);
|
|
2002
|
+
return true;
|
|
2003
|
+
} catch (err) {
|
|
2004
|
+
const code = String(err.meta?.code ?? "");
|
|
2005
|
+
if (code === "1062" || /Duplicate entry/i.test(String(err.message))) return false;
|
|
2006
|
+
throw err;
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
/** The run columns a raw patch may write, with their kinds; any other goes through `updateMany`. */
|
|
2010
|
+
static RUN_COLUMNS = {
|
|
2011
|
+
workflow: [
|
|
2012
|
+
"workflow",
|
|
2013
|
+
"text"
|
|
2014
|
+
],
|
|
2015
|
+
workflowVersion: [
|
|
2016
|
+
"workflow_version",
|
|
2017
|
+
"text"
|
|
2018
|
+
],
|
|
2019
|
+
status: [
|
|
2020
|
+
"status",
|
|
2021
|
+
"text"
|
|
2022
|
+
],
|
|
2023
|
+
input: [
|
|
2024
|
+
"input",
|
|
2025
|
+
"json"
|
|
2026
|
+
],
|
|
2027
|
+
output: [
|
|
2028
|
+
"output",
|
|
2029
|
+
"json"
|
|
2030
|
+
],
|
|
2031
|
+
error: [
|
|
2032
|
+
"error",
|
|
2033
|
+
"json"
|
|
2034
|
+
],
|
|
2035
|
+
wakeAt: [
|
|
2036
|
+
"wake_at",
|
|
2037
|
+
"bigint"
|
|
2038
|
+
],
|
|
2039
|
+
lockedBy: [
|
|
2040
|
+
"locked_by",
|
|
2041
|
+
"text"
|
|
2042
|
+
],
|
|
2043
|
+
lockedUntil: [
|
|
2044
|
+
"locked_until",
|
|
2045
|
+
"date"
|
|
2046
|
+
],
|
|
2047
|
+
awaitingDecisionTaskId: [
|
|
2048
|
+
"awaiting_decision_task_id",
|
|
2049
|
+
"text"
|
|
2050
|
+
],
|
|
2051
|
+
recoveryAttempts: [
|
|
2052
|
+
"recovery_attempts",
|
|
2053
|
+
"int"
|
|
2054
|
+
],
|
|
2055
|
+
tags: [
|
|
2056
|
+
"tags",
|
|
2057
|
+
"json"
|
|
2058
|
+
],
|
|
2059
|
+
priority: [
|
|
2060
|
+
"priority",
|
|
2061
|
+
"int"
|
|
2062
|
+
],
|
|
2063
|
+
origin: [
|
|
2064
|
+
"origin",
|
|
2065
|
+
"text"
|
|
2066
|
+
],
|
|
2067
|
+
updatedAt: [
|
|
2068
|
+
"updated_at",
|
|
2069
|
+
"date"
|
|
2070
|
+
]
|
|
2071
|
+
};
|
|
2072
|
+
/**
|
|
2073
|
+
* `updateRunIf`'s compare-and-set as one raw UPDATE (Prisma's `updateMany` opens a transaction
|
|
2074
|
+
* around it), resolving the rows it changed; `undefined` for a patch left to `updateMany` (search
|
|
2075
|
+
* attributes, which reindex a side table, namespace, createdAt).
|
|
2076
|
+
*/
|
|
2077
|
+
updateRunWhere(runId, expected, patch) {
|
|
2078
|
+
const keys = Object.keys(patch).filter(
|
|
2079
|
+
// A non-null date field left undefined is left alone, as toRunPatch does.
|
|
2080
|
+
(k) => !(k === "updatedAt" && patch.updatedAt == null)
|
|
2081
|
+
);
|
|
2082
|
+
if (keys.length === 0 || keys.some((k) => !PrismaRoundTrips2.RUN_COLUMNS[k])) return void 0;
|
|
2083
|
+
const values = [];
|
|
2084
|
+
const set = keys.map((k) => {
|
|
2085
|
+
const [column, kind] = PrismaRoundTrips2.RUN_COLUMNS[k];
|
|
2086
|
+
values.push(this.value(kind, patch[k]));
|
|
2087
|
+
return `${this.q(column)} = ${this.param(kind, values.length)}`;
|
|
2088
|
+
});
|
|
2089
|
+
values.push(runId);
|
|
2090
|
+
const id = this.param("text", values.length);
|
|
2091
|
+
const statuses = expected.map((status) => {
|
|
2092
|
+
values.push(status);
|
|
2093
|
+
return this.param("text", values.length);
|
|
2094
|
+
});
|
|
2095
|
+
return this.raw.$executeRawUnsafe(`UPDATE ${this.q("durable_workflow_runs")} SET ${set.join(", ")} WHERE id = ${id} AND status IN (${statuses.join(", ")})`, ...values);
|
|
2096
|
+
}
|
|
2097
|
+
/** `releaseRunLock` as one raw UPDATE (no transaction around it). */
|
|
2098
|
+
async releaseRunLock(runId, owner) {
|
|
2099
|
+
const where = owner === void 0 ? "" : ` AND locked_by = ${this.param("text", 2)}`;
|
|
2100
|
+
await this.raw.$executeRawUnsafe(`UPDATE ${this.q("durable_workflow_runs")} SET locked_by = NULL, locked_until = NULL WHERE id = ${this.param("text", 1)}${where}`, runId, ...owner === void 0 ? [] : [
|
|
2101
|
+
owner
|
|
2102
|
+
]);
|
|
2103
|
+
}
|
|
2104
|
+
async settleResultsIfPending(results) {
|
|
2105
|
+
const out = results.map(() => null);
|
|
2106
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2107
|
+
const sent = results.flatMap((r, i) => {
|
|
2108
|
+
const key = checkpointKey(r.runId, r.seq);
|
|
2109
|
+
if (seen.has(key)) return [];
|
|
2110
|
+
seen.add(key);
|
|
2111
|
+
return [
|
|
2112
|
+
i
|
|
2113
|
+
];
|
|
2114
|
+
});
|
|
2115
|
+
if (sent.length === 0) return out;
|
|
2116
|
+
const finished = `NOT EXISTS (SELECT 1 FROM durable_workflow_runs r WHERE r.id = c.run_id AND r.status IN ('cancelled', 'completed'))`;
|
|
2117
|
+
if (!this.pg) {
|
|
2118
|
+
if (sent.length > 1) return settleResultsWithPlainCalls(this.store, results);
|
|
2119
|
+
const i = sent[0];
|
|
2120
|
+
const s = results[i];
|
|
2121
|
+
const done = s.status === "completed";
|
|
2122
|
+
const at = /* @__PURE__ */ __name((d) => d.toISOString().replace("T", " ").replace("Z", ""), "at");
|
|
2123
|
+
const wrote = await this.raw.$executeRawUnsafe(`UPDATE durable_step_checkpoints c SET c.status = ?, c.output = ${done ? "CAST(? AS JSON)" : "c.output"}, c.error = CAST(? AS JSON), c.events = COALESCE(CAST(? AS JSON), c.events), c.started_at = COALESCE(?, c.started_at), c.finished_at = ? WHERE c.run_id = ? AND c.seq = ? AND c.status = 'pending' AND ${finished}`, s.status, ...done ? [
|
|
2124
|
+
jsonText(s.output)
|
|
2125
|
+
] : [], jsonText(s.error), jsonText(s.events), s.startedAt ? at(s.startedAt) : null, at(s.finishedAt), s.runId, s.seq);
|
|
2126
|
+
if (wrote !== 1) return out;
|
|
2127
|
+
const [row] = await this.raw.$queryRawUnsafe(`SELECT ${SETTLED_CHECKPOINT_COLUMNS} FROM durable_step_checkpoints WHERE run_id = ? AND seq = ?`, s.runId, s.seq);
|
|
2128
|
+
out[i] = row ? fromCheckpointRow(fromRawRow({
|
|
2129
|
+
...row,
|
|
2130
|
+
input: null
|
|
2131
|
+
})) : null;
|
|
2132
|
+
return out;
|
|
2133
|
+
}
|
|
2134
|
+
for (let at = 0; at < sent.length; at += 500) {
|
|
2135
|
+
const chunk = sent.slice(at, at + 500);
|
|
2136
|
+
const values = [];
|
|
2137
|
+
const rows = chunk.map((i) => {
|
|
2138
|
+
const s = results[i];
|
|
2139
|
+
const n = values.length;
|
|
2140
|
+
values.push(s.runId, s.seq, s.status, jsonText(s.status === "completed" ? s.output : null), jsonText(s.error), jsonText(s.events), s.startedAt ? s.startedAt.toISOString() : null, s.finishedAt.toISOString());
|
|
2141
|
+
const p = /* @__PURE__ */ __name((k) => `$${n + k}`, "p");
|
|
2142
|
+
return `(${p(1)}::text, ${p(2)}::integer, ${p(3)}::text, ${p(4)}::jsonb, ${p(5)}::jsonb, ${p(6)}::jsonb, (${p(7)}::timestamptz AT TIME ZONE 'UTC'), (${p(8)}::timestamptz AT TIME ZONE 'UTC'))`;
|
|
2143
|
+
});
|
|
2144
|
+
const settled = await this.raw.$queryRawUnsafe(`UPDATE "durable_step_checkpoints" AS c SET status = v.status, output = CASE WHEN v.status = 'completed' THEN v.output ELSE c.output END, error = v.error, events = COALESCE(v.events, c.events), started_at = COALESCE(v.started_at, c.started_at), finished_at = v.finished_at FROM (VALUES ${rows.join(", ")}) AS v(run_id, seq, status, output, error, events, started_at, finished_at) WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending' AND ${finished} RETURNING ${SETTLED_CHECKPOINT_COLUMNS.split(", ").map((col) => `c.${col}`).join(", ")}`, ...values);
|
|
2145
|
+
const byKey = new Map(chunk.map((i) => {
|
|
2146
|
+
const s = results[i];
|
|
2147
|
+
return [
|
|
2148
|
+
checkpointKey(s.runId, s.seq),
|
|
2149
|
+
i
|
|
2150
|
+
];
|
|
2151
|
+
}));
|
|
2152
|
+
for (const raw of settled) {
|
|
2153
|
+
const cp = fromCheckpointRow(fromRawRow({
|
|
2154
|
+
...raw,
|
|
2155
|
+
input: null
|
|
2156
|
+
}));
|
|
2157
|
+
const i = byKey.get(checkpointKey(cp.runId, cp.seq));
|
|
2158
|
+
if (i !== void 0) out[i] = cp;
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
return out;
|
|
2162
|
+
}
|
|
2163
|
+
async claimPendingRuns(limit, owner, leaseUntilMs, nowMs, namespace) {
|
|
2164
|
+
if (limit <= 0) return [];
|
|
2165
|
+
const sort = /* @__PURE__ */ __name((runs) => runs.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id)), "sort");
|
|
2166
|
+
if (this.pg) {
|
|
2167
|
+
const ns = namespace !== void 0 ? " AND namespace = $5" : "";
|
|
2168
|
+
const rows = await this.raw.$queryRawUnsafe(`WITH claimed AS MATERIALIZED (SELECT id AS claimed_id FROM "durable_workflow_runs" WHERE status = 'pending' AND (locked_until IS NULL OR locked_until <= ($3::timestamptz AT TIME ZONE 'UTC'))${ns} ORDER BY created_at, id LIMIT $4 FOR UPDATE SKIP LOCKED) UPDATE "durable_workflow_runs" AS r SET locked_by = $1, locked_until = ($2::timestamptz AT TIME ZONE 'UTC') FROM claimed WHERE r.id = claimed.claimed_id RETURNING r.*`, owner, new Date(leaseUntilMs).toISOString(), new Date(nowMs).toISOString(), limit, ...namespace !== void 0 ? [
|
|
2169
|
+
namespace
|
|
2170
|
+
] : []);
|
|
2171
|
+
return sort(rows.map((raw) => fromRunRow(fromRawRow(raw))));
|
|
2172
|
+
}
|
|
2173
|
+
return this.db.$transaction(async (tx) => {
|
|
2174
|
+
const raw = tx;
|
|
2175
|
+
const at = /* @__PURE__ */ __name((ms) => new Date(ms).toISOString().replace("T", " ").replace("Z", ""), "at");
|
|
2176
|
+
const ns = namespace !== void 0 ? " AND namespace = ?" : "";
|
|
2177
|
+
const picked = (await raw.$queryRawUnsafe(`SELECT * FROM durable_workflow_runs WHERE status = 'pending' AND (locked_until IS NULL OR locked_until <= ?)${ns} ORDER BY created_at, id LIMIT ? FOR UPDATE SKIP LOCKED`, at(nowMs), ...namespace !== void 0 ? [
|
|
2178
|
+
namespace
|
|
2179
|
+
] : [], limit)).map((row) => fromRunRow(fromRawRow(row)));
|
|
2180
|
+
if (picked.length === 0) return [];
|
|
2181
|
+
await raw.$executeRawUnsafe(`UPDATE durable_workflow_runs SET locked_by = ?, locked_until = ? WHERE id IN (${picked.map(() => "?").join(", ")})`, owner, at(leaseUntilMs), ...picked.map((run) => run.id));
|
|
2182
|
+
return sort(picked.map((run) => ({
|
|
2183
|
+
...run,
|
|
2184
|
+
lockedBy: owner,
|
|
2185
|
+
lockedUntil: leaseUntilMs
|
|
2186
|
+
})));
|
|
2187
|
+
});
|
|
2188
|
+
}
|
|
2189
|
+
};
|
|
1719
2190
|
|
|
1720
2191
|
// src/schema.ts
|
|
1721
2192
|
function durableManagedTables() {
|
|
@@ -1726,6 +2197,7 @@ function durableManagedTables() {
|
|
|
1726
2197
|
"durable_signal_waiters",
|
|
1727
2198
|
"durable_buffered_signals",
|
|
1728
2199
|
"durable_buffered_events",
|
|
2200
|
+
"durable_signal_ids",
|
|
1729
2201
|
"durable_schedules"
|
|
1730
2202
|
];
|
|
1731
2203
|
}
|