pg-workflows 0.7.1 → 0.8.1
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/README.md +83 -765
- package/dist/client.entry.cjs +826 -0
- package/dist/client.entry.d.cts +229 -0
- package/dist/client.entry.d.ts +229 -0
- package/dist/client.entry.js +13 -0
- package/dist/client.entry.js.map +16 -0
- package/dist/index.cjs +644 -323
- package/dist/index.d.cts +125 -11
- package/dist/index.d.ts +125 -11
- package/dist/index.js +75 -487
- package/dist/index.js.map +12 -10
- package/dist/shared/chunk-wsa44g1x.js +757 -0
- package/dist/shared/chunk-wsa44g1x.js.map +16 -0
- package/package.json +11 -1
package/dist/index.js
CHANGED
|
@@ -1,50 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_PGBOSS_SCHEMA,
|
|
3
|
+
PAUSE_EVENT_NAME,
|
|
4
|
+
StepType,
|
|
5
|
+
WORKFLOW_RUN_QUEUE_NAME,
|
|
6
|
+
WorkflowClient,
|
|
7
|
+
WorkflowEngineError,
|
|
8
|
+
WorkflowRunNotFoundError,
|
|
9
|
+
WorkflowStatus,
|
|
10
|
+
createWorkflowRef,
|
|
11
|
+
getWorkflowRun,
|
|
12
|
+
getWorkflowRuns,
|
|
13
|
+
insertWorkflowRun,
|
|
14
|
+
runMigrations,
|
|
15
|
+
updateWorkflowRun,
|
|
16
|
+
withPostgresTransaction,
|
|
17
|
+
workflow
|
|
18
|
+
} from "./shared/chunk-wsa44g1x.js";
|
|
18
19
|
// src/duration.ts
|
|
19
20
|
import parse from "parse-duration";
|
|
20
|
-
|
|
21
|
-
// src/error.ts
|
|
22
|
-
class WorkflowEngineError extends Error {
|
|
23
|
-
workflowId;
|
|
24
|
-
runId;
|
|
25
|
-
cause;
|
|
26
|
-
issues;
|
|
27
|
-
constructor(message, workflowId, runId, cause = undefined, issues) {
|
|
28
|
-
super(message);
|
|
29
|
-
this.workflowId = workflowId;
|
|
30
|
-
this.runId = runId;
|
|
31
|
-
this.cause = cause;
|
|
32
|
-
this.issues = issues;
|
|
33
|
-
this.name = "WorkflowEngineError";
|
|
34
|
-
if (Error.captureStackTrace) {
|
|
35
|
-
Error.captureStackTrace(this, WorkflowEngineError);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
class WorkflowRunNotFoundError extends WorkflowEngineError {
|
|
41
|
-
constructor(runId, workflowId) {
|
|
42
|
-
super("Workflow run not found", workflowId, runId);
|
|
43
|
-
this.name = "WorkflowRunNotFoundError";
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// src/duration.ts
|
|
48
21
|
var MS_PER_SECOND = 1000;
|
|
49
22
|
var MS_PER_MINUTE = 60 * MS_PER_SECOND;
|
|
50
23
|
var MS_PER_HOUR = 60 * MS_PER_MINUTE;
|
|
@@ -75,28 +48,6 @@ import { PgBoss } from "pg-boss";
|
|
|
75
48
|
|
|
76
49
|
// src/ast-parser.ts
|
|
77
50
|
import * as ts from "typescript";
|
|
78
|
-
|
|
79
|
-
// src/types.ts
|
|
80
|
-
var WorkflowStatus;
|
|
81
|
-
((WorkflowStatus2) => {
|
|
82
|
-
WorkflowStatus2["PENDING"] = "pending";
|
|
83
|
-
WorkflowStatus2["RUNNING"] = "running";
|
|
84
|
-
WorkflowStatus2["PAUSED"] = "paused";
|
|
85
|
-
WorkflowStatus2["COMPLETED"] = "completed";
|
|
86
|
-
WorkflowStatus2["FAILED"] = "failed";
|
|
87
|
-
WorkflowStatus2["CANCELLED"] = "cancelled";
|
|
88
|
-
})(WorkflowStatus ||= {});
|
|
89
|
-
var StepType;
|
|
90
|
-
((StepType2) => {
|
|
91
|
-
StepType2["PAUSE"] = "pause";
|
|
92
|
-
StepType2["RUN"] = "run";
|
|
93
|
-
StepType2["WAIT_FOR"] = "waitFor";
|
|
94
|
-
StepType2["WAIT_UNTIL"] = "waitUntil";
|
|
95
|
-
StepType2["DELAY"] = "delay";
|
|
96
|
-
StepType2["POLL"] = "poll";
|
|
97
|
-
})(StepType ||= {});
|
|
98
|
-
|
|
99
|
-
// src/ast-parser.ts
|
|
100
51
|
function parseWorkflowHandler(handler) {
|
|
101
52
|
const handlerSource = handler.toString();
|
|
102
53
|
const sourceFile = ts.createSourceFile("handler.ts", handlerSource, ts.ScriptTarget.Latest, true);
|
|
@@ -165,390 +116,8 @@ function parseWorkflowHandler(handler) {
|
|
|
165
116
|
return { steps: Array.from(steps.values()) };
|
|
166
117
|
}
|
|
167
118
|
|
|
168
|
-
// src/db/migration.ts
|
|
169
|
-
var MIGRATION_LOCK_ID = 738291645;
|
|
170
|
-
var CURRENT_SCHEMA_VERSION = 2;
|
|
171
|
-
async function runMigrations(db) {
|
|
172
|
-
if (await isSchemaUpToDate(db)) {
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
const currentVersion = await getCurrentVersion(db);
|
|
176
|
-
const commands = [];
|
|
177
|
-
if (currentVersion < 1) {
|
|
178
|
-
commands.push(`
|
|
179
|
-
CREATE TABLE IF NOT EXISTS workflow_runs (
|
|
180
|
-
id varchar(32) PRIMARY KEY NOT NULL,
|
|
181
|
-
created_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
182
|
-
updated_at timestamp with time zone DEFAULT now() NOT NULL,
|
|
183
|
-
resource_id varchar(32),
|
|
184
|
-
workflow_id varchar(32) NOT NULL,
|
|
185
|
-
status text DEFAULT 'pending' NOT NULL,
|
|
186
|
-
input jsonb NOT NULL,
|
|
187
|
-
output jsonb,
|
|
188
|
-
error text,
|
|
189
|
-
current_step_id varchar(256) NOT NULL,
|
|
190
|
-
timeline jsonb DEFAULT '{}'::jsonb NOT NULL,
|
|
191
|
-
paused_at timestamp with time zone,
|
|
192
|
-
resumed_at timestamp with time zone,
|
|
193
|
-
completed_at timestamp with time zone,
|
|
194
|
-
timeout_at timestamp with time zone,
|
|
195
|
-
retry_count integer DEFAULT 0 NOT NULL,
|
|
196
|
-
max_retries integer DEFAULT 0 NOT NULL,
|
|
197
|
-
job_id varchar(256)
|
|
198
|
-
)
|
|
199
|
-
`);
|
|
200
|
-
commands.push(`
|
|
201
|
-
CREATE INDEX IF NOT EXISTS workflow_runs_created_at_idx ON workflow_runs USING btree (created_at)
|
|
202
|
-
`);
|
|
203
|
-
commands.push(`
|
|
204
|
-
CREATE INDEX IF NOT EXISTS workflow_runs_resource_id_created_at_idx ON workflow_runs USING btree (resource_id, created_at DESC)
|
|
205
|
-
`);
|
|
206
|
-
commands.push(`
|
|
207
|
-
CREATE INDEX IF NOT EXISTS workflow_runs_status_created_at_idx ON workflow_runs USING btree (status, created_at DESC)
|
|
208
|
-
`);
|
|
209
|
-
commands.push(`
|
|
210
|
-
CREATE INDEX IF NOT EXISTS workflow_runs_workflow_id_created_at_idx ON workflow_runs USING btree (workflow_id, created_at DESC)
|
|
211
|
-
`);
|
|
212
|
-
commands.push(`
|
|
213
|
-
CREATE INDEX IF NOT EXISTS workflow_runs_resource_id_workflow_id_created_at_idx ON workflow_runs USING btree (resource_id, workflow_id, created_at DESC)
|
|
214
|
-
`);
|
|
215
|
-
}
|
|
216
|
-
if (currentVersion < 2) {
|
|
217
|
-
commands.push("DROP INDEX IF EXISTS workflow_runs_workflow_id_idx");
|
|
218
|
-
commands.push("DROP INDEX IF EXISTS workflow_runs_resource_id_idx");
|
|
219
|
-
commands.push("ALTER TABLE workflow_runs ADD COLUMN IF NOT EXISTS idempotency_key varchar(256)");
|
|
220
|
-
commands.push(`
|
|
221
|
-
CREATE UNIQUE INDEX IF NOT EXISTS workflow_runs_idempotency_key_idx ON workflow_runs (idempotency_key) WHERE idempotency_key IS NOT NULL
|
|
222
|
-
`);
|
|
223
|
-
}
|
|
224
|
-
if (currentVersion === 0) {
|
|
225
|
-
commands.push(`INSERT INTO workflow_schema_version (version) VALUES (${CURRENT_SCHEMA_VERSION})`);
|
|
226
|
-
} else {
|
|
227
|
-
commands.push(`UPDATE workflow_schema_version SET version = ${CURRENT_SCHEMA_VERSION}`);
|
|
228
|
-
}
|
|
229
|
-
if (commands.length === 0) {
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
const sql = [
|
|
233
|
-
"BEGIN",
|
|
234
|
-
"SET LOCAL lock_timeout = '30s'",
|
|
235
|
-
"SET LOCAL idle_in_transaction_session_timeout = '30s'",
|
|
236
|
-
`SELECT pg_advisory_xact_lock(${MIGRATION_LOCK_ID})`,
|
|
237
|
-
"CREATE TABLE IF NOT EXISTS workflow_schema_version (version integer NOT NULL)",
|
|
238
|
-
...commands,
|
|
239
|
-
"COMMIT"
|
|
240
|
-
].join(`;
|
|
241
|
-
`);
|
|
242
|
-
await db.executeSql(sql, []);
|
|
243
|
-
}
|
|
244
|
-
async function isSchemaUpToDate(db) {
|
|
245
|
-
try {
|
|
246
|
-
const result = await db.executeSql("SELECT version FROM workflow_schema_version LIMIT 1", []);
|
|
247
|
-
return (result.rows[0]?.version ?? 0) >= CURRENT_SCHEMA_VERSION;
|
|
248
|
-
} catch {
|
|
249
|
-
return false;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
async function getCurrentVersion(db) {
|
|
253
|
-
try {
|
|
254
|
-
const result = await db.executeSql("SELECT version FROM workflow_schema_version LIMIT 1", []);
|
|
255
|
-
return result.rows[0]?.version ?? 0;
|
|
256
|
-
} catch {
|
|
257
|
-
return 0;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// src/db/queries.ts
|
|
262
|
-
import ksuid from "ksuid";
|
|
263
|
-
function generateKSUID(prefix) {
|
|
264
|
-
return `${prefix ? `${prefix}_` : ""}${ksuid.randomSync().string}`;
|
|
265
|
-
}
|
|
266
|
-
function mapRowToWorkflowRun(row) {
|
|
267
|
-
return {
|
|
268
|
-
id: row.id,
|
|
269
|
-
createdAt: new Date(row.created_at),
|
|
270
|
-
updatedAt: new Date(row.updated_at),
|
|
271
|
-
resourceId: row.resource_id,
|
|
272
|
-
workflowId: row.workflow_id,
|
|
273
|
-
status: row.status,
|
|
274
|
-
input: typeof row.input === "string" ? JSON.parse(row.input) : row.input,
|
|
275
|
-
output: typeof row.output === "string" ? row.output.trim().startsWith("{") || row.output.trim().startsWith("[") ? JSON.parse(row.output) : row.output : row.output ?? null,
|
|
276
|
-
error: row.error,
|
|
277
|
-
currentStepId: row.current_step_id,
|
|
278
|
-
timeline: typeof row.timeline === "string" ? JSON.parse(row.timeline) : row.timeline,
|
|
279
|
-
pausedAt: row.paused_at ? new Date(row.paused_at) : null,
|
|
280
|
-
resumedAt: row.resumed_at ? new Date(row.resumed_at) : null,
|
|
281
|
-
completedAt: row.completed_at ? new Date(row.completed_at) : null,
|
|
282
|
-
timeoutAt: row.timeout_at ? new Date(row.timeout_at) : null,
|
|
283
|
-
retryCount: row.retry_count,
|
|
284
|
-
maxRetries: row.max_retries,
|
|
285
|
-
jobId: row.job_id,
|
|
286
|
-
idempotencyKey: row.idempotency_key
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
async function insertWorkflowRun({
|
|
290
|
-
resourceId,
|
|
291
|
-
workflowId,
|
|
292
|
-
currentStepId,
|
|
293
|
-
status,
|
|
294
|
-
input,
|
|
295
|
-
maxRetries,
|
|
296
|
-
timeoutAt,
|
|
297
|
-
idempotencyKey
|
|
298
|
-
}, db) {
|
|
299
|
-
const runId = generateKSUID("run");
|
|
300
|
-
const now = new Date;
|
|
301
|
-
const result = await db.executeSql(`INSERT INTO workflow_runs (
|
|
302
|
-
id,
|
|
303
|
-
resource_id,
|
|
304
|
-
workflow_id,
|
|
305
|
-
current_step_id,
|
|
306
|
-
status,
|
|
307
|
-
input,
|
|
308
|
-
max_retries,
|
|
309
|
-
timeout_at,
|
|
310
|
-
created_at,
|
|
311
|
-
updated_at,
|
|
312
|
-
timeline,
|
|
313
|
-
retry_count,
|
|
314
|
-
idempotency_key
|
|
315
|
-
)
|
|
316
|
-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
|
|
317
|
-
ON CONFLICT (idempotency_key) WHERE idempotency_key IS NOT NULL DO NOTHING
|
|
318
|
-
RETURNING *`, [
|
|
319
|
-
runId,
|
|
320
|
-
resourceId ?? null,
|
|
321
|
-
workflowId,
|
|
322
|
-
currentStepId,
|
|
323
|
-
status,
|
|
324
|
-
JSON.stringify(input),
|
|
325
|
-
maxRetries,
|
|
326
|
-
timeoutAt,
|
|
327
|
-
now,
|
|
328
|
-
now,
|
|
329
|
-
"{}",
|
|
330
|
-
0,
|
|
331
|
-
idempotencyKey ?? null
|
|
332
|
-
]);
|
|
333
|
-
if (result.rows[0]) {
|
|
334
|
-
return { run: mapRowToWorkflowRun(result.rows[0]), created: true };
|
|
335
|
-
}
|
|
336
|
-
const existing = await db.executeSql("SELECT * FROM workflow_runs WHERE idempotency_key = $1", [
|
|
337
|
-
idempotencyKey
|
|
338
|
-
]);
|
|
339
|
-
if (!existing.rows[0]) {
|
|
340
|
-
throw new Error(`Idempotency conflict: existing run not found for key "${idempotencyKey}"`);
|
|
341
|
-
}
|
|
342
|
-
return { run: mapRowToWorkflowRun(existing.rows[0]), created: false };
|
|
343
|
-
}
|
|
344
|
-
async function getWorkflowRun({
|
|
345
|
-
runId,
|
|
346
|
-
resourceId
|
|
347
|
-
}, { exclusiveLock = false, db }) {
|
|
348
|
-
const lockSuffix = exclusiveLock ? "FOR UPDATE" : "";
|
|
349
|
-
const result = resourceId ? await db.executeSql(`SELECT * FROM workflow_runs
|
|
350
|
-
WHERE id = $1 AND resource_id = $2
|
|
351
|
-
${lockSuffix}`, [runId, resourceId]) : await db.executeSql(`SELECT * FROM workflow_runs
|
|
352
|
-
WHERE id = $1
|
|
353
|
-
${lockSuffix}`, [runId]);
|
|
354
|
-
const run = result.rows[0];
|
|
355
|
-
if (!run) {
|
|
356
|
-
return null;
|
|
357
|
-
}
|
|
358
|
-
return mapRowToWorkflowRun(run);
|
|
359
|
-
}
|
|
360
|
-
async function updateWorkflowRun({
|
|
361
|
-
runId,
|
|
362
|
-
resourceId,
|
|
363
|
-
data,
|
|
364
|
-
expectedStatuses
|
|
365
|
-
}, db) {
|
|
366
|
-
const now = new Date;
|
|
367
|
-
const updates = ["updated_at = $1"];
|
|
368
|
-
const values = [now];
|
|
369
|
-
let paramIndex = 2;
|
|
370
|
-
if (data.status !== undefined) {
|
|
371
|
-
updates.push(`status = $${paramIndex}`);
|
|
372
|
-
values.push(data.status);
|
|
373
|
-
paramIndex++;
|
|
374
|
-
}
|
|
375
|
-
if (data.currentStepId !== undefined) {
|
|
376
|
-
updates.push(`current_step_id = $${paramIndex}`);
|
|
377
|
-
values.push(data.currentStepId);
|
|
378
|
-
paramIndex++;
|
|
379
|
-
}
|
|
380
|
-
if (data.timeline !== undefined) {
|
|
381
|
-
updates.push(`timeline = $${paramIndex}`);
|
|
382
|
-
values.push(JSON.stringify(data.timeline));
|
|
383
|
-
paramIndex++;
|
|
384
|
-
}
|
|
385
|
-
if (data.pausedAt !== undefined) {
|
|
386
|
-
updates.push(`paused_at = $${paramIndex}`);
|
|
387
|
-
values.push(data.pausedAt);
|
|
388
|
-
paramIndex++;
|
|
389
|
-
}
|
|
390
|
-
if (data.resumedAt !== undefined) {
|
|
391
|
-
updates.push(`resumed_at = $${paramIndex}`);
|
|
392
|
-
values.push(data.resumedAt);
|
|
393
|
-
paramIndex++;
|
|
394
|
-
}
|
|
395
|
-
if (data.completedAt !== undefined) {
|
|
396
|
-
updates.push(`completed_at = $${paramIndex}`);
|
|
397
|
-
values.push(data.completedAt);
|
|
398
|
-
paramIndex++;
|
|
399
|
-
}
|
|
400
|
-
if (data.output !== undefined) {
|
|
401
|
-
updates.push(`output = $${paramIndex}`);
|
|
402
|
-
values.push(JSON.stringify(data.output));
|
|
403
|
-
paramIndex++;
|
|
404
|
-
}
|
|
405
|
-
if (data.error !== undefined) {
|
|
406
|
-
updates.push(`error = $${paramIndex}`);
|
|
407
|
-
values.push(data.error);
|
|
408
|
-
paramIndex++;
|
|
409
|
-
}
|
|
410
|
-
if (data.retryCount !== undefined) {
|
|
411
|
-
updates.push(`retry_count = $${paramIndex}`);
|
|
412
|
-
values.push(data.retryCount);
|
|
413
|
-
paramIndex++;
|
|
414
|
-
}
|
|
415
|
-
if (data.jobId !== undefined) {
|
|
416
|
-
updates.push(`job_id = $${paramIndex}`);
|
|
417
|
-
values.push(data.jobId);
|
|
418
|
-
paramIndex++;
|
|
419
|
-
}
|
|
420
|
-
values.push(runId);
|
|
421
|
-
const idParam = paramIndex;
|
|
422
|
-
paramIndex++;
|
|
423
|
-
if (resourceId) {
|
|
424
|
-
values.push(resourceId);
|
|
425
|
-
paramIndex++;
|
|
426
|
-
}
|
|
427
|
-
if (expectedStatuses && expectedStatuses.length > 0) {
|
|
428
|
-
values.push(expectedStatuses);
|
|
429
|
-
paramIndex++;
|
|
430
|
-
}
|
|
431
|
-
let whereClause = resourceId ? `WHERE id = $${idParam} AND resource_id = $${idParam + 1}` : `WHERE id = $${idParam}`;
|
|
432
|
-
if (expectedStatuses && expectedStatuses.length > 0) {
|
|
433
|
-
whereClause += ` AND status = ANY($${paramIndex - 1})`;
|
|
434
|
-
}
|
|
435
|
-
const query = `
|
|
436
|
-
UPDATE workflow_runs
|
|
437
|
-
SET ${updates.join(", ")}
|
|
438
|
-
${whereClause}
|
|
439
|
-
RETURNING *
|
|
440
|
-
`;
|
|
441
|
-
const result = await db.executeSql(query, values);
|
|
442
|
-
const run = result.rows[0];
|
|
443
|
-
if (!run) {
|
|
444
|
-
return null;
|
|
445
|
-
}
|
|
446
|
-
return mapRowToWorkflowRun(run);
|
|
447
|
-
}
|
|
448
|
-
async function getWorkflowRuns({
|
|
449
|
-
resourceId,
|
|
450
|
-
startingAfter,
|
|
451
|
-
endingBefore,
|
|
452
|
-
limit = 20,
|
|
453
|
-
statuses,
|
|
454
|
-
workflowId
|
|
455
|
-
}, db) {
|
|
456
|
-
const conditions = [];
|
|
457
|
-
const values = [];
|
|
458
|
-
let paramIndex = 1;
|
|
459
|
-
if (resourceId) {
|
|
460
|
-
conditions.push(`resource_id = $${paramIndex}`);
|
|
461
|
-
values.push(resourceId);
|
|
462
|
-
paramIndex++;
|
|
463
|
-
}
|
|
464
|
-
if (statuses && statuses.length > 0) {
|
|
465
|
-
conditions.push(`status = ANY($${paramIndex})`);
|
|
466
|
-
values.push(statuses);
|
|
467
|
-
paramIndex++;
|
|
468
|
-
}
|
|
469
|
-
if (workflowId) {
|
|
470
|
-
conditions.push(`workflow_id = $${paramIndex}`);
|
|
471
|
-
values.push(workflowId);
|
|
472
|
-
paramIndex++;
|
|
473
|
-
}
|
|
474
|
-
const cursorIds = [startingAfter, endingBefore].filter(Boolean);
|
|
475
|
-
if (cursorIds.length > 0) {
|
|
476
|
-
const cursorResult = await db.executeSql("SELECT id, created_at FROM workflow_runs WHERE id = ANY($1)", [cursorIds]);
|
|
477
|
-
const cursorMap = new Map;
|
|
478
|
-
for (const row of cursorResult.rows) {
|
|
479
|
-
cursorMap.set(row.id, typeof row.created_at === "string" ? new Date(row.created_at) : row.created_at);
|
|
480
|
-
}
|
|
481
|
-
if (startingAfter) {
|
|
482
|
-
const cursor = cursorMap.get(startingAfter);
|
|
483
|
-
if (cursor) {
|
|
484
|
-
conditions.push(`created_at < $${paramIndex}`);
|
|
485
|
-
values.push(cursor);
|
|
486
|
-
paramIndex++;
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
if (endingBefore) {
|
|
490
|
-
const cursor = cursorMap.get(endingBefore);
|
|
491
|
-
if (cursor) {
|
|
492
|
-
conditions.push(`created_at > $${paramIndex}`);
|
|
493
|
-
values.push(cursor);
|
|
494
|
-
paramIndex++;
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
499
|
-
const actualLimit = Math.min(Math.max(limit, 1), 100) + 1;
|
|
500
|
-
const isBackward = !!endingBefore && !startingAfter;
|
|
501
|
-
const query = `
|
|
502
|
-
SELECT * FROM workflow_runs
|
|
503
|
-
${whereClause}
|
|
504
|
-
ORDER BY created_at ${isBackward ? "ASC" : "DESC"}
|
|
505
|
-
LIMIT $${paramIndex}
|
|
506
|
-
`;
|
|
507
|
-
values.push(actualLimit);
|
|
508
|
-
const result = await db.executeSql(query, values);
|
|
509
|
-
const rows = result.rows;
|
|
510
|
-
const hasExtraRow = rows.length > (limit ?? 20);
|
|
511
|
-
const rawItems = hasExtraRow ? rows.slice(0, limit) : rows;
|
|
512
|
-
if (isBackward) {
|
|
513
|
-
rawItems.reverse();
|
|
514
|
-
}
|
|
515
|
-
const items = rawItems.map((row) => mapRowToWorkflowRun(row));
|
|
516
|
-
const hasMore = isBackward ? items.length > 0 : hasExtraRow;
|
|
517
|
-
const hasPrev = isBackward ? hasExtraRow : !!startingAfter && items.length > 0;
|
|
518
|
-
const nextCursor = hasMore && items.length > 0 ? items[items.length - 1]?.id ?? null : null;
|
|
519
|
-
const prevCursor = hasPrev && items.length > 0 ? items[0]?.id ?? null : null;
|
|
520
|
-
return { items, nextCursor, prevCursor, hasMore, hasPrev };
|
|
521
|
-
}
|
|
522
|
-
async function withPostgresTransaction(db, callback, pool) {
|
|
523
|
-
let txDb;
|
|
524
|
-
let release;
|
|
525
|
-
if (pool) {
|
|
526
|
-
const client = await pool.connect();
|
|
527
|
-
txDb = {
|
|
528
|
-
executeSql: (text, values) => client.query(text, values)
|
|
529
|
-
};
|
|
530
|
-
release = () => client.release();
|
|
531
|
-
} else {
|
|
532
|
-
txDb = db;
|
|
533
|
-
}
|
|
534
|
-
try {
|
|
535
|
-
await txDb.executeSql("BEGIN", []);
|
|
536
|
-
const result = await callback(txDb);
|
|
537
|
-
await txDb.executeSql("COMMIT", []);
|
|
538
|
-
return result;
|
|
539
|
-
} catch (error) {
|
|
540
|
-
await txDb.executeSql("ROLLBACK", []);
|
|
541
|
-
throw error;
|
|
542
|
-
} finally {
|
|
543
|
-
release?.();
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
|
|
547
119
|
// src/engine.ts
|
|
548
|
-
var PAUSE_EVENT_NAME = "__internal_pause";
|
|
549
|
-
var WORKFLOW_RUN_QUEUE_NAME = "workflow-run";
|
|
550
120
|
var LOG_PREFIX = "[WorkflowEngine]";
|
|
551
|
-
var DEFAULT_PGBOSS_SCHEMA = "pgboss_v12_pgworkflow";
|
|
552
121
|
var StepTypeToIcon = {
|
|
553
122
|
["run" /* RUN */]: "λ",
|
|
554
123
|
["waitFor" /* WAIT_FOR */]: "○",
|
|
@@ -655,13 +224,26 @@ class WorkflowEngine {
|
|
|
655
224
|
this.workflows.clear();
|
|
656
225
|
return this;
|
|
657
226
|
}
|
|
658
|
-
async startWorkflow({
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
idempotencyKey
|
|
663
|
-
options
|
|
664
|
-
|
|
227
|
+
async startWorkflow(refOrParams, inputArg, optionsArg) {
|
|
228
|
+
let workflowId;
|
|
229
|
+
let input;
|
|
230
|
+
let resourceId;
|
|
231
|
+
let idempotencyKey;
|
|
232
|
+
let options;
|
|
233
|
+
if (typeof refOrParams === "function" && "id" in refOrParams) {
|
|
234
|
+
workflowId = refOrParams.id;
|
|
235
|
+
input = inputArg;
|
|
236
|
+
options = optionsArg;
|
|
237
|
+
resourceId = optionsArg?.resourceId;
|
|
238
|
+
idempotencyKey = optionsArg?.idempotencyKey;
|
|
239
|
+
} else {
|
|
240
|
+
const params = refOrParams;
|
|
241
|
+
workflowId = params.workflowId;
|
|
242
|
+
input = params.input;
|
|
243
|
+
resourceId = params.resourceId;
|
|
244
|
+
idempotencyKey = params.idempotencyKey;
|
|
245
|
+
options = params.options;
|
|
246
|
+
}
|
|
665
247
|
if (!this._started) {
|
|
666
248
|
await this.start(false, { batchSize: options?.batchSize ?? 1 });
|
|
667
249
|
}
|
|
@@ -912,27 +494,29 @@ class WorkflowEngine {
|
|
|
912
494
|
return run.resourceId ?? undefined;
|
|
913
495
|
}
|
|
914
496
|
async handleWorkflowRun([job]) {
|
|
915
|
-
const { runId, resourceId, workflowId, input, event } = job?.data ?? {};
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
}
|
|
919
|
-
if (!workflowId) {
|
|
920
|
-
throw new WorkflowEngineError("Invalid workflow run job, missing workflowId", undefined, runId);
|
|
921
|
-
}
|
|
922
|
-
const workflow2 = this.workflows.get(workflowId);
|
|
923
|
-
if (!workflow2) {
|
|
924
|
-
throw new WorkflowEngineError(`Workflow ${workflowId} not found`, workflowId, runId);
|
|
925
|
-
}
|
|
926
|
-
this.logger.log("Processing workflow run...", {
|
|
927
|
-
runId,
|
|
928
|
-
workflowId
|
|
929
|
-
});
|
|
930
|
-
let run = await this.getRun({ runId });
|
|
931
|
-
if (run.workflowId !== workflowId) {
|
|
932
|
-
throw new WorkflowEngineError(`Workflow run ${runId} does not match job workflowId ${workflowId}`, workflowId, runId);
|
|
933
|
-
}
|
|
934
|
-
const scopedResourceId = this.resolveScopedResourceId(resourceId, run);
|
|
497
|
+
const { runId = "", resourceId, workflowId = "", input, event } = job?.data ?? {};
|
|
498
|
+
let run;
|
|
499
|
+
let scopedResourceId;
|
|
935
500
|
try {
|
|
501
|
+
if (!runId) {
|
|
502
|
+
throw new WorkflowEngineError("Invalid workflow run job, missing runId", workflowId);
|
|
503
|
+
}
|
|
504
|
+
if (!workflowId) {
|
|
505
|
+
throw new WorkflowEngineError("Invalid workflow run job, missing workflowId", undefined, runId);
|
|
506
|
+
}
|
|
507
|
+
const workflow2 = this.workflows.get(workflowId);
|
|
508
|
+
if (!workflow2) {
|
|
509
|
+
throw new WorkflowEngineError(`Workflow ${workflowId} not found`, workflowId, runId);
|
|
510
|
+
}
|
|
511
|
+
this.logger.log("Processing workflow run...", {
|
|
512
|
+
runId,
|
|
513
|
+
workflowId
|
|
514
|
+
});
|
|
515
|
+
run = await this.getRun({ runId });
|
|
516
|
+
if (run.workflowId !== workflowId) {
|
|
517
|
+
throw new WorkflowEngineError(`Workflow run ${runId} does not match job workflowId ${workflowId}`, workflowId, runId);
|
|
518
|
+
}
|
|
519
|
+
scopedResourceId = this.resolveScopedResourceId(resourceId, run);
|
|
936
520
|
if (run.status === "cancelled" /* CANCELLED */) {
|
|
937
521
|
this.logger.log(`Workflow run ${runId} is cancelled, skipping`);
|
|
938
522
|
return;
|
|
@@ -1076,7 +660,7 @@ class WorkflowEngine {
|
|
|
1076
660
|
});
|
|
1077
661
|
}
|
|
1078
662
|
} catch (error) {
|
|
1079
|
-
if (run.retryCount < run.maxRetries) {
|
|
663
|
+
if (run && run.retryCount < run.maxRetries) {
|
|
1080
664
|
await this.updateRun({
|
|
1081
665
|
runId,
|
|
1082
666
|
resourceId: scopedResourceId,
|
|
@@ -1098,15 +682,17 @@ class WorkflowEngine {
|
|
|
1098
682
|
});
|
|
1099
683
|
return;
|
|
1100
684
|
}
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
685
|
+
if (runId) {
|
|
686
|
+
await this.updateRun({
|
|
687
|
+
runId,
|
|
688
|
+
resourceId: scopedResourceId,
|
|
689
|
+
data: {
|
|
690
|
+
status: "failed" /* FAILED */,
|
|
691
|
+
error: error instanceof Error ? error.message : String(error),
|
|
692
|
+
jobId: job?.id
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
}
|
|
1110
696
|
throw error;
|
|
1111
697
|
}
|
|
1112
698
|
}
|
|
@@ -1406,12 +992,14 @@ ${error.stack}` : String(error)
|
|
|
1406
992
|
export {
|
|
1407
993
|
workflow,
|
|
1408
994
|
parseDuration,
|
|
995
|
+
createWorkflowRef,
|
|
1409
996
|
WorkflowStatus,
|
|
1410
997
|
WorkflowRunNotFoundError,
|
|
1411
998
|
WorkflowEngineError,
|
|
1412
999
|
WorkflowEngine,
|
|
1000
|
+
WorkflowClient,
|
|
1413
1001
|
StepType
|
|
1414
1002
|
};
|
|
1415
1003
|
|
|
1416
|
-
//# debugId=
|
|
1004
|
+
//# debugId=9A3E4733F40A491264756E2164756E21
|
|
1417
1005
|
//# sourceMappingURL=index.js.map
|