@dudousxd/nestjs-durable-store-drizzle 0.27.0 → 0.29.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 +4 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -2
- package/dist/index.d.ts +9 -2
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/pg/index.cjs +568 -454
- package/dist/pg/index.cjs.map +1 -1
- package/dist/pg/index.d.cts +1269 -1223
- package/dist/pg/index.d.ts +1269 -1223
- package/dist/pg/index.js +561 -450
- package/dist/pg/index.js.map +1 -1
- package/package.json +9 -6
package/dist/pg/index.js
CHANGED
|
@@ -36,6 +36,219 @@ function drizzlePgExecutor(db) {
|
|
|
36
36
|
}
|
|
37
37
|
__name(drizzlePgExecutor, "drizzlePgExecutor");
|
|
38
38
|
|
|
39
|
+
// src/pg/schema.ts
|
|
40
|
+
import { getTableName } from "drizzle-orm";
|
|
41
|
+
import { bigserial, boolean, doublePrecision, index, integer, jsonb, pgSchema, pgTable, primaryKey, text, timestamp } from "drizzle-orm/pg-core";
|
|
42
|
+
var DEFAULT_NAMESPACE = "default";
|
|
43
|
+
var tstz = /* @__PURE__ */ __name((name) => timestamp(name, {
|
|
44
|
+
withTimezone: true,
|
|
45
|
+
mode: "date"
|
|
46
|
+
}), "tstz");
|
|
47
|
+
function buildDurablePgTables(table) {
|
|
48
|
+
const durableWorkflowRuns2 = table("durable_workflow_runs", {
|
|
49
|
+
id: text("id").primaryKey(),
|
|
50
|
+
workflow: text("workflow").notNull(),
|
|
51
|
+
workflowVersion: text("workflow_version").notNull(),
|
|
52
|
+
status: text("status").notNull(),
|
|
53
|
+
input: jsonb("input"),
|
|
54
|
+
output: jsonb("output"),
|
|
55
|
+
error: jsonb("error"),
|
|
56
|
+
wakeAt: tstz("wake_at"),
|
|
57
|
+
lockedBy: text("locked_by"),
|
|
58
|
+
lockedUntil: tstz("locked_until"),
|
|
59
|
+
// REMOTE turn the engine suspended on awaiting a decision; matched by completeRemoteDecision so
|
|
60
|
+
// only the currently-awaited turn's decision is applied. Nullable: cleared when a decision lands.
|
|
61
|
+
awaitingDecisionTaskId: text("awaiting_decision_task_id"),
|
|
62
|
+
recoveryAttempts: integer("recovery_attempts"),
|
|
63
|
+
tags: jsonb("tags").$type(),
|
|
64
|
+
searchAttributes: jsonb("search_attributes").$type(),
|
|
65
|
+
priority: integer("priority"),
|
|
66
|
+
// The worker-pool partition (tenant) — THE isolation boundary every poll predicate filters on.
|
|
67
|
+
// NOT NULL DEFAULT 'default' so a row can never fall outside every worker's namespace.
|
|
68
|
+
namespace: text("namespace").notNull().default(DEFAULT_NAMESPACE),
|
|
69
|
+
// Which package declared the workflow. Nullable, no default: NULL = unknown, never guessed.
|
|
70
|
+
origin: text("origin"),
|
|
71
|
+
// The customer (tenancy) the run belongs to — not the worker-pool `namespace`. NULL = none.
|
|
72
|
+
tenantId: text("tenant_id"),
|
|
73
|
+
tenantKind: text("tenant_kind"),
|
|
74
|
+
createdAt: tstz("created_at").notNull(),
|
|
75
|
+
updatedAt: tstz("updated_at").notNull()
|
|
76
|
+
}, (t) => [
|
|
77
|
+
// App-facing reads with tenancy on: `tenant_id = $1 ORDER BY created_at DESC`.
|
|
78
|
+
index("durable_workflow_runs_tenant_idx").on(t.tenantId, t.createdAt),
|
|
79
|
+
// Timer poller (`listDueTimers`) + recovery (`listIncompleteRuns`). Same name as the TypeORM DDL.
|
|
80
|
+
index("durable_runs_status_idx").on(t.status, t.wakeAt),
|
|
81
|
+
// Dashboard `listRuns({ workflow, status })` and the singleton gate's per-workflow scans.
|
|
82
|
+
index("durable_runs_workflow_status_idx").on(t.workflow, t.status),
|
|
83
|
+
// Every namespaced poll tick: `namespace = $1 AND status = $2 ORDER BY created_at` (FIFO pick-up).
|
|
84
|
+
// Name matches the MikroORM adapter + the SQLite drizzle schema so a store swap keeps the index.
|
|
85
|
+
index("durable_workflow_runs_namespace_status_idx").on(t.namespace, t.status, t.createdAt),
|
|
86
|
+
// `listRuns({ tag })` compiles to `tags @> '["tag"]'`, which a `jsonb_path_ops` GIN index serves —
|
|
87
|
+
// this is what keeps the singleton gate (`tag: singleton:<key>`, e.g. a per-tenant concurrency
|
|
88
|
+
// key) an index probe instead of a scan of every run the workflow ever had.
|
|
89
|
+
index("durable_runs_tags_gin_idx").using("gin", t.tags.op("jsonb_path_ops")),
|
|
90
|
+
// Retention (`pruneTerminalRuns`) orders a status set by `updated_at`.
|
|
91
|
+
index("durable_runs_status_updated_idx").on(t.status, t.updatedAt)
|
|
92
|
+
]);
|
|
93
|
+
const durableStepCheckpoints2 = table(
|
|
94
|
+
"durable_step_checkpoints",
|
|
95
|
+
{
|
|
96
|
+
runId: text("run_id").notNull(),
|
|
97
|
+
seq: integer("seq").notNull(),
|
|
98
|
+
name: text("name").notNull(),
|
|
99
|
+
kind: text("kind").notNull(),
|
|
100
|
+
stepId: text("step_id").notNull(),
|
|
101
|
+
status: text("status").notNull(),
|
|
102
|
+
input: jsonb("input"),
|
|
103
|
+
output: jsonb("output"),
|
|
104
|
+
error: jsonb("error"),
|
|
105
|
+
events: jsonb("events"),
|
|
106
|
+
attempts: integer("attempts").notNull(),
|
|
107
|
+
workerGroup: text("worker_group"),
|
|
108
|
+
parallelGroup: text("parallel_group"),
|
|
109
|
+
wakeAt: tstz("wake_at"),
|
|
110
|
+
enqueuedAt: tstz("enqueued_at"),
|
|
111
|
+
startedAt: tstz("started_at").notNull(),
|
|
112
|
+
finishedAt: tstz("finished_at").notNull()
|
|
113
|
+
},
|
|
114
|
+
// (run_id, seq) is the primary key, so listCheckpoints / getEvent already hit an index.
|
|
115
|
+
(t) => [
|
|
116
|
+
primaryKey({
|
|
117
|
+
columns: [
|
|
118
|
+
t.runId,
|
|
119
|
+
t.seq
|
|
120
|
+
]
|
|
121
|
+
}),
|
|
122
|
+
// A run detail reads one fan at a time: checkpointStats / queryCheckpoints by group, seq-paged.
|
|
123
|
+
index("durable_step_checkpoints_group_idx").on(t.runId, t.parallelGroup, t.seq)
|
|
124
|
+
]
|
|
125
|
+
);
|
|
126
|
+
const durableRunAttributes2 = table("durable_run_attributes", {
|
|
127
|
+
runId: text("run_id").notNull(),
|
|
128
|
+
key: text("key").notNull(),
|
|
129
|
+
strValue: text("str_value"),
|
|
130
|
+
numValue: doublePrecision("num_value")
|
|
131
|
+
}, (t) => [
|
|
132
|
+
primaryKey({
|
|
133
|
+
columns: [
|
|
134
|
+
t.runId,
|
|
135
|
+
t.key
|
|
136
|
+
]
|
|
137
|
+
}),
|
|
138
|
+
index("durable_run_attributes_num_idx").on(t.key, t.numValue),
|
|
139
|
+
index("durable_run_attributes_str_idx").on(t.key, t.strValue)
|
|
140
|
+
]);
|
|
141
|
+
const durableSignalWaiters2 = table(
|
|
142
|
+
"durable_signal_waiters",
|
|
143
|
+
{
|
|
144
|
+
token: text("token").primaryKey(),
|
|
145
|
+
runId: text("run_id").notNull(),
|
|
146
|
+
seq: integer("seq").notNull(),
|
|
147
|
+
parallelGroup: text("parallel_group")
|
|
148
|
+
},
|
|
149
|
+
// deleteRun / pruneTerminalRuns delete a run's waiters by run_id.
|
|
150
|
+
(t) => [
|
|
151
|
+
index("durable_signal_waiters_run_id_idx").on(t.runId)
|
|
152
|
+
]
|
|
153
|
+
);
|
|
154
|
+
const durableBufferedSignals2 = table(
|
|
155
|
+
"durable_buffered_signals",
|
|
156
|
+
{
|
|
157
|
+
id: bigserial("id", {
|
|
158
|
+
mode: "number"
|
|
159
|
+
}).primaryKey(),
|
|
160
|
+
token: text("token").notNull(),
|
|
161
|
+
payload: jsonb("payload")
|
|
162
|
+
},
|
|
163
|
+
// Taken FIFO per token (smallest id first).
|
|
164
|
+
(t) => [
|
|
165
|
+
index("durable_buffered_signals_token_idx").on(t.token, t.id)
|
|
166
|
+
]
|
|
167
|
+
);
|
|
168
|
+
const durableBufferedEvents2 = table("durable_buffered_events", {
|
|
169
|
+
id: text("id").primaryKey(),
|
|
170
|
+
name: text("name").notNull(),
|
|
171
|
+
payload: jsonb("payload"),
|
|
172
|
+
publishedAt: tstz("published_at").notNull()
|
|
173
|
+
}, (t) => [
|
|
174
|
+
index("durable_buffered_events_name_published_at_idx").on(t.name, t.publishedAt)
|
|
175
|
+
]);
|
|
176
|
+
const durableSignalIds2 = table("durable_signal_ids", {
|
|
177
|
+
token: text("token").notNull(),
|
|
178
|
+
signalId: text("signal_id").notNull(),
|
|
179
|
+
expiresAt: tstz("expires_at").notNull()
|
|
180
|
+
}, (t) => [
|
|
181
|
+
primaryKey({
|
|
182
|
+
columns: [
|
|
183
|
+
t.token,
|
|
184
|
+
t.signalId
|
|
185
|
+
]
|
|
186
|
+
}),
|
|
187
|
+
index("durable_signal_ids_expires_at_idx").on(t.expiresAt)
|
|
188
|
+
]);
|
|
189
|
+
const durableSchedules2 = table(
|
|
190
|
+
"durable_schedules",
|
|
191
|
+
{
|
|
192
|
+
id: text("id").primaryKey(),
|
|
193
|
+
namespace: text("namespace").notNull().default(DEFAULT_NAMESPACE),
|
|
194
|
+
workflow: text("workflow").notNull(),
|
|
195
|
+
paused: boolean("paused").notNull().default(false),
|
|
196
|
+
nextFireAt: tstz("next_fire_at"),
|
|
197
|
+
tags: jsonb("tags").$type(),
|
|
198
|
+
spec: jsonb("spec").$type().notNull(),
|
|
199
|
+
state: jsonb("state").$type().notNull(),
|
|
200
|
+
// The customer the schedule (and every run it starts) belongs to. NULL = none.
|
|
201
|
+
tenantId: text("tenant_id"),
|
|
202
|
+
tenantKind: text("tenant_kind"),
|
|
203
|
+
createdAt: tstz("created_at").notNull(),
|
|
204
|
+
updatedAt: tstz("updated_at").notNull()
|
|
205
|
+
},
|
|
206
|
+
// The poll's "due by T" scan: `paused = false AND next_fire_at <= T ORDER BY next_fire_at`.
|
|
207
|
+
(t) => [
|
|
208
|
+
index("durable_schedules_due_idx").on(t.paused, t.nextFireAt),
|
|
209
|
+
index("durable_schedules_tenant_idx").on(t.tenantId)
|
|
210
|
+
]
|
|
211
|
+
);
|
|
212
|
+
return {
|
|
213
|
+
durableWorkflowRuns: durableWorkflowRuns2,
|
|
214
|
+
durableStepCheckpoints: durableStepCheckpoints2,
|
|
215
|
+
durableRunAttributes: durableRunAttributes2,
|
|
216
|
+
durableSignalWaiters: durableSignalWaiters2,
|
|
217
|
+
durableBufferedSignals: durableBufferedSignals2,
|
|
218
|
+
durableBufferedEvents: durableBufferedEvents2,
|
|
219
|
+
durableSignalIds: durableSignalIds2,
|
|
220
|
+
durableSchedules: durableSchedules2
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
__name(buildDurablePgTables, "buildDurablePgTables");
|
|
224
|
+
var durablePgSchema = buildDurablePgTables(pgTable);
|
|
225
|
+
var { durableWorkflowRuns, durableStepCheckpoints, durableRunAttributes, durableSignalWaiters, durableBufferedSignals, durableBufferedEvents, durableSignalIds, durableSchedules } = durablePgSchema;
|
|
226
|
+
var SCHEMA_NAME = /^[A-Za-z_][A-Za-z0-9_$]*$/;
|
|
227
|
+
function durableSchemaName(schema) {
|
|
228
|
+
if (schema === void 0) return void 0;
|
|
229
|
+
if (!SCHEMA_NAME.test(schema) || schema.length > 63) {
|
|
230
|
+
throw new Error(`[nestjs-durable] invalid schema ${JSON.stringify(schema)}: use letters, digits, _ or $ (not first), at most 63 characters.`);
|
|
231
|
+
}
|
|
232
|
+
return schema;
|
|
233
|
+
}
|
|
234
|
+
__name(durableSchemaName, "durableSchemaName");
|
|
235
|
+
var tablesBySchema = /* @__PURE__ */ new Map();
|
|
236
|
+
function durablePgTables(schema) {
|
|
237
|
+
const name = durableSchemaName(schema);
|
|
238
|
+
if (name === void 0) return durablePgSchema;
|
|
239
|
+
let tables = tablesBySchema.get(name);
|
|
240
|
+
if (!tables) {
|
|
241
|
+
tables = buildDurablePgTables(pgSchema(name).table);
|
|
242
|
+
tablesBySchema.set(name, tables);
|
|
243
|
+
}
|
|
244
|
+
return tables;
|
|
245
|
+
}
|
|
246
|
+
__name(durablePgTables, "durablePgTables");
|
|
247
|
+
function durablePgManagedTables() {
|
|
248
|
+
return Object.values(durablePgSchema).map((table) => getTableName(table));
|
|
249
|
+
}
|
|
250
|
+
__name(durablePgManagedTables, "durablePgManagedTables");
|
|
251
|
+
|
|
39
252
|
// src/pg/ddl.ts
|
|
40
253
|
var DURABLE_PG_DDL = [
|
|
41
254
|
`CREATE TABLE IF NOT EXISTS "durable_workflow_runs" (
|
|
@@ -146,6 +359,16 @@ var DURABLE_PG_DDL = [
|
|
|
146
359
|
`CREATE INDEX IF NOT EXISTS "durable_schedules_tenant_idx" ON "durable_schedules" USING btree ("tenant_id")`,
|
|
147
360
|
`CREATE INDEX IF NOT EXISTS "durable_signal_ids_expires_at_idx" ON "durable_signal_ids" USING btree ("expires_at")`
|
|
148
361
|
];
|
|
362
|
+
function durablePgDdl(schema) {
|
|
363
|
+
const name = durableSchemaName(schema);
|
|
364
|
+
if (name === void 0) return DURABLE_PG_DDL;
|
|
365
|
+
const q = `"${name}"`;
|
|
366
|
+
return [
|
|
367
|
+
`CREATE SCHEMA IF NOT EXISTS ${q}`,
|
|
368
|
+
...DURABLE_PG_DDL.map((statement) => statement.replace(/^(CREATE TABLE IF NOT EXISTS |ALTER TABLE |(?:CREATE (?:UNIQUE )?INDEX (?:CONCURRENTLY )?IF NOT EXISTS "[^"]+" ON ))"(durable_[^"]+)"/i, `$1${q}."$2"`))
|
|
369
|
+
];
|
|
370
|
+
}
|
|
371
|
+
__name(durablePgDdl, "durablePgDdl");
|
|
149
372
|
var SCHEMA_LOCK_KEY = "durable_schema";
|
|
150
373
|
function drizzlePgSchemaRepairWarning(repairs) {
|
|
151
374
|
return [
|
|
@@ -159,18 +382,18 @@ function drizzlePgSchemaRepairWarning(repairs) {
|
|
|
159
382
|
}
|
|
160
383
|
__name(drizzlePgSchemaRepairWarning, "drizzlePgSchemaRepairWarning");
|
|
161
384
|
function ddlTarget(statement) {
|
|
162
|
-
const table = statement.match(/^CREATE TABLE IF NOT EXISTS "([^"]+)"/i);
|
|
385
|
+
const table = statement.match(/^CREATE TABLE IF NOT EXISTS (?:"[^"]+"\.)?"([^"]+)"/i);
|
|
163
386
|
if (table?.[1]) return {
|
|
164
387
|
kind: "table",
|
|
165
388
|
table: table[1]
|
|
166
389
|
};
|
|
167
|
-
const index2 = statement.match(/^CREATE (?:UNIQUE )?INDEX (?:CONCURRENTLY )?IF NOT EXISTS "([^"]+)" ON "([^"]+)"/i);
|
|
390
|
+
const index2 = statement.match(/^CREATE (?:UNIQUE )?INDEX (?:CONCURRENTLY )?IF NOT EXISTS "([^"]+)" ON (?:"[^"]+"\.)?"([^"]+)"/i);
|
|
168
391
|
if (index2?.[1] && index2[2]) return {
|
|
169
392
|
kind: "index",
|
|
170
393
|
index: index2[1],
|
|
171
394
|
table: index2[2]
|
|
172
395
|
};
|
|
173
|
-
const column = statement.match(/^ALTER TABLE "([^"]+)" ADD COLUMN IF NOT EXISTS "([^"]+)"/i);
|
|
396
|
+
const column = statement.match(/^ALTER TABLE (?:"[^"]+"\.)?"([^"]+)" ADD COLUMN IF NOT EXISTS "([^"]+)"/i);
|
|
174
397
|
if (column?.[1] && column[2]) return {
|
|
175
398
|
kind: "column",
|
|
176
399
|
table: column[1],
|
|
@@ -181,15 +404,27 @@ function ddlTarget(statement) {
|
|
|
181
404
|
__name(ddlTarget, "ddlTarget");
|
|
182
405
|
async function ensureDrizzlePgDurableSchema(db, options = {}) {
|
|
183
406
|
const logger = options.logger ?? console;
|
|
407
|
+
const schema = durableSchemaName(options.schema);
|
|
408
|
+
const lockKey = schema === void 0 ? SCHEMA_LOCK_KEY : `${SCHEMA_LOCK_KEY}:${schema}`;
|
|
409
|
+
const inSchema = /* @__PURE__ */ __name((column) => schema === void 0 ? sql2.raw(`${column} = current_schema()`) : sql2`${sql2.raw(column)} = ${schema}`, "inSchema");
|
|
410
|
+
const ddl = durablePgDdl(schema).filter((statement) => !statement.startsWith("CREATE SCHEMA"));
|
|
184
411
|
const { repairs, freshInstall } = await db.transaction(async (tx) => {
|
|
185
|
-
await tx.execute(sql2`SELECT pg_advisory_xact_lock(hashtext(${
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
412
|
+
await tx.execute(sql2`SELECT pg_advisory_xact_lock(hashtext(${lockKey}))`);
|
|
413
|
+
const hasSchema = schema === void 0 || rowsOf(await tx.execute(sql2`SELECT 1 FROM pg_namespace WHERE nspname = ${schema}`)).length > 0;
|
|
414
|
+
if (!hasSchema) {
|
|
415
|
+
await tx.execute(sql2.raw(`CREATE SCHEMA IF NOT EXISTS "${schema}"`)).catch((err) => {
|
|
416
|
+
throw new Error(`[nestjs-durable] store-drizzle could not create the schema "${schema}" for the durable tables. Create it ahead of time, or grant this role CREATE on the database (GRANT CREATE ON DATABASE <db> TO <role>). Cause: ${err.message}`, {
|
|
417
|
+
cause: err
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
const tables = new Set(rowsOf(await tx.execute(sql2`SELECT tablename AS name FROM pg_tables WHERE ${inSchema("schemaname")} AND left(tablename, 8) = 'durable_'`)).map((row) => row.name));
|
|
422
|
+
const indexes = new Set(rowsOf(await tx.execute(sql2`SELECT indexname AS name FROM pg_indexes WHERE ${inSchema("schemaname")} AND left(tablename, 8) = 'durable_'`)).map((row) => row.name));
|
|
423
|
+
const columns = new Set(rowsOf(await tx.execute(sql2`SELECT table_name || '.' || column_name AS name FROM information_schema.columns WHERE ${inSchema("table_schema")} AND left(table_name, 8) = 'durable_'`)).map((row) => row.name));
|
|
189
424
|
const created = /* @__PURE__ */ new Set();
|
|
190
425
|
const ddlTables = /* @__PURE__ */ new Set();
|
|
191
426
|
const applied = [];
|
|
192
|
-
for (const statement of
|
|
427
|
+
for (const statement of ddl) {
|
|
193
428
|
const target = ddlTarget(statement);
|
|
194
429
|
await tx.execute(sql2.raw(statement));
|
|
195
430
|
if (!target) continue;
|
|
@@ -399,193 +634,6 @@ function placeholders(from, count) {
|
|
|
399
634
|
}
|
|
400
635
|
__name(placeholders, "placeholders");
|
|
401
636
|
|
|
402
|
-
// src/pg/schema.ts
|
|
403
|
-
import { getTableName } from "drizzle-orm";
|
|
404
|
-
import { bigserial, boolean, doublePrecision, index, integer, jsonb, pgTable, primaryKey, text, timestamp } from "drizzle-orm/pg-core";
|
|
405
|
-
var DEFAULT_NAMESPACE = "default";
|
|
406
|
-
var tstz = /* @__PURE__ */ __name((name) => timestamp(name, {
|
|
407
|
-
withTimezone: true,
|
|
408
|
-
mode: "date"
|
|
409
|
-
}), "tstz");
|
|
410
|
-
var durableWorkflowRuns = pgTable("durable_workflow_runs", {
|
|
411
|
-
id: text("id").primaryKey(),
|
|
412
|
-
workflow: text("workflow").notNull(),
|
|
413
|
-
workflowVersion: text("workflow_version").notNull(),
|
|
414
|
-
status: text("status").notNull(),
|
|
415
|
-
input: jsonb("input"),
|
|
416
|
-
output: jsonb("output"),
|
|
417
|
-
error: jsonb("error"),
|
|
418
|
-
wakeAt: tstz("wake_at"),
|
|
419
|
-
lockedBy: text("locked_by"),
|
|
420
|
-
lockedUntil: tstz("locked_until"),
|
|
421
|
-
// REMOTE turn the engine suspended on awaiting a decision; matched by completeRemoteDecision so
|
|
422
|
-
// only the currently-awaited turn's decision is applied. Nullable: cleared when a decision lands.
|
|
423
|
-
awaitingDecisionTaskId: text("awaiting_decision_task_id"),
|
|
424
|
-
recoveryAttempts: integer("recovery_attempts"),
|
|
425
|
-
tags: jsonb("tags").$type(),
|
|
426
|
-
searchAttributes: jsonb("search_attributes").$type(),
|
|
427
|
-
priority: integer("priority"),
|
|
428
|
-
// The worker-pool partition (tenant) — THE isolation boundary every poll predicate filters on.
|
|
429
|
-
// NOT NULL DEFAULT 'default' so a row can never fall outside every worker's namespace.
|
|
430
|
-
namespace: text("namespace").notNull().default(DEFAULT_NAMESPACE),
|
|
431
|
-
// Which package declared the workflow. Nullable, no default: NULL = unknown, never guessed.
|
|
432
|
-
origin: text("origin"),
|
|
433
|
-
// The customer (tenancy) the run belongs to — not the worker-pool `namespace`. NULL = none.
|
|
434
|
-
tenantId: text("tenant_id"),
|
|
435
|
-
tenantKind: text("tenant_kind"),
|
|
436
|
-
createdAt: tstz("created_at").notNull(),
|
|
437
|
-
updatedAt: tstz("updated_at").notNull()
|
|
438
|
-
}, (t) => [
|
|
439
|
-
// App-facing reads with tenancy on: `tenant_id = $1 ORDER BY created_at DESC`.
|
|
440
|
-
index("durable_workflow_runs_tenant_idx").on(t.tenantId, t.createdAt),
|
|
441
|
-
// Timer poller (`listDueTimers`) + recovery (`listIncompleteRuns`). Same name as the TypeORM DDL.
|
|
442
|
-
index("durable_runs_status_idx").on(t.status, t.wakeAt),
|
|
443
|
-
// Dashboard `listRuns({ workflow, status })` and the singleton gate's per-workflow scans.
|
|
444
|
-
index("durable_runs_workflow_status_idx").on(t.workflow, t.status),
|
|
445
|
-
// Every namespaced poll tick: `namespace = $1 AND status = $2 ORDER BY created_at` (FIFO pick-up).
|
|
446
|
-
// Name matches the MikroORM adapter + the SQLite drizzle schema so a store swap keeps the index.
|
|
447
|
-
index("durable_workflow_runs_namespace_status_idx").on(t.namespace, t.status, t.createdAt),
|
|
448
|
-
// `listRuns({ tag })` compiles to `tags @> '["tag"]'`, which a `jsonb_path_ops` GIN index serves —
|
|
449
|
-
// this is what keeps the singleton gate (`tag: singleton:<key>`, e.g. a per-tenant concurrency
|
|
450
|
-
// key) an index probe instead of a scan of every run the workflow ever had.
|
|
451
|
-
index("durable_runs_tags_gin_idx").using("gin", t.tags.op("jsonb_path_ops")),
|
|
452
|
-
// Retention (`pruneTerminalRuns`) orders a status set by `updated_at`.
|
|
453
|
-
index("durable_runs_status_updated_idx").on(t.status, t.updatedAt)
|
|
454
|
-
]);
|
|
455
|
-
var durableStepCheckpoints = pgTable(
|
|
456
|
-
"durable_step_checkpoints",
|
|
457
|
-
{
|
|
458
|
-
runId: text("run_id").notNull(),
|
|
459
|
-
seq: integer("seq").notNull(),
|
|
460
|
-
name: text("name").notNull(),
|
|
461
|
-
kind: text("kind").notNull(),
|
|
462
|
-
stepId: text("step_id").notNull(),
|
|
463
|
-
status: text("status").notNull(),
|
|
464
|
-
input: jsonb("input"),
|
|
465
|
-
output: jsonb("output"),
|
|
466
|
-
error: jsonb("error"),
|
|
467
|
-
events: jsonb("events"),
|
|
468
|
-
attempts: integer("attempts").notNull(),
|
|
469
|
-
workerGroup: text("worker_group"),
|
|
470
|
-
parallelGroup: text("parallel_group"),
|
|
471
|
-
wakeAt: tstz("wake_at"),
|
|
472
|
-
enqueuedAt: tstz("enqueued_at"),
|
|
473
|
-
startedAt: tstz("started_at").notNull(),
|
|
474
|
-
finishedAt: tstz("finished_at").notNull()
|
|
475
|
-
},
|
|
476
|
-
// (run_id, seq) is the primary key, so listCheckpoints / getEvent already hit an index.
|
|
477
|
-
(t) => [
|
|
478
|
-
primaryKey({
|
|
479
|
-
columns: [
|
|
480
|
-
t.runId,
|
|
481
|
-
t.seq
|
|
482
|
-
]
|
|
483
|
-
}),
|
|
484
|
-
// A run detail reads one fan at a time: checkpointStats / queryCheckpoints by group, seq-paged.
|
|
485
|
-
index("durable_step_checkpoints_group_idx").on(t.runId, t.parallelGroup, t.seq)
|
|
486
|
-
]
|
|
487
|
-
);
|
|
488
|
-
var durableRunAttributes = pgTable("durable_run_attributes", {
|
|
489
|
-
runId: text("run_id").notNull(),
|
|
490
|
-
key: text("key").notNull(),
|
|
491
|
-
strValue: text("str_value"),
|
|
492
|
-
numValue: doublePrecision("num_value")
|
|
493
|
-
}, (t) => [
|
|
494
|
-
primaryKey({
|
|
495
|
-
columns: [
|
|
496
|
-
t.runId,
|
|
497
|
-
t.key
|
|
498
|
-
]
|
|
499
|
-
}),
|
|
500
|
-
index("durable_run_attributes_num_idx").on(t.key, t.numValue),
|
|
501
|
-
index("durable_run_attributes_str_idx").on(t.key, t.strValue)
|
|
502
|
-
]);
|
|
503
|
-
var durableSignalWaiters = pgTable(
|
|
504
|
-
"durable_signal_waiters",
|
|
505
|
-
{
|
|
506
|
-
token: text("token").primaryKey(),
|
|
507
|
-
runId: text("run_id").notNull(),
|
|
508
|
-
seq: integer("seq").notNull(),
|
|
509
|
-
parallelGroup: text("parallel_group")
|
|
510
|
-
},
|
|
511
|
-
// deleteRun / pruneTerminalRuns delete a run's waiters by run_id.
|
|
512
|
-
(t) => [
|
|
513
|
-
index("durable_signal_waiters_run_id_idx").on(t.runId)
|
|
514
|
-
]
|
|
515
|
-
);
|
|
516
|
-
var durableBufferedSignals = pgTable(
|
|
517
|
-
"durable_buffered_signals",
|
|
518
|
-
{
|
|
519
|
-
id: bigserial("id", {
|
|
520
|
-
mode: "number"
|
|
521
|
-
}).primaryKey(),
|
|
522
|
-
token: text("token").notNull(),
|
|
523
|
-
payload: jsonb("payload")
|
|
524
|
-
},
|
|
525
|
-
// Taken FIFO per token (smallest id first).
|
|
526
|
-
(t) => [
|
|
527
|
-
index("durable_buffered_signals_token_idx").on(t.token, t.id)
|
|
528
|
-
]
|
|
529
|
-
);
|
|
530
|
-
var durableBufferedEvents = pgTable("durable_buffered_events", {
|
|
531
|
-
id: text("id").primaryKey(),
|
|
532
|
-
name: text("name").notNull(),
|
|
533
|
-
payload: jsonb("payload"),
|
|
534
|
-
publishedAt: tstz("published_at").notNull()
|
|
535
|
-
}, (t) => [
|
|
536
|
-
index("durable_buffered_events_name_published_at_idx").on(t.name, t.publishedAt)
|
|
537
|
-
]);
|
|
538
|
-
var durableSignalIds = pgTable("durable_signal_ids", {
|
|
539
|
-
token: text("token").notNull(),
|
|
540
|
-
signalId: text("signal_id").notNull(),
|
|
541
|
-
expiresAt: tstz("expires_at").notNull()
|
|
542
|
-
}, (t) => [
|
|
543
|
-
primaryKey({
|
|
544
|
-
columns: [
|
|
545
|
-
t.token,
|
|
546
|
-
t.signalId
|
|
547
|
-
]
|
|
548
|
-
}),
|
|
549
|
-
index("durable_signal_ids_expires_at_idx").on(t.expiresAt)
|
|
550
|
-
]);
|
|
551
|
-
var durableSchedules = pgTable(
|
|
552
|
-
"durable_schedules",
|
|
553
|
-
{
|
|
554
|
-
id: text("id").primaryKey(),
|
|
555
|
-
namespace: text("namespace").notNull().default(DEFAULT_NAMESPACE),
|
|
556
|
-
workflow: text("workflow").notNull(),
|
|
557
|
-
paused: boolean("paused").notNull().default(false),
|
|
558
|
-
nextFireAt: tstz("next_fire_at"),
|
|
559
|
-
tags: jsonb("tags").$type(),
|
|
560
|
-
spec: jsonb("spec").$type().notNull(),
|
|
561
|
-
state: jsonb("state").$type().notNull(),
|
|
562
|
-
// The customer the schedule (and every run it starts) belongs to. NULL = none.
|
|
563
|
-
tenantId: text("tenant_id"),
|
|
564
|
-
tenantKind: text("tenant_kind"),
|
|
565
|
-
createdAt: tstz("created_at").notNull(),
|
|
566
|
-
updatedAt: tstz("updated_at").notNull()
|
|
567
|
-
},
|
|
568
|
-
// The poll's "due by T" scan: `paused = false AND next_fire_at <= T ORDER BY next_fire_at`.
|
|
569
|
-
(t) => [
|
|
570
|
-
index("durable_schedules_due_idx").on(t.paused, t.nextFireAt),
|
|
571
|
-
index("durable_schedules_tenant_idx").on(t.tenantId)
|
|
572
|
-
]
|
|
573
|
-
);
|
|
574
|
-
var durablePgSchema = {
|
|
575
|
-
durableWorkflowRuns,
|
|
576
|
-
durableStepCheckpoints,
|
|
577
|
-
durableRunAttributes,
|
|
578
|
-
durableSignalWaiters,
|
|
579
|
-
durableBufferedSignals,
|
|
580
|
-
durableBufferedEvents,
|
|
581
|
-
durableSignalIds,
|
|
582
|
-
durableSchedules
|
|
583
|
-
};
|
|
584
|
-
function durablePgManagedTables() {
|
|
585
|
-
return Object.values(durablePgSchema).map((table) => getTableName(table));
|
|
586
|
-
}
|
|
587
|
-
__name(durablePgManagedTables, "durablePgManagedTables");
|
|
588
|
-
|
|
589
637
|
// src/pg/drizzle-pg-state-store.ts
|
|
590
638
|
var RETENTION_EXCLUDED = [
|
|
591
639
|
LIFECYCLE_REGISTRY_WORKFLOW
|
|
@@ -603,19 +651,19 @@ function tenantFilters(column, query) {
|
|
|
603
651
|
];
|
|
604
652
|
}
|
|
605
653
|
__name(tenantFilters, "tenantFilters");
|
|
606
|
-
function namespaceFilter(namespace) {
|
|
654
|
+
function namespaceFilter(t, namespace) {
|
|
607
655
|
if (namespace === void 0) return void 0;
|
|
608
656
|
if (namespace === DEFAULT_NAMESPACE || namespace === "") {
|
|
609
|
-
return inArray(
|
|
657
|
+
return inArray(t.workflowRuns.namespace, [
|
|
610
658
|
DEFAULT_NAMESPACE,
|
|
611
659
|
""
|
|
612
660
|
]);
|
|
613
661
|
}
|
|
614
|
-
return eq(
|
|
662
|
+
return eq(t.workflowRuns.namespace, namespace);
|
|
615
663
|
}
|
|
616
664
|
__name(namespaceFilter, "namespaceFilter");
|
|
617
|
-
function hasTag(tag) {
|
|
618
|
-
return sql3`${
|
|
665
|
+
function hasTag(t, tag) {
|
|
666
|
+
return sql3`${t.workflowRuns.tags} @> ${JSON.stringify([
|
|
619
667
|
tag
|
|
620
668
|
])}::jsonb`;
|
|
621
669
|
}
|
|
@@ -635,9 +683,16 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
635
683
|
hotScope;
|
|
636
684
|
/** Hot-path UPDATE texts by patch shape. */
|
|
637
685
|
hotUpdates = /* @__PURE__ */ new Map();
|
|
686
|
+
/** The durable tables, in {@link DrizzlePgStateStoreOptions.schema} when one is set. */
|
|
687
|
+
t;
|
|
688
|
+
/** The hot path's constant statements for those tables. */
|
|
689
|
+
s;
|
|
638
690
|
constructor(db, options = {}) {
|
|
639
691
|
this.db = db;
|
|
640
692
|
this.options = options;
|
|
693
|
+
const schema = durableSchemaName(options.schema);
|
|
694
|
+
this.t = storeTables(schema);
|
|
695
|
+
this.s = hotStatements(schema);
|
|
641
696
|
this.scopeNamespace = options.scope?.namespace;
|
|
642
697
|
this.hot = PgHotPath.for(db, {
|
|
643
698
|
preparedStatements: options.preparedStatements
|
|
@@ -670,7 +725,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
670
725
|
if (text2 === void 0) {
|
|
671
726
|
const set = keys.map((key, i) => `${RUNS.name(key)} = $${i + 1}`).join(", ");
|
|
672
727
|
const id = keys.length + 1;
|
|
673
|
-
text2 = `UPDATE ${T_RUNS} SET ${set} WHERE "id" = $${id}`;
|
|
728
|
+
text2 = `UPDATE ${this.s.T_RUNS} SET ${set} WHERE "id" = $${id}`;
|
|
674
729
|
if (expected) {
|
|
675
730
|
text2 += ` AND "status" IN (${placeholders(id + 1, expected.length)}) RETURNING "id"`;
|
|
676
731
|
}
|
|
@@ -695,49 +750,50 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
695
750
|
* module's `autoSchema` is on. Turn `autoSchema` off if drizzle-kit owns these tables. */
|
|
696
751
|
async ensureSchema() {
|
|
697
752
|
await ensureDrizzlePgDurableSchema(this.db, {
|
|
698
|
-
logger: this.options.schemaLogger
|
|
753
|
+
logger: this.options.schemaLogger,
|
|
754
|
+
schema: this.options.schema
|
|
699
755
|
});
|
|
700
756
|
}
|
|
701
757
|
/** The read-scope predicate on the runs table (undefined when the store is unscoped). */
|
|
702
758
|
scope() {
|
|
703
|
-
return namespaceFilter(this.scopeNamespace);
|
|
759
|
+
return namespaceFilter(this.t, this.scopeNamespace);
|
|
704
760
|
}
|
|
705
761
|
async tryAdmitSingleton(runId, tag, workflow, limit, retryWakeAt) {
|
|
706
762
|
return this.db.transaction(async (tx) => {
|
|
707
|
-
const where = and(eq(
|
|
763
|
+
const where = and(eq(this.t.workflowRuns.workflow, workflow), inArray(this.t.workflowRuns.status, SINGLETON_ACTIVE_STATUSES), hasTag(this.t, tag), this.scope());
|
|
708
764
|
await tx.select({
|
|
709
|
-
id:
|
|
710
|
-
}).from(
|
|
711
|
-
const rows = await tx.select().from(
|
|
765
|
+
id: this.t.workflowRuns.id
|
|
766
|
+
}).from(this.t.workflowRuns).where(where).orderBy(asc(this.t.workflowRuns.id)).for("update");
|
|
767
|
+
const rows = await tx.select().from(this.t.workflowRuns).where(where);
|
|
712
768
|
const runs = rows.map(fromRunRow);
|
|
713
769
|
const tags = singletonAdmissionTags(runs, runId, tag, workflow, limit);
|
|
714
770
|
if (!tags) {
|
|
715
771
|
const patch = singletonAdmissionDenialPatch(runs, runId, tag, workflow, retryWakeAt);
|
|
716
|
-
if (patch) await tx.update(
|
|
772
|
+
if (patch) await tx.update(this.t.workflowRuns).set(toRunPatch(patch)).where(eq(this.t.workflowRuns.id, runId));
|
|
717
773
|
return false;
|
|
718
774
|
}
|
|
719
|
-
await tx.update(
|
|
775
|
+
await tx.update(this.t.workflowRuns).set({
|
|
720
776
|
tags
|
|
721
|
-
}).where(eq(
|
|
777
|
+
}).where(eq(this.t.workflowRuns.id, runId));
|
|
722
778
|
return true;
|
|
723
779
|
});
|
|
724
780
|
}
|
|
725
781
|
async createRun(run) {
|
|
726
782
|
if (normalizeAttributeRows(run.id, run.searchAttributes).length === 0) {
|
|
727
783
|
if (this.hot) {
|
|
728
|
-
await this.hot.query(INSERT_RUN, RUNS.encodeRow(toRunRow(run)));
|
|
729
|
-
await this.hot.query(DELETE_ATTRIBUTES, [
|
|
784
|
+
await this.hot.query(this.s.INSERT_RUN, RUNS.encodeRow(toRunRow(run)));
|
|
785
|
+
await this.hot.query(this.s.DELETE_ATTRIBUTES, [
|
|
730
786
|
run.id
|
|
731
787
|
]);
|
|
732
788
|
return;
|
|
733
789
|
}
|
|
734
|
-
await this.db.insert(
|
|
735
|
-
await this.db.delete(
|
|
790
|
+
await this.db.insert(this.t.workflowRuns).values(toRunRow(run));
|
|
791
|
+
await this.db.delete(this.t.runAttributes).where(eq(this.t.runAttributes.runId, run.id));
|
|
736
792
|
return;
|
|
737
793
|
}
|
|
738
794
|
await this.db.transaction(async (tx) => {
|
|
739
|
-
await tx.insert(
|
|
740
|
-
await reindexAttributes(tx, run.id, run.searchAttributes);
|
|
795
|
+
await tx.insert(this.t.workflowRuns).values(toRunRow(run));
|
|
796
|
+
await reindexAttributes(this.t, tx, run.id, run.searchAttributes);
|
|
741
797
|
});
|
|
742
798
|
}
|
|
743
799
|
async updateRun(runId, inputPatch) {
|
|
@@ -751,12 +807,12 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
751
807
|
await this.hot.query(text2, values);
|
|
752
808
|
return;
|
|
753
809
|
}
|
|
754
|
-
await this.db.update(
|
|
810
|
+
await this.db.update(this.t.workflowRuns).set(row).where(eq(this.t.workflowRuns.id, runId));
|
|
755
811
|
return;
|
|
756
812
|
}
|
|
757
813
|
await this.db.transaction(async (tx) => {
|
|
758
|
-
if (Object.keys(row).length) await tx.update(
|
|
759
|
-
if (reindex) await reindexAttributes(tx, runId, patch.searchAttributes);
|
|
814
|
+
if (Object.keys(row).length) await tx.update(this.t.workflowRuns).set(row).where(eq(this.t.workflowRuns.id, runId));
|
|
815
|
+
if (reindex) await reindexAttributes(this.t, tx, runId, patch.searchAttributes);
|
|
760
816
|
});
|
|
761
817
|
}
|
|
762
818
|
async updateRunIf(runId, expectedStatuses, inputPatch) {
|
|
@@ -764,48 +820,48 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
764
820
|
const patch = inputPatch.status && TERMINAL_RUN_STATUSES.includes(inputPatch.status) ? await clearTerminalSingletonAdmission(runId, inputPatch, (id) => this.withScope({}).getRun(id)) : inputPatch;
|
|
765
821
|
const row = toRunPatch(patch);
|
|
766
822
|
const reindex = "searchAttributes" in patch;
|
|
767
|
-
const matches = and(eq(
|
|
823
|
+
const matches = and(eq(this.t.workflowRuns.id, runId), inArray(this.t.workflowRuns.status, expectedStatuses));
|
|
768
824
|
if (!reindex && Object.keys(row).length) {
|
|
769
825
|
if (this.hot) {
|
|
770
826
|
const { text: text2, values } = this.hotRunUpdate(runId, row, expectedStatuses);
|
|
771
827
|
return (await this.hot.query(text2, values)).length === 1;
|
|
772
828
|
}
|
|
773
|
-
const applied = await this.db.update(
|
|
774
|
-
id:
|
|
829
|
+
const applied = await this.db.update(this.t.workflowRuns).set(row).where(matches).returning({
|
|
830
|
+
id: this.t.workflowRuns.id
|
|
775
831
|
});
|
|
776
832
|
return applied.length === 1;
|
|
777
833
|
}
|
|
778
834
|
return this.db.transaction(async (tx) => {
|
|
779
|
-
const applied = Object.keys(row).length ? (await tx.update(
|
|
780
|
-
id:
|
|
835
|
+
const applied = Object.keys(row).length ? (await tx.update(this.t.workflowRuns).set(row).where(matches).returning({
|
|
836
|
+
id: this.t.workflowRuns.id
|
|
781
837
|
})).length === 1 : (await tx.select({
|
|
782
|
-
id:
|
|
783
|
-
}).from(
|
|
784
|
-
if (applied && reindex) await reindexAttributes(tx, runId, patch.searchAttributes);
|
|
838
|
+
id: this.t.workflowRuns.id
|
|
839
|
+
}).from(this.t.workflowRuns).where(matches).for("update")).length === 1;
|
|
840
|
+
if (applied && reindex) await reindexAttributes(this.t, tx, runId, patch.searchAttributes);
|
|
785
841
|
return applied;
|
|
786
842
|
});
|
|
787
843
|
}
|
|
788
844
|
async getRun(runId) {
|
|
789
845
|
if (this.hot) {
|
|
790
|
-
const rows2 = await this.hot.query(`${SELECT_RUN}${this.hotScope.text} LIMIT 1`, [
|
|
846
|
+
const rows2 = await this.hot.query(`${this.s.SELECT_RUN}${this.hotScope.text} LIMIT 1`, [
|
|
791
847
|
runId,
|
|
792
848
|
...this.hotScope.values
|
|
793
849
|
]);
|
|
794
850
|
return rows2[0] ? fromRunRow(RUNS.decode(rows2[0])) : null;
|
|
795
851
|
}
|
|
796
|
-
const rows = await this.db.select().from(
|
|
852
|
+
const rows = await this.db.select().from(this.t.workflowRuns).where(and(eq(this.t.workflowRuns.id, runId), this.scope())).limit(1);
|
|
797
853
|
return rows[0] ? fromRunRow(rows[0]) : null;
|
|
798
854
|
}
|
|
799
855
|
async getRuns(runIds) {
|
|
800
856
|
if (runIds.length === 0) return [];
|
|
801
|
-
const rows = await this.db.select().from(
|
|
857
|
+
const rows = await this.db.select().from(this.t.workflowRuns).where(and(inArray(this.t.workflowRuns.id, [
|
|
802
858
|
...new Set(runIds)
|
|
803
859
|
]), this.scope()));
|
|
804
860
|
return rows.map(fromRunRow);
|
|
805
861
|
}
|
|
806
862
|
async deleteRun(runId) {
|
|
807
863
|
await this.db.transaction(async (tx) => {
|
|
808
|
-
await deleteRunsCascade(tx, [
|
|
864
|
+
await deleteRunsCascade(this.t, tx, [
|
|
809
865
|
runId
|
|
810
866
|
]);
|
|
811
867
|
});
|
|
@@ -813,7 +869,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
813
869
|
async deleteRuns(runIds) {
|
|
814
870
|
if (runIds.length === 0) return;
|
|
815
871
|
await this.db.transaction(async (tx) => {
|
|
816
|
-
await deleteRunsCascade(tx, runIds);
|
|
872
|
+
await deleteRunsCascade(this.t, tx, runIds);
|
|
817
873
|
});
|
|
818
874
|
}
|
|
819
875
|
async pruneTerminalRuns(policy, nowMs, limit) {
|
|
@@ -830,18 +886,18 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
830
886
|
if (policy.maxAge != null) {
|
|
831
887
|
const cutoff = new Date(nowMs - parseDuration(policy.maxAge));
|
|
832
888
|
const rows = await tx.select({
|
|
833
|
-
id:
|
|
834
|
-
tags:
|
|
835
|
-
}).from(
|
|
889
|
+
id: this.t.workflowRuns.id,
|
|
890
|
+
tags: this.t.workflowRuns.tags
|
|
891
|
+
}).from(this.t.workflowRuns).where(and(...scoped, lt(this.t.workflowRuns.updatedAt, cutoff))).orderBy(asc(this.t.workflowRuns.updatedAt)).limit(limit).for("update", {
|
|
836
892
|
skipLocked: true
|
|
837
893
|
});
|
|
838
894
|
for (const r of rows) if (!retentionKeeps(r.tags)) ids.add(r.id);
|
|
839
895
|
}
|
|
840
896
|
if (policy.maxCount != null && ids.size < limit) {
|
|
841
897
|
const rows = await tx.select({
|
|
842
|
-
id:
|
|
843
|
-
tags:
|
|
844
|
-
}).from(
|
|
898
|
+
id: this.t.workflowRuns.id,
|
|
899
|
+
tags: this.t.workflowRuns.tags
|
|
900
|
+
}).from(this.t.workflowRuns).where(and(...scoped)).orderBy(desc(this.t.workflowRuns.updatedAt), desc(this.t.workflowRuns.id)).limit(limit).offset(policy.maxCount).for("update", {
|
|
845
901
|
skipLocked: true
|
|
846
902
|
});
|
|
847
903
|
for (const r of rows) {
|
|
@@ -853,19 +909,19 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
853
909
|
const idList = [
|
|
854
910
|
...ids
|
|
855
911
|
].slice(0, limit);
|
|
856
|
-
await deleteRunsCascade(tx, idList);
|
|
912
|
+
await deleteRunsCascade(this.t, tx, idList);
|
|
857
913
|
return idList.length;
|
|
858
914
|
});
|
|
859
915
|
}
|
|
860
916
|
async getCheckpoint(runId, seq) {
|
|
861
917
|
if (this.hot) {
|
|
862
|
-
const rows2 = await this.hot.query(SELECT_CHECKPOINT, [
|
|
918
|
+
const rows2 = await this.hot.query(this.s.SELECT_CHECKPOINT, [
|
|
863
919
|
runId,
|
|
864
920
|
seq
|
|
865
921
|
]);
|
|
866
922
|
return rows2[0] ? fromCheckpointRow(CPS.decode(rows2[0])) : null;
|
|
867
923
|
}
|
|
868
|
-
const rows = await this.db.select().from(
|
|
924
|
+
const rows = await this.db.select().from(this.t.stepCheckpoints).where(and(eq(this.t.stepCheckpoints.runId, runId), eq(this.t.stepCheckpoints.seq, seq))).limit(1);
|
|
869
925
|
return rows[0] ? fromCheckpointRow(rows[0]) : null;
|
|
870
926
|
}
|
|
871
927
|
async getCheckpoints(runId, seqs) {
|
|
@@ -874,7 +930,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
874
930
|
];
|
|
875
931
|
const rows = [];
|
|
876
932
|
for (let at = 0; at < unique.length; at += 500) {
|
|
877
|
-
rows.push(...await this.db.select().from(
|
|
933
|
+
rows.push(...await this.db.select().from(this.t.stepCheckpoints).where(and(eq(this.t.stepCheckpoints.runId, runId), inArray(this.t.stepCheckpoints.seq, unique.slice(at, at + 500)))));
|
|
878
934
|
}
|
|
879
935
|
return rows.sort((a, b) => a.seq - b.seq).map(fromCheckpointRow);
|
|
880
936
|
}
|
|
@@ -884,16 +940,16 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
884
940
|
];
|
|
885
941
|
if (this.hot) {
|
|
886
942
|
if (unique.length === 0) return [];
|
|
887
|
-
const rows2 = await this.hot.query(SELECT_CHECKPOINTS_FOR_SETTLE, [
|
|
943
|
+
const rows2 = await this.hot.query(this.s.SELECT_CHECKPOINTS_FOR_SETTLE, [
|
|
888
944
|
runId,
|
|
889
945
|
unique
|
|
890
946
|
]);
|
|
891
947
|
return rows2.map((raw) => fromCheckpointRow(CPS_NO_INPUT.decode(raw)));
|
|
892
948
|
}
|
|
893
|
-
const { input: _input, ...columns } = getTableColumns2(
|
|
949
|
+
const { input: _input, ...columns } = getTableColumns2(this.t.stepCheckpoints);
|
|
894
950
|
const rows = [];
|
|
895
951
|
for (let at = 0; at < unique.length; at += 500) {
|
|
896
|
-
rows.push(...await this.db.select(columns).from(
|
|
952
|
+
rows.push(...await this.db.select(columns).from(this.t.stepCheckpoints).where(and(eq(this.t.stepCheckpoints.runId, runId), inArray(this.t.stepCheckpoints.seq, unique.slice(at, at + 500)))));
|
|
897
953
|
}
|
|
898
954
|
return rows.sort((a, b) => a.seq - b.seq).map((row) => fromCheckpointRow({
|
|
899
955
|
...row,
|
|
@@ -902,29 +958,29 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
902
958
|
}
|
|
903
959
|
async saveCheckpoint(checkpoint) {
|
|
904
960
|
if (this.hot) {
|
|
905
|
-
await this.hot.query(UPSERT_CHECKPOINT, CPS.encodeRow(toCheckpointRow(checkpoint)));
|
|
961
|
+
await this.hot.query(this.s.UPSERT_CHECKPOINT, CPS.encodeRow(toCheckpointRow(checkpoint)));
|
|
906
962
|
return;
|
|
907
963
|
}
|
|
908
|
-
await upsertCheckpoint(this.db, checkpoint);
|
|
964
|
+
await upsertCheckpoint(this.t, this.db, checkpoint);
|
|
909
965
|
}
|
|
910
966
|
async saveCheckpoints(checkpoints) {
|
|
911
967
|
if (checkpoints.length === 0) return;
|
|
912
968
|
await this.db.transaction(async (tx) => {
|
|
913
969
|
for (let at = 0; at < checkpoints.length; at += CHECKPOINT_BATCH_ROWS) {
|
|
914
|
-
await tx.insert(
|
|
970
|
+
await tx.insert(this.t.stepCheckpoints).values(checkpoints.slice(at, at + CHECKPOINT_BATCH_ROWS).map(toCheckpointRow)).onConflictDoUpdate({
|
|
915
971
|
target: [
|
|
916
|
-
|
|
917
|
-
|
|
972
|
+
this.t.stepCheckpoints.runId,
|
|
973
|
+
this.t.stepCheckpoints.seq
|
|
918
974
|
],
|
|
919
|
-
set: excludedSet(
|
|
975
|
+
set: excludedSet(this.t.stepCheckpoints)
|
|
920
976
|
});
|
|
921
977
|
}
|
|
922
978
|
});
|
|
923
979
|
}
|
|
924
980
|
async saveCheckpointIfPending(checkpoint) {
|
|
925
981
|
const { runId, seq, ...set } = toCheckpointRow(checkpoint);
|
|
926
|
-
const rows = await this.db.update(
|
|
927
|
-
seq:
|
|
982
|
+
const rows = await this.db.update(this.t.stepCheckpoints).set(set).where(and(eq(this.t.stepCheckpoints.runId, runId), eq(this.t.stepCheckpoints.seq, seq), eq(this.t.stepCheckpoints.status, "pending"))).returning({
|
|
983
|
+
seq: this.t.stepCheckpoints.seq
|
|
928
984
|
});
|
|
929
985
|
return rows.length === 1;
|
|
930
986
|
}
|
|
@@ -935,7 +991,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
935
991
|
for (const round of distinctRounds(checkpoints)) {
|
|
936
992
|
for (let at = 0; at < round.length; at += CHECKPOINT_BATCH_ROWS) {
|
|
937
993
|
const chunk = round.slice(at, at + CHECKPOINT_BATCH_ROWS);
|
|
938
|
-
const updated = rowsOf(await tx.execute(updateIfPendingFrom(chunk.map((i) => checkpoints[i]))));
|
|
994
|
+
const updated = rowsOf(await tx.execute(updateIfPendingFrom(this.t, chunk.map((i) => checkpoints[i]))));
|
|
939
995
|
const byKey = new Map(chunk.map((i) => [
|
|
940
996
|
rowKey(checkpoints[i]),
|
|
941
997
|
i
|
|
@@ -959,7 +1015,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
959
1015
|
for (const round of distinctRounds(settlements)) {
|
|
960
1016
|
for (let at = 0; at < round.length; at += CHECKPOINT_BATCH_ROWS) {
|
|
961
1017
|
const chunk = round.slice(at, at + CHECKPOINT_BATCH_ROWS);
|
|
962
|
-
const updated = rowsOf(await tx.execute(settleIfPendingFrom(chunk.map((i) => settlements[i]))));
|
|
1018
|
+
const updated = rowsOf(await tx.execute(settleIfPendingFrom(this.t, chunk.map((i) => settlements[i]))));
|
|
963
1019
|
const byKey = new Map(chunk.map((i) => [
|
|
964
1020
|
rowKey(settlements[i]),
|
|
965
1021
|
i
|
|
@@ -989,7 +1045,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
989
1045
|
});
|
|
990
1046
|
if (sent.length === 0) return out;
|
|
991
1047
|
if (this.hot && sent.length <= HOT_SETTLE_MAX_ROWS) {
|
|
992
|
-
const rows = await this.hot.query(settleResultsText(sent.length), sent.flatMap((i) => settleResultValues(results[i])));
|
|
1048
|
+
const rows = await this.hot.query(this.s.settleResultsText(sent.length), sent.flatMap((i) => settleResultValues(results[i])));
|
|
993
1049
|
const byKey = new Map(sent.map((i) => [
|
|
994
1050
|
rowKey(results[i]),
|
|
995
1051
|
i
|
|
@@ -1004,7 +1060,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1004
1060
|
await this.inTransactionIf(sent.length <= CHECKPOINT_BATCH_ROWS, async (tx) => {
|
|
1005
1061
|
for (let at = 0; at < sent.length; at += CHECKPOINT_BATCH_ROWS) {
|
|
1006
1062
|
const chunk = sent.slice(at, at + CHECKPOINT_BATCH_ROWS);
|
|
1007
|
-
const rows = rowsOf(await tx.execute(settleResultsFrom(chunk.map((i) => results[i]))));
|
|
1063
|
+
const rows = rowsOf(await tx.execute(settleResultsFrom(this.t, chunk.map((i) => results[i]))));
|
|
1008
1064
|
const byKey = new Map(chunk.map((i) => [
|
|
1009
1065
|
rowKey(results[i]),
|
|
1010
1066
|
i
|
|
@@ -1021,21 +1077,21 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1021
1077
|
async insertCheckpointIfAbsent(checkpoint) {
|
|
1022
1078
|
if (this.hot) {
|
|
1023
1079
|
const values = CPS.encodeRow(toCheckpointRow(checkpoint));
|
|
1024
|
-
return (await this.hot.query(INSERT_CHECKPOINT_IF_ABSENT, values)).length === 1;
|
|
1080
|
+
return (await this.hot.query(this.s.INSERT_CHECKPOINT_IF_ABSENT, values)).length === 1;
|
|
1025
1081
|
}
|
|
1026
|
-
const rows = await this.db.insert(
|
|
1082
|
+
const rows = await this.db.insert(this.t.stepCheckpoints).values(toCheckpointRow(checkpoint)).onConflictDoNothing({
|
|
1027
1083
|
target: [
|
|
1028
|
-
|
|
1029
|
-
|
|
1084
|
+
this.t.stepCheckpoints.runId,
|
|
1085
|
+
this.t.stepCheckpoints.seq
|
|
1030
1086
|
]
|
|
1031
1087
|
}).returning({
|
|
1032
|
-
seq:
|
|
1088
|
+
seq: this.t.stepCheckpoints.seq
|
|
1033
1089
|
});
|
|
1034
1090
|
return rows.length === 1;
|
|
1035
1091
|
}
|
|
1036
1092
|
async tryLockRunAndGet(runId, owner, leaseUntilMs, nowMs) {
|
|
1037
1093
|
if (this.hot) {
|
|
1038
|
-
const rows2 = await this.hot.query(`${LOCK_RUN}${this.hotScope.text.replace(/\$(\d+)/g, (_, n) => `$${Number(n) + 3}`)} RETURNING ${RUNS.list}`, [
|
|
1094
|
+
const rows2 = await this.hot.query(`${this.s.LOCK_RUN}${this.hotScope.text.replace(/\$(\d+)/g, (_, n) => `$${Number(n) + 3}`)} RETURNING ${RUNS.list}`, [
|
|
1039
1095
|
runId,
|
|
1040
1096
|
owner,
|
|
1041
1097
|
lockTime(leaseUntilMs),
|
|
@@ -1044,10 +1100,10 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1044
1100
|
]);
|
|
1045
1101
|
return rows2[0] ? fromRunRow(RUNS.decode(rows2[0])) : null;
|
|
1046
1102
|
}
|
|
1047
|
-
const rows = await this.db.update(
|
|
1103
|
+
const rows = await this.db.update(this.t.workflowRuns).set({
|
|
1048
1104
|
lockedBy: owner,
|
|
1049
1105
|
lockedUntil: new Date(leaseUntilMs)
|
|
1050
|
-
}).where(and(eq(
|
|
1106
|
+
}).where(and(eq(this.t.workflowRuns.id, runId), or(isNull(this.t.workflowRuns.lockedUntil), lte(this.t.workflowRuns.lockedUntil, new Date(nowMs))), this.scope())).returning();
|
|
1051
1107
|
return rows[0] ? fromRunRow(rows[0]) : null;
|
|
1052
1108
|
}
|
|
1053
1109
|
/** Run `work` on the db as is when it is one statement (atomic by itself), else in a transaction. */
|
|
@@ -1057,69 +1113,69 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1057
1113
|
async transaction(work) {
|
|
1058
1114
|
return this.db.transaction(async (tx) => work({
|
|
1059
1115
|
raw: tx,
|
|
1060
|
-
saveCheckpoint: /* @__PURE__ */ __name((cp) => upsertCheckpoint(tx, cp), "saveCheckpoint")
|
|
1116
|
+
saveCheckpoint: /* @__PURE__ */ __name((cp) => upsertCheckpoint(this.t, tx, cp), "saveCheckpoint")
|
|
1061
1117
|
}));
|
|
1062
1118
|
}
|
|
1063
1119
|
/** Crash recovery. Namespace-scoped: a worker re-drives only its own tenant's runs. */
|
|
1064
1120
|
async listIncompleteRuns(namespace) {
|
|
1065
|
-
const rows = await withTenantColumnsHint("pg", this.db.select().from(
|
|
1121
|
+
const rows = await withTenantColumnsHint("pg", this.db.select().from(this.t.workflowRuns).where(and(inArray(this.t.workflowRuns.status, [
|
|
1066
1122
|
"running",
|
|
1067
1123
|
"cancelling"
|
|
1068
|
-
]), namespaceFilter(namespace), this.scope())));
|
|
1124
|
+
]), namespaceFilter(this.t, namespace), this.scope())));
|
|
1069
1125
|
return rows.map(fromRunRow);
|
|
1070
1126
|
}
|
|
1071
1127
|
async listOrphanedRuns(nowMs, limit, namespace, ownedBy) {
|
|
1072
1128
|
if (limit <= 0) return [];
|
|
1073
|
-
const query = this.db.select().from(
|
|
1129
|
+
const query = this.db.select().from(this.t.workflowRuns).where(and(inArray(this.t.workflowRuns.status, [
|
|
1074
1130
|
"running",
|
|
1075
1131
|
"cancelling"
|
|
1076
1132
|
]), or(
|
|
1077
|
-
isNull(
|
|
1078
|
-
lte(
|
|
1133
|
+
isNull(this.t.workflowRuns.lockedUntil),
|
|
1134
|
+
lte(this.t.workflowRuns.lockedUntil, new Date(nowMs)),
|
|
1079
1135
|
// A live lease left by a previous process with the caller's own instance id.
|
|
1080
|
-
ownedBy ? and(eq(
|
|
1081
|
-
), namespaceFilter(namespace), this.scope())).orderBy(asc(
|
|
1136
|
+
ownedBy ? and(eq(this.t.workflowRuns.lockedBy, ownedBy.owner), lt(this.t.workflowRuns.lockedUntil, new Date(ownedBy.lockedBefore))) : void 0
|
|
1137
|
+
), namespaceFilter(this.t, namespace), this.scope())).orderBy(asc(this.t.workflowRuns.createdAt)).limit(limit);
|
|
1082
1138
|
const rows = await withTenantColumnsHint("pg", query);
|
|
1083
1139
|
return rows.map(fromRunRow);
|
|
1084
1140
|
}
|
|
1085
1141
|
/** FIFO pick-up, served by `durable_workflow_runs_namespace_status_idx`. The engine then leases
|
|
1086
1142
|
* each run with {@link tryLockRun}; the listing itself takes no locks (it runs outside a tx). */
|
|
1087
1143
|
async listPendingRuns(limit, namespace) {
|
|
1088
|
-
const rows = await withTenantColumnsHint("pg", this.db.select().from(
|
|
1144
|
+
const rows = await withTenantColumnsHint("pg", this.db.select().from(this.t.workflowRuns).where(and(eq(this.t.workflowRuns.status, "pending"), namespaceFilter(this.t, namespace), this.scope())).orderBy(asc(this.t.workflowRuns.createdAt)).limit(limit));
|
|
1089
1145
|
return rows.map(fromRunRow);
|
|
1090
1146
|
}
|
|
1091
1147
|
async claimPendingRuns(limit, owner, leaseUntilMs, nowMs, namespace) {
|
|
1092
1148
|
if (limit <= 0) return [];
|
|
1093
|
-
const filters = and(eq(
|
|
1149
|
+
const filters = and(eq(this.t.workflowRuns.status, "pending"), or(isNull(this.t.workflowRuns.lockedUntil), lte(this.t.workflowRuns.lockedUntil, new Date(nowMs))), namespaceFilter(this.t, namespace), this.scope());
|
|
1094
1150
|
const returning = sql3.join(RUNS.columns.map(([, column]) => sql3`r.${sql3.identifier(column.name)}`), sql3`, `);
|
|
1095
1151
|
const rows = rowsOf(
|
|
1096
1152
|
// A CTE, MATERIALIZED, picks the rows once. As a subquery in UPDATE ... FROM, Postgres may
|
|
1097
1153
|
// rescan it, and each rescan skips the rows this statement already updated and takes the
|
|
1098
1154
|
// next ones, so a claim of `limit` rows could lease every free pending run.
|
|
1099
1155
|
await withTenantColumnsHint("pg", this.db.execute(sql3`WITH c AS MATERIALIZED (
|
|
1100
|
-
SELECT ${
|
|
1156
|
+
SELECT ${this.t.workflowRuns.id} AS claimed_id FROM ${this.t.workflowRuns}
|
|
1101
1157
|
WHERE ${filters}
|
|
1102
|
-
ORDER BY ${
|
|
1158
|
+
ORDER BY ${this.t.workflowRuns.createdAt}, ${this.t.workflowRuns.id}
|
|
1103
1159
|
LIMIT ${limit}
|
|
1104
1160
|
FOR UPDATE SKIP LOCKED
|
|
1105
1161
|
)
|
|
1106
|
-
UPDATE ${
|
|
1107
|
-
SET ${sql3.identifier(getTableColumns2(
|
|
1108
|
-
${sql3.identifier(getTableColumns2(
|
|
1162
|
+
UPDATE ${this.t.workflowRuns} AS r
|
|
1163
|
+
SET ${sql3.identifier(getTableColumns2(this.t.workflowRuns).lockedBy.name)} = ${owner},
|
|
1164
|
+
${sql3.identifier(getTableColumns2(this.t.workflowRuns).lockedUntil.name)} = ${new Date(leaseUntilMs).toISOString()}::timestamptz
|
|
1109
1165
|
FROM c
|
|
1110
|
-
WHERE r.${sql3.identifier(getTableColumns2(
|
|
1166
|
+
WHERE r.${sql3.identifier(getTableColumns2(this.t.workflowRuns).id.name)} = c.claimed_id
|
|
1111
1167
|
RETURNING ${returning}`))
|
|
1112
1168
|
);
|
|
1113
1169
|
return rows.map((raw) => fromRunRow(RUNS.decode(raw))).sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id));
|
|
1114
1170
|
}
|
|
1115
1171
|
async listDueTimers(nowMs, namespace, limit) {
|
|
1116
|
-
const query = this.db.select().from(
|
|
1172
|
+
const query = this.db.select().from(this.t.workflowRuns).where(and(eq(this.t.workflowRuns.status, "suspended"), isNotNull(this.t.workflowRuns.wakeAt), lte(this.t.workflowRuns.wakeAt, new Date(nowMs)), namespaceFilter(this.t, namespace), this.scope())).orderBy(asc(this.t.workflowRuns.wakeAt));
|
|
1117
1173
|
const rows = await withTenantColumnsHint("pg", limit !== void 0 ? query.limit(limit) : query);
|
|
1118
1174
|
return rows.map(fromRunRow);
|
|
1119
1175
|
}
|
|
1120
1176
|
async tryLockRun(runId, owner, leaseUntilMs, nowMs) {
|
|
1121
1177
|
if (this.hot) {
|
|
1122
|
-
const rows2 = await this.hot.query(`${LOCK_RUN} RETURNING "id"`, [
|
|
1178
|
+
const rows2 = await this.hot.query(`${this.s.LOCK_RUN} RETURNING "id"`, [
|
|
1123
1179
|
runId,
|
|
1124
1180
|
owner,
|
|
1125
1181
|
lockTime(leaseUntilMs),
|
|
@@ -1127,42 +1183,42 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1127
1183
|
]);
|
|
1128
1184
|
return rows2.length === 1;
|
|
1129
1185
|
}
|
|
1130
|
-
const rows = await this.db.update(
|
|
1186
|
+
const rows = await this.db.update(this.t.workflowRuns).set({
|
|
1131
1187
|
lockedBy: owner,
|
|
1132
1188
|
lockedUntil: new Date(leaseUntilMs)
|
|
1133
|
-
}).where(and(eq(
|
|
1134
|
-
id:
|
|
1189
|
+
}).where(and(eq(this.t.workflowRuns.id, runId), or(isNull(this.t.workflowRuns.lockedUntil), lte(this.t.workflowRuns.lockedUntil, new Date(nowMs))))).returning({
|
|
1190
|
+
id: this.t.workflowRuns.id
|
|
1135
1191
|
});
|
|
1136
1192
|
return rows.length === 1;
|
|
1137
1193
|
}
|
|
1138
1194
|
async releaseRunLock(runId, owner) {
|
|
1139
1195
|
if (this.hot) {
|
|
1140
|
-
await (owner === void 0 ? this.hot.query(RELEASE_LOCK, [
|
|
1196
|
+
await (owner === void 0 ? this.hot.query(this.s.RELEASE_LOCK, [
|
|
1141
1197
|
runId
|
|
1142
|
-
]) : this.hot.query(`${RELEASE_LOCK} AND "locked_by" = $2`, [
|
|
1198
|
+
]) : this.hot.query(`${this.s.RELEASE_LOCK} AND "locked_by" = $2`, [
|
|
1143
1199
|
runId,
|
|
1144
1200
|
owner
|
|
1145
1201
|
]));
|
|
1146
1202
|
return;
|
|
1147
1203
|
}
|
|
1148
|
-
await this.db.update(
|
|
1204
|
+
await this.db.update(this.t.workflowRuns).set({
|
|
1149
1205
|
lockedBy: null,
|
|
1150
1206
|
lockedUntil: null
|
|
1151
|
-
}).where(owner === void 0 ? eq(
|
|
1207
|
+
}).where(owner === void 0 ? eq(this.t.workflowRuns.id, runId) : and(eq(this.t.workflowRuns.id, runId), eq(this.t.workflowRuns.lockedBy, owner)));
|
|
1152
1208
|
}
|
|
1153
1209
|
async renewRunLock(runId, owner, leaseUntilMs) {
|
|
1154
1210
|
if (this.hot) {
|
|
1155
|
-
const rows2 = await this.hot.query(RENEW_LOCK, [
|
|
1211
|
+
const rows2 = await this.hot.query(this.s.RENEW_LOCK, [
|
|
1156
1212
|
runId,
|
|
1157
1213
|
owner,
|
|
1158
1214
|
lockTime(leaseUntilMs)
|
|
1159
1215
|
]);
|
|
1160
1216
|
return rows2.length === 1;
|
|
1161
1217
|
}
|
|
1162
|
-
const rows = await this.db.update(
|
|
1218
|
+
const rows = await this.db.update(this.t.workflowRuns).set({
|
|
1163
1219
|
lockedUntil: new Date(leaseUntilMs)
|
|
1164
|
-
}).where(and(eq(
|
|
1165
|
-
id:
|
|
1220
|
+
}).where(and(eq(this.t.workflowRuns.id, runId), eq(this.t.workflowRuns.lockedBy, owner))).returning({
|
|
1221
|
+
id: this.t.workflowRuns.id
|
|
1166
1222
|
});
|
|
1167
1223
|
return rows.length === 1;
|
|
1168
1224
|
}
|
|
@@ -1170,30 +1226,30 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1170
1226
|
runFilters(query) {
|
|
1171
1227
|
return [
|
|
1172
1228
|
this.scope(),
|
|
1173
|
-
query.workflow ? eq(
|
|
1174
|
-
query.workflows ? query.workflows.length ? inArray(
|
|
1175
|
-
query.excludeWorkflows?.length ? notInArray(
|
|
1176
|
-
query.updatedAfter !== void 0 ? gte(
|
|
1229
|
+
query.workflow ? eq(this.t.workflowRuns.workflow, query.workflow) : void 0,
|
|
1230
|
+
query.workflows ? query.workflows.length ? inArray(this.t.workflowRuns.workflow, query.workflows) : sql3`1 = 0` : void 0,
|
|
1231
|
+
query.excludeWorkflows?.length ? notInArray(this.t.workflowRuns.workflow, query.excludeWorkflows) : void 0,
|
|
1232
|
+
query.updatedAfter !== void 0 ? gte(this.t.workflowRuns.updatedAt, new Date(query.updatedAfter)) : void 0,
|
|
1177
1233
|
// The tenant boundary. Also the engine's execution-timeout sweep path
|
|
1178
1234
|
// (`listRuns({ workflow, status, namespace })`) — unscoped, it would cancel another tenant's runs.
|
|
1179
|
-
namespaceFilter(query.namespace),
|
|
1180
|
-
query.namespaces ? query.namespaces.length ? inArray(
|
|
1235
|
+
namespaceFilter(this.t, query.namespace),
|
|
1236
|
+
query.namespaces ? query.namespaces.length ? inArray(this.t.workflowRuns.namespace, query.namespaces) : sql3`1 = 0` : void 0,
|
|
1181
1237
|
// `null` selects the unattributed bucket; a value never matches a NULL origin.
|
|
1182
|
-
query.origin === null ? isNull(
|
|
1183
|
-
query.origins ? anyOf(query.origins.map((o) => o === null ? isNull(
|
|
1238
|
+
query.origin === null ? isNull(this.t.workflowRuns.origin) : query.origin !== void 0 ? eq(this.t.workflowRuns.origin, query.origin) : void 0,
|
|
1239
|
+
query.origins ? anyOf(query.origins.map((o) => o === null ? isNull(this.t.workflowRuns.origin) : eq(this.t.workflowRuns.origin, o))) : void 0,
|
|
1184
1240
|
// The customer (tenancy matching rule): `null` = runs with no tenant.
|
|
1185
|
-
...tenantFilters(
|
|
1186
|
-
query.status ? eq(
|
|
1187
|
-
query.statuses ? query.statuses.length ? inArray(
|
|
1188
|
-
query.tag ? hasTag(query.tag) : void 0,
|
|
1189
|
-
query.tags ? anyOf(query.tags.map(hasTag)) : void 0,
|
|
1241
|
+
...tenantFilters(this.t.workflowRuns.tenantId, query),
|
|
1242
|
+
query.status ? eq(this.t.workflowRuns.status, query.status) : void 0,
|
|
1243
|
+
query.statuses ? query.statuses.length ? inArray(this.t.workflowRuns.status, query.statuses) : sql3`1 = 0` : void 0,
|
|
1244
|
+
query.tag ? hasTag(this.t, query.tag) : void 0,
|
|
1245
|
+
query.tags ? anyOf(query.tags.map((tag) => hasTag(this.t, tag))) : void 0,
|
|
1190
1246
|
// One EXISTS per attribute predicate against the normalized side table (ANDed).
|
|
1191
1247
|
...query.attributes?.map((f) => this.attributeExists(f)) ?? []
|
|
1192
1248
|
].filter((f) => f !== void 0);
|
|
1193
1249
|
}
|
|
1194
1250
|
async listRuns(query) {
|
|
1195
1251
|
const filters = this.runFilters(query);
|
|
1196
|
-
const base = this.db.select().from(
|
|
1252
|
+
const base = this.db.select().from(this.t.workflowRuns).where(filters.length ? and(...filters) : void 0).orderBy(desc(this.t.workflowRuns.createdAt), desc(this.t.workflowRuns.id)).$dynamic();
|
|
1197
1253
|
if (query.limit != null) base.limit(query.limit);
|
|
1198
1254
|
if (query.offset != null) base.offset(query.offset);
|
|
1199
1255
|
const rows = await base;
|
|
@@ -1203,16 +1259,16 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1203
1259
|
const filters = this.runFilters(query);
|
|
1204
1260
|
const rows = await this.db.select({
|
|
1205
1261
|
count: sql3`count(*)`.mapWith(Number)
|
|
1206
|
-
}).from(
|
|
1262
|
+
}).from(this.t.workflowRuns).where(filters.length ? and(...filters) : void 0);
|
|
1207
1263
|
return rows[0]?.count ?? 0;
|
|
1208
1264
|
}
|
|
1209
1265
|
async runFacets(query) {
|
|
1210
1266
|
const filters = this.runFilters(query);
|
|
1211
1267
|
const rows = await this.db.select({
|
|
1212
|
-
status:
|
|
1213
|
-
origin:
|
|
1268
|
+
status: this.t.workflowRuns.status,
|
|
1269
|
+
origin: this.t.workflowRuns.origin,
|
|
1214
1270
|
count: sql3`count(*)`.mapWith(Number)
|
|
1215
|
-
}).from(
|
|
1271
|
+
}).from(this.t.workflowRuns).where(filters.length ? and(...filters) : void 0).groupBy(this.t.workflowRuns.status, this.t.workflowRuns.origin);
|
|
1216
1272
|
return mergeRunFacetRows(rows.map((r) => ({
|
|
1217
1273
|
status: r.status,
|
|
1218
1274
|
origin: r.origin,
|
|
@@ -1229,12 +1285,12 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1229
1285
|
});
|
|
1230
1286
|
return runValueFacetsFromRuns(runs, axis, opts);
|
|
1231
1287
|
}
|
|
1232
|
-
const column =
|
|
1288
|
+
const column = this.t.workflowRuns[axis.field];
|
|
1233
1289
|
const filters = this.runFilters(query);
|
|
1234
1290
|
const rows = await this.db.select({
|
|
1235
1291
|
value: column,
|
|
1236
1292
|
count: sql3`count(*)`.mapWith(Number)
|
|
1237
|
-
}).from(
|
|
1293
|
+
}).from(this.t.workflowRuns).where(filters.length ? and(...filters) : void 0).groupBy(column);
|
|
1238
1294
|
return mergeRunValueFacetRows(rows.map((r) => ({
|
|
1239
1295
|
value: r.value ?? null,
|
|
1240
1296
|
count: r.count
|
|
@@ -1244,59 +1300,59 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1244
1300
|
* excludes runs lacking the key (EXISTS requires the row). Numbers compare `num_value`, strings
|
|
1245
1301
|
* and booleans `str_value`; an `in` set ORs its members inside the one EXISTS. */
|
|
1246
1302
|
attributeExists(f) {
|
|
1247
|
-
const comparisons = attributePredicateOperands(f).map(({ column, comparator, operand }) => sql3`${column === "numValue" ?
|
|
1303
|
+
const comparisons = attributePredicateOperands(f).map(({ column, comparator, operand }) => sql3`${column === "numValue" ? this.t.runAttributes.numValue : this.t.runAttributes.strValue} ${sql3.raw(comparator)} ${operand}`);
|
|
1248
1304
|
return exists(this.db.select({
|
|
1249
1305
|
one: sql3`1`
|
|
1250
|
-
}).from(
|
|
1306
|
+
}).from(this.t.runAttributes).where(and(eq(this.t.runAttributes.runId, this.t.workflowRuns.id), eq(this.t.runAttributes.key, f.key), anyOf(comparisons))));
|
|
1251
1307
|
}
|
|
1252
1308
|
// ---- persisted schedules ----------------------------------------------------------------------
|
|
1253
1309
|
async saveSchedule(record) {
|
|
1254
1310
|
const row = toScheduleRow(record);
|
|
1255
|
-
await this.db.insert(
|
|
1256
|
-
target:
|
|
1311
|
+
await this.db.insert(this.t.schedules).values(row).onConflictDoUpdate({
|
|
1312
|
+
target: this.t.schedules.id,
|
|
1257
1313
|
set: row
|
|
1258
1314
|
});
|
|
1259
1315
|
}
|
|
1260
1316
|
async getSchedule(id) {
|
|
1261
|
-
const rows = await this.db.select().from(
|
|
1317
|
+
const rows = await this.db.select().from(this.t.schedules).where(eq(this.t.schedules.id, id)).limit(1);
|
|
1262
1318
|
return rows[0] ? fromScheduleRow(rows[0]) : null;
|
|
1263
1319
|
}
|
|
1264
1320
|
/** A single conditional `UPDATE … RETURNING`: the compare-and-set on `next_fire_at` is atomic. */
|
|
1265
1321
|
async updateSchedule(id, patch, expectedNextFireAt) {
|
|
1266
1322
|
const set = toSchedulePatch(patch);
|
|
1267
1323
|
if (!Object.keys(set).length) return await this.getSchedule(id) !== null;
|
|
1268
|
-
const rows = await this.db.update(
|
|
1269
|
-
id:
|
|
1324
|
+
const rows = await this.db.update(this.t.schedules).set(set).where(and(eq(this.t.schedules.id, id), expectedNextFireAt === void 0 ? void 0 : expectedNextFireAt === null ? isNull(this.t.schedules.nextFireAt) : eq(this.t.schedules.nextFireAt, new Date(expectedNextFireAt)))).returning({
|
|
1325
|
+
id: this.t.schedules.id
|
|
1270
1326
|
});
|
|
1271
1327
|
return rows.length > 0;
|
|
1272
1328
|
}
|
|
1273
1329
|
async deleteSchedule(id) {
|
|
1274
|
-
const rows = await this.db.delete(
|
|
1275
|
-
id:
|
|
1330
|
+
const rows = await this.db.delete(this.t.schedules).where(eq(this.t.schedules.id, id)).returning({
|
|
1331
|
+
id: this.t.schedules.id
|
|
1276
1332
|
});
|
|
1277
1333
|
return rows.length > 0;
|
|
1278
1334
|
}
|
|
1279
1335
|
async listSchedules(query) {
|
|
1280
|
-
const base = this.db.select().from(
|
|
1336
|
+
const base = this.db.select().from(this.t.schedules).where(and(query.namespace !== void 0 ? eq(this.t.schedules.namespace, query.namespace) : void 0, query.workflow !== void 0 ? eq(this.t.schedules.workflow, query.workflow) : void 0, query.paused !== void 0 ? eq(this.t.schedules.paused, query.paused) : void 0, query.tag !== void 0 ? sql3`${this.t.schedules.tags} @> ${JSON.stringify([
|
|
1281
1337
|
query.tag
|
|
1282
|
-
])}::jsonb` : void 0, ...tenantFilters(
|
|
1338
|
+
])}::jsonb` : void 0, ...tenantFilters(this.t.schedules.tenantId, query), query.dueBy !== void 0 ? and(eq(this.t.schedules.paused, false), lte(this.t.schedules.nextFireAt, new Date(query.dueBy))) : void 0)).orderBy(sql3`${this.t.schedules.nextFireAt} ASC NULLS LAST`, asc(this.t.schedules.id)).$dynamic();
|
|
1283
1339
|
if (query.limit != null) base.limit(query.limit);
|
|
1284
1340
|
if (query.offset != null) base.offset(query.offset);
|
|
1285
1341
|
return (await withTenantColumnsHint("pg", base)).map(fromScheduleRow);
|
|
1286
1342
|
}
|
|
1287
1343
|
async listCheckpoints(runId) {
|
|
1288
|
-
const rows = await this.db.select().from(
|
|
1344
|
+
const rows = await this.db.select().from(this.t.stepCheckpoints).where(eq(this.t.stepCheckpoints.runId, runId)).orderBy(asc(this.t.stepCheckpoints.seq));
|
|
1289
1345
|
return rows.map(fromCheckpointRow);
|
|
1290
1346
|
}
|
|
1291
1347
|
async listCheckpointsForReplay(runId) {
|
|
1292
1348
|
if (this.hot) {
|
|
1293
|
-
const rows2 = (await this.hot.query(SELECT_REPLAY, [
|
|
1349
|
+
const rows2 = (await this.hot.query(this.s.SELECT_REPLAY, [
|
|
1294
1350
|
runId
|
|
1295
1351
|
])).map((raw) => CPS_NO_INPUT.decode(raw));
|
|
1296
1352
|
const open2 = rows2.filter((row) => row.status !== "completed").map((row) => row.seq);
|
|
1297
1353
|
const inputs2 = /* @__PURE__ */ new Map();
|
|
1298
1354
|
if (open2.length > 0) {
|
|
1299
|
-
for (const raw of await this.hot.query(SELECT_INPUTS, [
|
|
1355
|
+
for (const raw of await this.hot.query(this.s.SELECT_INPUTS, [
|
|
1300
1356
|
runId,
|
|
1301
1357
|
open2
|
|
1302
1358
|
])) {
|
|
@@ -1308,15 +1364,15 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1308
1364
|
input: inputs2.get(row.seq) ?? null
|
|
1309
1365
|
}));
|
|
1310
1366
|
}
|
|
1311
|
-
const { input: _input, ...columns } = getTableColumns2(
|
|
1312
|
-
const rows = await this.db.select(columns).from(
|
|
1367
|
+
const { input: _input, ...columns } = getTableColumns2(this.t.stepCheckpoints);
|
|
1368
|
+
const rows = await this.db.select(columns).from(this.t.stepCheckpoints).where(eq(this.t.stepCheckpoints.runId, runId)).orderBy(asc(this.t.stepCheckpoints.seq));
|
|
1313
1369
|
const open = rows.filter((row) => row.status !== "completed").map((row) => row.seq);
|
|
1314
1370
|
const inputs = /* @__PURE__ */ new Map();
|
|
1315
1371
|
for (let at = 0; at < open.length; at += 500) {
|
|
1316
1372
|
const found = await this.db.select({
|
|
1317
|
-
seq:
|
|
1318
|
-
input:
|
|
1319
|
-
}).from(
|
|
1373
|
+
seq: this.t.stepCheckpoints.seq,
|
|
1374
|
+
input: this.t.stepCheckpoints.input
|
|
1375
|
+
}).from(this.t.stepCheckpoints).where(and(eq(this.t.stepCheckpoints.runId, runId), inArray(this.t.stepCheckpoints.seq, open.slice(at, at + 500))));
|
|
1320
1376
|
for (const row of found) inputs.set(row.seq, row.input);
|
|
1321
1377
|
}
|
|
1322
1378
|
return rows.map((row) => fromCheckpointRow({
|
|
@@ -1325,13 +1381,13 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1325
1381
|
}));
|
|
1326
1382
|
}
|
|
1327
1383
|
async getLatestCheckpointByName(runId, name) {
|
|
1328
|
-
const rows = await this.db.select().from(
|
|
1384
|
+
const rows = await this.db.select().from(this.t.stepCheckpoints).where(and(eq(this.t.stepCheckpoints.runId, runId), eq(this.t.stepCheckpoints.name, name))).orderBy(desc(this.t.stepCheckpoints.seq)).limit(1);
|
|
1329
1385
|
return rows[0] ? fromCheckpointRow(rows[0]) : void 0;
|
|
1330
1386
|
}
|
|
1331
1387
|
async hasCheckpoints(runId, fromSeq, toSeq, statuses) {
|
|
1332
1388
|
if (statuses.length === 0) return false;
|
|
1333
1389
|
if (this.hot) {
|
|
1334
|
-
const rows2 = await this.hot.query(HAS_CHECKPOINTS, [
|
|
1390
|
+
const rows2 = await this.hot.query(this.s.HAS_CHECKPOINTS, [
|
|
1335
1391
|
runId,
|
|
1336
1392
|
fromSeq,
|
|
1337
1393
|
toSeq,
|
|
@@ -1340,14 +1396,14 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1340
1396
|
return rows2.length > 0;
|
|
1341
1397
|
}
|
|
1342
1398
|
const rows = await this.db.select({
|
|
1343
|
-
seq:
|
|
1344
|
-
}).from(
|
|
1399
|
+
seq: this.t.stepCheckpoints.seq
|
|
1400
|
+
}).from(this.t.stepCheckpoints).where(and(eq(this.t.stepCheckpoints.runId, runId), gte(this.t.stepCheckpoints.seq, fromSeq), lte(this.t.stepCheckpoints.seq, toSeq), inArray(this.t.stepCheckpoints.status, statuses))).orderBy(desc(this.t.stepCheckpoints.seq)).limit(1);
|
|
1345
1401
|
return rows.length > 0;
|
|
1346
1402
|
}
|
|
1347
1403
|
async countCheckpoints(runId, fromSeq, toSeq, statuses) {
|
|
1348
1404
|
if (statuses.length === 0) return 0;
|
|
1349
1405
|
if (this.hot) {
|
|
1350
|
-
const rows2 = await this.hot.query(COUNT_CHECKPOINTS, [
|
|
1406
|
+
const rows2 = await this.hot.query(this.s.COUNT_CHECKPOINTS, [
|
|
1351
1407
|
runId,
|
|
1352
1408
|
fromSeq,
|
|
1353
1409
|
toSeq,
|
|
@@ -1357,18 +1413,18 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1357
1413
|
}
|
|
1358
1414
|
const rows = await this.db.select({
|
|
1359
1415
|
count: sql3`count(*)`.mapWith(Number)
|
|
1360
|
-
}).from(
|
|
1416
|
+
}).from(this.t.stepCheckpoints).where(and(eq(this.t.stepCheckpoints.runId, runId), gte(this.t.stepCheckpoints.seq, fromSeq), lte(this.t.stepCheckpoints.seq, toSeq), inArray(this.t.stepCheckpoints.status, statuses)));
|
|
1361
1417
|
return rows[0]?.count ?? 0;
|
|
1362
1418
|
}
|
|
1363
1419
|
async listCheckpointsByNamePrefix(runId, prefixes) {
|
|
1364
1420
|
if (prefixes.length === 0) return [];
|
|
1365
|
-
const rows = await this.db.select().from(
|
|
1421
|
+
const rows = await this.db.select().from(this.t.stepCheckpoints).where(and(eq(this.t.stepCheckpoints.runId, runId), or(...prefixes.map((p) => like(this.t.stepCheckpoints.name, `${escapeLike(p)}%`))))).orderBy(asc(this.t.stepCheckpoints.seq));
|
|
1366
1422
|
return rows.map(fromCheckpointRow);
|
|
1367
1423
|
}
|
|
1368
1424
|
/** {@link StateStore.checkpointStats}: one `GROUP BY parallel_group, status, name_kind` over the
|
|
1369
1425
|
* run's checkpoints, so a wide fan comes back as a few bucket rows instead of every member. */
|
|
1370
1426
|
async checkpointStats(runId, opts) {
|
|
1371
|
-
const c =
|
|
1427
|
+
const c = this.t.stepCheckpoints;
|
|
1372
1428
|
const nameKind = sql3`CASE
|
|
1373
1429
|
WHEN ${c.name} LIKE 'signal:child:%' THEN 'child'
|
|
1374
1430
|
WHEN ${c.name} LIKE 'fan:%' THEN 'folded'
|
|
@@ -1399,7 +1455,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1399
1455
|
* paged there, so reading one page of a wide fan never loads the rest of it. */
|
|
1400
1456
|
async queryCheckpoints(runId, query) {
|
|
1401
1457
|
if (query.statuses?.length === 0 || query.namePrefixes?.length === 0) return [];
|
|
1402
|
-
const c =
|
|
1458
|
+
const c = this.t.stepCheckpoints;
|
|
1403
1459
|
const base = this.db.select().from(c).where(and(
|
|
1404
1460
|
eq(c.runId, runId),
|
|
1405
1461
|
query.group !== void 0 ? eq(c.parallelGroup, query.group) : void 0,
|
|
@@ -1418,77 +1474,77 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1418
1474
|
seq: waiter.seq,
|
|
1419
1475
|
parallelGroup: waiter.parallelGroup ?? null
|
|
1420
1476
|
};
|
|
1421
|
-
await this.db.insert(
|
|
1477
|
+
await this.db.insert(this.t.signalWaiters).values({
|
|
1422
1478
|
token: waiter.token,
|
|
1423
1479
|
...set
|
|
1424
1480
|
}).onConflictDoUpdate({
|
|
1425
|
-
target:
|
|
1481
|
+
target: this.t.signalWaiters.token,
|
|
1426
1482
|
set
|
|
1427
1483
|
});
|
|
1428
1484
|
}
|
|
1429
1485
|
async takeSignalWaiter(token) {
|
|
1430
|
-
const rows = await this.db.delete(
|
|
1486
|
+
const rows = await this.db.delete(this.t.signalWaiters).where(eq(this.t.signalWaiters.token, token)).returning();
|
|
1431
1487
|
const row = rows[0];
|
|
1432
1488
|
return row ? fromWaiterRow(row) : null;
|
|
1433
1489
|
}
|
|
1434
1490
|
async listSignalWaiters(prefix) {
|
|
1435
|
-
const rows = await this.db.select().from(
|
|
1491
|
+
const rows = await this.db.select().from(this.t.signalWaiters).where(like(this.t.signalWaiters.token, `${escapeLike(prefix)}%`));
|
|
1436
1492
|
return rows.map(fromWaiterRow);
|
|
1437
1493
|
}
|
|
1438
1494
|
async registerChildWaits(entries) {
|
|
1439
1495
|
if (entries.length === 0) return;
|
|
1440
1496
|
await this.db.transaction(async (tx) => {
|
|
1441
|
-
await tx.insert(
|
|
1497
|
+
await tx.insert(this.t.signalWaiters).values(entries.map(({ waiter }) => ({
|
|
1442
1498
|
token: waiter.token,
|
|
1443
1499
|
runId: waiter.runId,
|
|
1444
1500
|
seq: waiter.seq,
|
|
1445
1501
|
parallelGroup: waiter.parallelGroup ?? null
|
|
1446
1502
|
}))).onConflictDoUpdate({
|
|
1447
|
-
target:
|
|
1448
|
-
set: excludedSet(
|
|
1503
|
+
target: this.t.signalWaiters.token,
|
|
1504
|
+
set: excludedSet(this.t.signalWaiters, [
|
|
1449
1505
|
"runId",
|
|
1450
1506
|
"seq",
|
|
1451
1507
|
"parallelGroup"
|
|
1452
1508
|
])
|
|
1453
1509
|
});
|
|
1454
|
-
await tx.insert(
|
|
1510
|
+
await tx.insert(this.t.stepCheckpoints).values(entries.map(({ checkpoint }) => toCheckpointRow(checkpoint))).onConflictDoUpdate({
|
|
1455
1511
|
target: [
|
|
1456
|
-
|
|
1457
|
-
|
|
1512
|
+
this.t.stepCheckpoints.runId,
|
|
1513
|
+
this.t.stepCheckpoints.seq
|
|
1458
1514
|
],
|
|
1459
|
-
set: excludedSet(
|
|
1515
|
+
set: excludedSet(this.t.stepCheckpoints)
|
|
1460
1516
|
});
|
|
1461
1517
|
});
|
|
1462
1518
|
}
|
|
1463
1519
|
async compactCheckpoints(runId, fromSeq, toSeq, into) {
|
|
1464
1520
|
await this.db.transaction(async (tx) => {
|
|
1465
|
-
await tx.delete(
|
|
1466
|
-
await upsertCheckpoint(tx, into);
|
|
1521
|
+
await tx.delete(this.t.stepCheckpoints).where(and(eq(this.t.stepCheckpoints.runId, runId), gte(this.t.stepCheckpoints.seq, fromSeq), lte(this.t.stepCheckpoints.seq, toSeq)));
|
|
1522
|
+
await upsertCheckpoint(this.t, tx, into);
|
|
1467
1523
|
});
|
|
1468
1524
|
}
|
|
1469
1525
|
async listSignalWaitersByRun(runIds) {
|
|
1470
1526
|
if (runIds.length === 0) return [];
|
|
1471
|
-
const rows = await this.db.select().from(
|
|
1527
|
+
const rows = await this.db.select().from(this.t.signalWaiters).where(inArray(this.t.signalWaiters.runId, runIds));
|
|
1472
1528
|
return rows.map(fromWaiterRow);
|
|
1473
1529
|
}
|
|
1474
1530
|
async hasSignalWaiter(runId, tokenPrefix) {
|
|
1475
1531
|
const rows = await this.db.select({
|
|
1476
|
-
token:
|
|
1477
|
-
}).from(
|
|
1532
|
+
token: this.t.signalWaiters.token
|
|
1533
|
+
}).from(this.t.signalWaiters).where(and(eq(this.t.signalWaiters.runId, runId), like(this.t.signalWaiters.token, `${escapeLike(tokenPrefix)}%`))).limit(1);
|
|
1478
1534
|
return rows.length > 0;
|
|
1479
1535
|
}
|
|
1480
1536
|
async removeSignalWaiter(waiter) {
|
|
1481
|
-
await this.db.delete(
|
|
1537
|
+
await this.db.delete(this.t.signalWaiters).where(and(eq(this.t.signalWaiters.token, waiter.token), eq(this.t.signalWaiters.runId, waiter.runId), eq(this.t.signalWaiters.seq, waiter.seq)));
|
|
1482
1538
|
}
|
|
1483
1539
|
async bufferSignal(token, payload) {
|
|
1484
|
-
await this.db.insert(
|
|
1540
|
+
await this.db.insert(this.t.bufferedSignals).values({
|
|
1485
1541
|
token,
|
|
1486
1542
|
payload: payload ?? null
|
|
1487
1543
|
});
|
|
1488
1544
|
}
|
|
1489
1545
|
async takeBufferedSignal(token) {
|
|
1490
1546
|
if (this.hot) {
|
|
1491
|
-
const [row2] = await this.hot.query(TAKE_BUFFERED_SIGNAL, [
|
|
1547
|
+
const [row2] = await this.hot.query(this.s.TAKE_BUFFERED_SIGNAL, [
|
|
1492
1548
|
token
|
|
1493
1549
|
]);
|
|
1494
1550
|
if (!row2) return null;
|
|
@@ -1498,12 +1554,12 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1498
1554
|
};
|
|
1499
1555
|
}
|
|
1500
1556
|
const oldest = this.db.select({
|
|
1501
|
-
id:
|
|
1502
|
-
}).from(
|
|
1557
|
+
id: this.t.bufferedSignals.id
|
|
1558
|
+
}).from(this.t.bufferedSignals).where(eq(this.t.bufferedSignals.token, token)).orderBy(asc(this.t.bufferedSignals.id)).limit(1).for("update", {
|
|
1503
1559
|
skipLocked: true
|
|
1504
1560
|
});
|
|
1505
|
-
const rows = await this.db.delete(
|
|
1506
|
-
payload:
|
|
1561
|
+
const rows = await this.db.delete(this.t.bufferedSignals).where(inArray(this.t.bufferedSignals.id, oldest)).returning({
|
|
1562
|
+
payload: this.t.bufferedSignals.payload
|
|
1507
1563
|
});
|
|
1508
1564
|
const row = rows[0];
|
|
1509
1565
|
return row ? {
|
|
@@ -1513,15 +1569,15 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1513
1569
|
async takeOldestBufferedSignal(tokens) {
|
|
1514
1570
|
if (tokens.length === 0) return null;
|
|
1515
1571
|
const oldest = this.db.select({
|
|
1516
|
-
id:
|
|
1517
|
-
}).from(
|
|
1572
|
+
id: this.t.bufferedSignals.id
|
|
1573
|
+
}).from(this.t.bufferedSignals).where(inArray(this.t.bufferedSignals.token, [
|
|
1518
1574
|
...new Set(tokens)
|
|
1519
|
-
])).orderBy(asc(
|
|
1575
|
+
])).orderBy(asc(this.t.bufferedSignals.id)).limit(1).for("update", {
|
|
1520
1576
|
skipLocked: true
|
|
1521
1577
|
});
|
|
1522
|
-
const rows = await this.db.delete(
|
|
1523
|
-
token:
|
|
1524
|
-
payload:
|
|
1578
|
+
const rows = await this.db.delete(this.t.bufferedSignals).where(inArray(this.t.bufferedSignals.id, oldest)).returning({
|
|
1579
|
+
token: this.t.bufferedSignals.token,
|
|
1580
|
+
payload: this.t.bufferedSignals.payload
|
|
1525
1581
|
});
|
|
1526
1582
|
const row = rows[0];
|
|
1527
1583
|
return row ? {
|
|
@@ -1530,41 +1586,41 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1530
1586
|
} : null;
|
|
1531
1587
|
}
|
|
1532
1588
|
async claimSignalId(token, id, expiresAt, nowMs) {
|
|
1533
|
-
const rows = await this.db.insert(
|
|
1589
|
+
const rows = await this.db.insert(this.t.signalIds).values({
|
|
1534
1590
|
token,
|
|
1535
1591
|
signalId: id,
|
|
1536
1592
|
expiresAt: new Date(expiresAt)
|
|
1537
1593
|
}).onConflictDoUpdate({
|
|
1538
1594
|
target: [
|
|
1539
|
-
|
|
1540
|
-
|
|
1595
|
+
this.t.signalIds.token,
|
|
1596
|
+
this.t.signalIds.signalId
|
|
1541
1597
|
],
|
|
1542
1598
|
set: {
|
|
1543
1599
|
expiresAt: new Date(expiresAt)
|
|
1544
1600
|
},
|
|
1545
|
-
setWhere: lte(
|
|
1601
|
+
setWhere: lte(this.t.signalIds.expiresAt, new Date(nowMs))
|
|
1546
1602
|
}).returning({
|
|
1547
|
-
token:
|
|
1603
|
+
token: this.t.signalIds.token
|
|
1548
1604
|
});
|
|
1549
1605
|
return rows.length > 0;
|
|
1550
1606
|
}
|
|
1551
1607
|
async releaseSignalId(token, id) {
|
|
1552
|
-
await this.db.delete(
|
|
1608
|
+
await this.db.delete(this.t.signalIds).where(and(eq(this.t.signalIds.token, token), eq(this.t.signalIds.signalId, id)));
|
|
1553
1609
|
}
|
|
1554
1610
|
async deleteExpiredSignalIds(nowMs, limit) {
|
|
1555
1611
|
const expired = this.db.select({
|
|
1556
|
-
token:
|
|
1557
|
-
signalId:
|
|
1558
|
-
}).from(
|
|
1612
|
+
token: this.t.signalIds.token,
|
|
1613
|
+
signalId: this.t.signalIds.signalId
|
|
1614
|
+
}).from(this.t.signalIds).where(lte(this.t.signalIds.expiresAt, new Date(nowMs))).limit(limit).for("update", {
|
|
1559
1615
|
skipLocked: true
|
|
1560
1616
|
});
|
|
1561
|
-
const rows = await this.db.delete(
|
|
1562
|
-
token:
|
|
1617
|
+
const rows = await this.db.delete(this.t.signalIds).where(sql3`(${this.t.signalIds.token}, ${this.t.signalIds.signalId}) in (${expired})`).returning({
|
|
1618
|
+
token: this.t.signalIds.token
|
|
1563
1619
|
});
|
|
1564
1620
|
return rows.length;
|
|
1565
1621
|
}
|
|
1566
1622
|
async listBufferedSignals(tokenPrefix, afterId, limit) {
|
|
1567
|
-
const rows = await this.db.select().from(
|
|
1623
|
+
const rows = await this.db.select().from(this.t.bufferedSignals).where(and(like(this.t.bufferedSignals.token, `${escapeLike(tokenPrefix)}%`), afterId !== void 0 ? gt(this.t.bufferedSignals.id, Number(afterId)) : void 0)).orderBy(asc(this.t.bufferedSignals.id)).limit(limit);
|
|
1568
1624
|
return rows.filter((r) => r.token.startsWith(tokenPrefix)).map((r) => ({
|
|
1569
1625
|
id: String(r.id),
|
|
1570
1626
|
token: r.token,
|
|
@@ -1573,10 +1629,10 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1573
1629
|
}
|
|
1574
1630
|
async deleteBufferedSignals(ids) {
|
|
1575
1631
|
if (ids.length === 0) return;
|
|
1576
|
-
await this.db.delete(
|
|
1632
|
+
await this.db.delete(this.t.bufferedSignals).where(inArray(this.t.bufferedSignals.id, ids.map(Number)));
|
|
1577
1633
|
}
|
|
1578
1634
|
async bufferEvent(input) {
|
|
1579
|
-
await this.db.insert(
|
|
1635
|
+
await this.db.insert(this.t.bufferedEvents).values({
|
|
1580
1636
|
id: input.id,
|
|
1581
1637
|
name: input.name,
|
|
1582
1638
|
payload: input.payload ?? null,
|
|
@@ -1585,7 +1641,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1585
1641
|
}
|
|
1586
1642
|
async listBufferedEvents(name, limit) {
|
|
1587
1643
|
if (this.hot) {
|
|
1588
|
-
return (await this.hot.query(LIST_BUFFERED_EVENTS, [
|
|
1644
|
+
return (await this.hot.query(this.s.LIST_BUFFERED_EVENTS, [
|
|
1589
1645
|
name,
|
|
1590
1646
|
limit
|
|
1591
1647
|
])).map((raw) => {
|
|
@@ -1597,7 +1653,7 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1597
1653
|
};
|
|
1598
1654
|
});
|
|
1599
1655
|
}
|
|
1600
|
-
const rows = await this.db.select().from(
|
|
1656
|
+
const rows = await this.db.select().from(this.t.bufferedEvents).where(eq(this.t.bufferedEvents.name, name)).orderBy(asc(this.t.bufferedEvents.publishedAt), asc(this.t.bufferedEvents.id)).limit(limit);
|
|
1601
1657
|
return rows.map((r) => ({
|
|
1602
1658
|
id: r.id,
|
|
1603
1659
|
payload: r.payload ?? void 0,
|
|
@@ -1605,11 +1661,11 @@ var DrizzlePgStateStore = class _DrizzlePgStateStore {
|
|
|
1605
1661
|
}));
|
|
1606
1662
|
}
|
|
1607
1663
|
async removeBufferedEvent(id) {
|
|
1608
|
-
if (this.hot) return (await this.hot.query(REMOVE_BUFFERED_EVENT, [
|
|
1664
|
+
if (this.hot) return (await this.hot.query(this.s.REMOVE_BUFFERED_EVENT, [
|
|
1609
1665
|
id
|
|
1610
1666
|
])).length === 1;
|
|
1611
|
-
const rows = await this.db.delete(
|
|
1612
|
-
id:
|
|
1667
|
+
const rows = await this.db.delete(this.t.bufferedEvents).where(eq(this.t.bufferedEvents.id, id)).returning({
|
|
1668
|
+
id: this.t.bufferedEvents.id
|
|
1613
1669
|
});
|
|
1614
1670
|
return rows.length === 1;
|
|
1615
1671
|
}
|
|
@@ -1626,47 +1682,99 @@ var CPS_NO_INPUT = new TableCodec(durableStepCheckpoints, [
|
|
|
1626
1682
|
]);
|
|
1627
1683
|
var EVENTS = new TableCodec(durableBufferedEvents);
|
|
1628
1684
|
var SIGNAL_PAYLOAD = getTableColumns2(durableBufferedSignals).payload;
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1685
|
+
function buildHotStatements(schema) {
|
|
1686
|
+
const qualify = /* @__PURE__ */ __name((table) => schema === void 0 ? quote(getTableName2(table)) : `${quote(schema)}.${quote(getTableName2(table))}`, "qualify");
|
|
1687
|
+
const T_RUNS = qualify(durableWorkflowRuns);
|
|
1688
|
+
const T_CPS = qualify(durableStepCheckpoints);
|
|
1689
|
+
const T_EVENTS = qualify(durableBufferedEvents);
|
|
1690
|
+
const T_SIGNALS = qualify(durableBufferedSignals);
|
|
1691
|
+
const T_ATTRIBUTES = qualify(durableRunAttributes);
|
|
1692
|
+
const SELECT_RUN = `SELECT ${RUNS.list} FROM ${T_RUNS} WHERE "id" = $1`;
|
|
1693
|
+
const INSERT_RUN = `INSERT INTO ${T_RUNS} (${RUNS.list}) VALUES (${placeholders(1, RUNS.columns.length)})`;
|
|
1694
|
+
const DELETE_ATTRIBUTES = `DELETE FROM ${T_ATTRIBUTES} WHERE "run_id" = $1`;
|
|
1695
|
+
const LOCK_RUN = `UPDATE ${T_RUNS} SET "locked_by" = $2, "locked_until" = $3 WHERE "id" = $1 AND ("locked_until" IS NULL OR "locked_until" <= $4)`;
|
|
1696
|
+
const RELEASE_LOCK = `UPDATE ${T_RUNS} SET "locked_by" = NULL, "locked_until" = NULL WHERE "id" = $1`;
|
|
1697
|
+
const RENEW_LOCK = `UPDATE ${T_RUNS} SET "locked_until" = $3 WHERE "id" = $1 AND "locked_by" = $2 RETURNING "id"`;
|
|
1698
|
+
const SELECT_CHECKPOINT = `SELECT ${CPS.list} FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" = $2 LIMIT 1`;
|
|
1699
|
+
const SELECT_CHECKPOINTS_FOR_SETTLE = `SELECT ${CPS_NO_INPUT.list} FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" = ANY($2::integer[]) ORDER BY "seq"`;
|
|
1700
|
+
const SELECT_REPLAY = `SELECT ${CPS_NO_INPUT.list} FROM ${T_CPS} WHERE "run_id" = $1 ORDER BY "seq"`;
|
|
1701
|
+
const SELECT_INPUTS = `SELECT "seq", "input" FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" = ANY($2::integer[])`;
|
|
1702
|
+
const CHECKPOINT_VALUES = `(${CPS.list}) VALUES (${placeholders(1, CPS.columns.length)})`;
|
|
1703
|
+
const UPSERT_CHECKPOINT = `INSERT INTO ${T_CPS} ${CHECKPOINT_VALUES} ON CONFLICT ("run_id", "seq") DO UPDATE SET ${CPS.columns.map(([, c]) => `${quote(c.name)} = EXCLUDED.${quote(c.name)}`).join(", ")}`;
|
|
1704
|
+
const INSERT_CHECKPOINT_IF_ABSENT = `INSERT INTO ${T_CPS} ${CHECKPOINT_VALUES} ON CONFLICT ("run_id", "seq") DO NOTHING RETURNING "seq"`;
|
|
1705
|
+
const CHECKPOINT_RANGE = `FROM ${T_CPS} WHERE "run_id" = $1 AND "seq" >= $2 AND "seq" <= $3 AND "status" = ANY($4::text[])`;
|
|
1706
|
+
const HAS_CHECKPOINTS = `SELECT "seq" ${CHECKPOINT_RANGE} ORDER BY "seq" DESC LIMIT 1`;
|
|
1707
|
+
const COUNT_CHECKPOINTS = `SELECT count(*)::integer AS "n" ${CHECKPOINT_RANGE}`;
|
|
1708
|
+
const LIST_BUFFERED_EVENTS = `SELECT ${EVENTS.list} FROM ${T_EVENTS} WHERE "name" = $1 ORDER BY "published_at", "id" LIMIT $2`;
|
|
1709
|
+
const REMOVE_BUFFERED_EVENT = `DELETE FROM ${T_EVENTS} WHERE "id" = $1 RETURNING "id"`;
|
|
1710
|
+
const TAKE_BUFFERED_SIGNAL = `WITH taken AS MATERIALIZED (SELECT "id" FROM ${T_SIGNALS} WHERE "token" = $1 ORDER BY "id" LIMIT 1 FOR UPDATE SKIP LOCKED) DELETE FROM ${T_SIGNALS} WHERE "id" IN (SELECT "id" FROM taken) RETURNING "payload"`;
|
|
1711
|
+
const settleTexts = /* @__PURE__ */ new Map();
|
|
1712
|
+
const settleResultsText = /* @__PURE__ */ __name((n) => {
|
|
1713
|
+
let text2 = settleTexts.get(n);
|
|
1714
|
+
if (text2 !== void 0) return text2;
|
|
1715
|
+
const rows = Array.from({
|
|
1716
|
+
length: n
|
|
1717
|
+
}, (_, r) => {
|
|
1718
|
+
const p = /* @__PURE__ */ __name((k) => `$${r * 8 + k}`, "p");
|
|
1719
|
+
return `(${p(1)}::text, ${p(2)}::integer, ${p(3)}::text, ${p(4)}::jsonb, ${p(5)}::jsonb, ${p(6)}::jsonb, ${p(7)}::timestamptz, ${p(8)}::timestamptz)`;
|
|
1720
|
+
});
|
|
1721
|
+
text2 = `UPDATE ${T_CPS} 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 NOT EXISTS (SELECT 1 FROM ${T_RUNS} AS r WHERE r.id = c.run_id AND r.status IN ('cancelled', 'completed')) RETURNING ${CPS_NO_INPUT.columns.map(([, c]) => `c.${quote(c.name)}`).join(", ")}`;
|
|
1722
|
+
settleTexts.set(n, text2);
|
|
1723
|
+
return text2;
|
|
1724
|
+
}, "settleResultsText");
|
|
1725
|
+
return {
|
|
1726
|
+
T_RUNS,
|
|
1727
|
+
T_CPS,
|
|
1728
|
+
T_EVENTS,
|
|
1729
|
+
T_SIGNALS,
|
|
1730
|
+
T_ATTRIBUTES,
|
|
1731
|
+
SELECT_RUN,
|
|
1732
|
+
INSERT_RUN,
|
|
1733
|
+
DELETE_ATTRIBUTES,
|
|
1734
|
+
LOCK_RUN,
|
|
1735
|
+
RELEASE_LOCK,
|
|
1736
|
+
RENEW_LOCK,
|
|
1737
|
+
SELECT_CHECKPOINT,
|
|
1738
|
+
SELECT_CHECKPOINTS_FOR_SETTLE,
|
|
1739
|
+
SELECT_REPLAY,
|
|
1740
|
+
SELECT_INPUTS,
|
|
1741
|
+
UPSERT_CHECKPOINT,
|
|
1742
|
+
INSERT_CHECKPOINT_IF_ABSENT,
|
|
1743
|
+
HAS_CHECKPOINTS,
|
|
1744
|
+
COUNT_CHECKPOINTS,
|
|
1745
|
+
LIST_BUFFERED_EVENTS,
|
|
1746
|
+
REMOVE_BUFFERED_EVENT,
|
|
1747
|
+
TAKE_BUFFERED_SIGNAL,
|
|
1748
|
+
settleResultsText
|
|
1749
|
+
};
|
|
1750
|
+
}
|
|
1751
|
+
__name(buildHotStatements, "buildHotStatements");
|
|
1752
|
+
var hotStatementsBySchema = /* @__PURE__ */ new Map();
|
|
1753
|
+
function hotStatements(schema) {
|
|
1754
|
+
let statements = hotStatementsBySchema.get(schema);
|
|
1755
|
+
if (!statements) {
|
|
1756
|
+
statements = buildHotStatements(schema);
|
|
1757
|
+
hotStatementsBySchema.set(schema, statements);
|
|
1758
|
+
}
|
|
1759
|
+
return statements;
|
|
1760
|
+
}
|
|
1761
|
+
__name(hotStatements, "hotStatements");
|
|
1762
|
+
function storeTables(schema) {
|
|
1763
|
+
const t = durablePgTables(schema);
|
|
1764
|
+
return {
|
|
1765
|
+
workflowRuns: t.durableWorkflowRuns,
|
|
1766
|
+
stepCheckpoints: t.durableStepCheckpoints,
|
|
1767
|
+
runAttributes: t.durableRunAttributes,
|
|
1768
|
+
signalWaiters: t.durableSignalWaiters,
|
|
1769
|
+
bufferedSignals: t.durableBufferedSignals,
|
|
1770
|
+
bufferedEvents: t.durableBufferedEvents,
|
|
1771
|
+
signalIds: t.durableSignalIds,
|
|
1772
|
+
schedules: t.durableSchedules
|
|
1773
|
+
};
|
|
1774
|
+
}
|
|
1775
|
+
__name(storeTables, "storeTables");
|
|
1653
1776
|
var HOT_SETTLE_MAX_ROWS = 64;
|
|
1654
|
-
var settleTexts = /* @__PURE__ */ new Map();
|
|
1655
1777
|
var lockTime = /* @__PURE__ */ __name((ms2) => RUNS.encode("lockedUntil", new Date(ms2)), "lockTime");
|
|
1656
|
-
function settleResultsText(n) {
|
|
1657
|
-
let text2 = settleTexts.get(n);
|
|
1658
|
-
if (text2 !== void 0) return text2;
|
|
1659
|
-
const rows = Array.from({
|
|
1660
|
-
length: n
|
|
1661
|
-
}, (_, r) => {
|
|
1662
|
-
const p = /* @__PURE__ */ __name((k) => `$${r * 8 + k}`, "p");
|
|
1663
|
-
return `(${p(1)}::text, ${p(2)}::integer, ${p(3)}::text, ${p(4)}::jsonb, ${p(5)}::jsonb, ${p(6)}::jsonb, ${p(7)}::timestamptz, ${p(8)}::timestamptz)`;
|
|
1664
|
-
});
|
|
1665
|
-
text2 = `UPDATE ${T_CPS} 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 NOT EXISTS (SELECT 1 FROM ${T_RUNS} AS r WHERE r.id = c.run_id AND r.status IN ('cancelled', 'completed')) RETURNING ${CPS_NO_INPUT.columns.map(([, c]) => `c.${quote(c.name)}`).join(", ")}`;
|
|
1666
|
-
settleTexts.set(n, text2);
|
|
1667
|
-
return text2;
|
|
1668
|
-
}
|
|
1669
|
-
__name(settleResultsText, "settleResultsText");
|
|
1670
1778
|
function settleResultValues(r) {
|
|
1671
1779
|
const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
|
|
1672
1780
|
return [
|
|
@@ -1696,14 +1804,14 @@ function distinctRounds(checkpoints) {
|
|
|
1696
1804
|
return rounds;
|
|
1697
1805
|
}
|
|
1698
1806
|
__name(distinctRounds, "distinctRounds");
|
|
1699
|
-
function updateIfPendingFrom(checkpoints) {
|
|
1807
|
+
function updateIfPendingFrom(t, checkpoints) {
|
|
1700
1808
|
const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
|
|
1701
1809
|
const ts = /* @__PURE__ */ __name((v) => v == null ? null : v.toISOString(), "ts");
|
|
1702
1810
|
const values = checkpoints.map((cp) => {
|
|
1703
1811
|
const r = toCheckpointRow(cp);
|
|
1704
1812
|
return sql3`(${r.runId}::text, ${r.seq}::integer, ${r.name}::text, ${r.kind}::text, ${r.stepId}::text, ${r.status}::text, ${json(r.input)}::jsonb, ${json(r.output)}::jsonb, ${json(r.error)}::jsonb, ${json(r.events)}::jsonb, ${r.attempts}::integer, ${r.workerGroup}::text, ${r.parallelGroup}::text, ${ts(r.wakeAt)}::timestamptz, ${ts(r.enqueuedAt)}::timestamptz, ${ts(r.startedAt)}::timestamptz, ${ts(r.finishedAt)}::timestamptz)`;
|
|
1705
1813
|
});
|
|
1706
|
-
return sql3`UPDATE ${
|
|
1814
|
+
return sql3`UPDATE ${t.stepCheckpoints} AS c SET
|
|
1707
1815
|
name = v.name, kind = v.kind, step_id = v.step_id, status = v.status, input = v.input,
|
|
1708
1816
|
output = v.output, error = v.error, events = v.events, attempts = v.attempts,
|
|
1709
1817
|
worker_group = v.worker_group, parallel_group = v.parallel_group, wake_at = v.wake_at,
|
|
@@ -1715,10 +1823,10 @@ function updateIfPendingFrom(checkpoints) {
|
|
|
1715
1823
|
RETURNING c.run_id, c.seq`;
|
|
1716
1824
|
}
|
|
1717
1825
|
__name(updateIfPendingFrom, "updateIfPendingFrom");
|
|
1718
|
-
function settleIfPendingFrom(settlements) {
|
|
1826
|
+
function settleIfPendingFrom(t, settlements) {
|
|
1719
1827
|
const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
|
|
1720
1828
|
const values = settlements.map((s) => sql3`(${s.runId}::text, ${s.seq}::integer, ${s.status}::text, ${json(s.output)}::jsonb, ${json(s.error)}::jsonb, ${json(s.events)}::jsonb, ${s.startedAt.toISOString()}::timestamptz, ${s.finishedAt.toISOString()}::timestamptz)`);
|
|
1721
|
-
return sql3`UPDATE ${
|
|
1829
|
+
return sql3`UPDATE ${t.stepCheckpoints} AS c SET
|
|
1722
1830
|
status = v.status, output = v.output, error = v.error, events = v.events,
|
|
1723
1831
|
started_at = v.started_at, finished_at = v.finished_at
|
|
1724
1832
|
FROM (VALUES ${sql3.join(values, sql3`, `)}) AS v(run_id, seq, status, output, error, events,
|
|
@@ -1748,11 +1856,11 @@ function settledRowFromDriver(raw) {
|
|
|
1748
1856
|
return row;
|
|
1749
1857
|
}
|
|
1750
1858
|
__name(settledRowFromDriver, "settledRowFromDriver");
|
|
1751
|
-
function settleResultsFrom(results) {
|
|
1859
|
+
function settleResultsFrom(t, results) {
|
|
1752
1860
|
const json = /* @__PURE__ */ __name((v) => v == null ? null : JSON.stringify(v), "json");
|
|
1753
1861
|
const values = results.map((r) => sql3`(${r.runId}::text, ${r.seq}::integer, ${r.status}::text, ${json(r.status === "completed" ? r.output : null)}::jsonb, ${json(r.error)}::jsonb, ${json(r.events)}::jsonb, ${r.startedAt ? r.startedAt.toISOString() : null}::timestamptz, ${r.finishedAt.toISOString()}::timestamptz)`);
|
|
1754
1862
|
const returning = sql3.join(Object.values(SETTLED_COLUMNS).map((column) => sql3`c.${sql3.identifier(column.name)}`), sql3`, `);
|
|
1755
|
-
return sql3`UPDATE ${
|
|
1863
|
+
return sql3`UPDATE ${t.stepCheckpoints} AS c SET
|
|
1756
1864
|
status = v.status,
|
|
1757
1865
|
output = CASE WHEN v.status = 'completed' THEN v.output ELSE c.output END,
|
|
1758
1866
|
error = v.error,
|
|
@@ -1763,37 +1871,37 @@ function settleResultsFrom(results) {
|
|
|
1763
1871
|
started_at, finished_at)
|
|
1764
1872
|
WHERE c.run_id = v.run_id AND c.seq = v.seq AND c.status = 'pending'
|
|
1765
1873
|
AND NOT EXISTS (
|
|
1766
|
-
SELECT 1 FROM ${
|
|
1874
|
+
SELECT 1 FROM ${t.workflowRuns} AS r
|
|
1767
1875
|
WHERE r.id = c.run_id AND r.status IN ('cancelled', 'completed')
|
|
1768
1876
|
)
|
|
1769
1877
|
RETURNING ${returning}`;
|
|
1770
1878
|
}
|
|
1771
1879
|
__name(settleResultsFrom, "settleResultsFrom");
|
|
1772
|
-
async function upsertCheckpoint(db, cp) {
|
|
1880
|
+
async function upsertCheckpoint(t, db, cp) {
|
|
1773
1881
|
const row = toCheckpointRow(cp);
|
|
1774
|
-
await db.insert(
|
|
1882
|
+
await db.insert(t.stepCheckpoints).values(row).onConflictDoUpdate({
|
|
1775
1883
|
target: [
|
|
1776
|
-
|
|
1777
|
-
|
|
1884
|
+
t.stepCheckpoints.runId,
|
|
1885
|
+
t.stepCheckpoints.seq
|
|
1778
1886
|
],
|
|
1779
1887
|
set: row
|
|
1780
1888
|
});
|
|
1781
1889
|
}
|
|
1782
1890
|
__name(upsertCheckpoint, "upsertCheckpoint");
|
|
1783
|
-
async function reindexAttributes(db, runId, attributes) {
|
|
1784
|
-
await db.delete(
|
|
1891
|
+
async function reindexAttributes(t, db, runId, attributes) {
|
|
1892
|
+
await db.delete(t.runAttributes).where(eq(t.runAttributes.runId, runId));
|
|
1785
1893
|
const rows = normalizeAttributeRows(runId, attributes);
|
|
1786
|
-
if (rows.length) await db.insert(
|
|
1894
|
+
if (rows.length) await db.insert(t.runAttributes).values(rows);
|
|
1787
1895
|
}
|
|
1788
1896
|
__name(reindexAttributes, "reindexAttributes");
|
|
1789
|
-
async function deleteRunsCascade(db, runIds) {
|
|
1897
|
+
async function deleteRunsCascade(t, db, runIds) {
|
|
1790
1898
|
await db.select({
|
|
1791
|
-
id:
|
|
1792
|
-
}).from(
|
|
1793
|
-
await db.delete(
|
|
1794
|
-
await db.delete(
|
|
1795
|
-
await db.delete(
|
|
1796
|
-
await db.delete(
|
|
1899
|
+
id: t.workflowRuns.id
|
|
1900
|
+
}).from(t.workflowRuns).where(inArray(t.workflowRuns.id, runIds)).orderBy(asc(t.workflowRuns.id)).for("update");
|
|
1901
|
+
await db.delete(t.stepCheckpoints).where(inArray(t.stepCheckpoints.runId, runIds));
|
|
1902
|
+
await db.delete(t.signalWaiters).where(inArray(t.signalWaiters.runId, runIds));
|
|
1903
|
+
await db.delete(t.runAttributes).where(inArray(t.runAttributes.runId, runIds));
|
|
1904
|
+
await db.delete(t.workflowRuns).where(inArray(t.workflowRuns.id, runIds));
|
|
1797
1905
|
}
|
|
1798
1906
|
__name(deleteRunsCascade, "deleteRunsCascade");
|
|
1799
1907
|
function fromWaiterRow(row) {
|
|
@@ -2002,10 +2110,13 @@ export {
|
|
|
2002
2110
|
drizzlePgSchemaRepairWarning,
|
|
2003
2111
|
durableBufferedEvents,
|
|
2004
2112
|
durableBufferedSignals,
|
|
2113
|
+
durablePgDdl,
|
|
2005
2114
|
durablePgManagedTables,
|
|
2006
2115
|
durablePgSchema,
|
|
2116
|
+
durablePgTables,
|
|
2007
2117
|
durableRunAttributes,
|
|
2008
2118
|
durableSchedules,
|
|
2119
|
+
durableSchemaName,
|
|
2009
2120
|
durableSignalIds,
|
|
2010
2121
|
durableSignalWaiters,
|
|
2011
2122
|
durableStepCheckpoints,
|