pg-workflows 0.0.1-claimed → 0.1.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.js ADDED
@@ -0,0 +1,1004 @@
1
+ // src/definition.ts
2
+ function workflow(id, handler, { inputSchema, timeout, retries } = {}) {
3
+ return {
4
+ id,
5
+ handler,
6
+ inputSchema,
7
+ timeout,
8
+ retries
9
+ };
10
+ }
11
+ // src/engine.ts
12
+ import merge from "lodash/merge";
13
+
14
+ // src/ast-parser.ts
15
+ import * as ts from "typescript";
16
+ function parseWorkflowHandler(handler) {
17
+ const handlerSource = handler.toString();
18
+ const sourceFile = ts.createSourceFile("handler.ts", handlerSource, ts.ScriptTarget.Latest, true);
19
+ const steps = new Map;
20
+ function isInConditional(node) {
21
+ let current = node.parent;
22
+ while (current) {
23
+ if (ts.isIfStatement(current) || ts.isConditionalExpression(current) || ts.isSwitchStatement(current) || ts.isCaseClause(current)) {
24
+ return true;
25
+ }
26
+ current = current.parent;
27
+ }
28
+ return false;
29
+ }
30
+ function isInLoop(node) {
31
+ let current = node.parent;
32
+ while (current) {
33
+ if (ts.isForStatement(current) || ts.isForInStatement(current) || ts.isForOfStatement(current) || ts.isWhileStatement(current) || ts.isDoStatement(current)) {
34
+ return true;
35
+ }
36
+ current = current.parent;
37
+ }
38
+ return false;
39
+ }
40
+ function extractStepId(arg) {
41
+ if (ts.isStringLiteral(arg) || ts.isNoSubstitutionTemplateLiteral(arg)) {
42
+ return { id: arg.text, isDynamic: false };
43
+ }
44
+ if (ts.isTemplateExpression(arg)) {
45
+ let templateStr = arg.head.text;
46
+ for (const span of arg.templateSpans) {
47
+ templateStr += `\${...}`;
48
+ templateStr += span.literal.text;
49
+ }
50
+ return { id: templateStr, isDynamic: true };
51
+ }
52
+ return { id: arg.getText(sourceFile), isDynamic: true };
53
+ }
54
+ function visit(node) {
55
+ if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
56
+ const propertyAccess = node.expression;
57
+ const objectName = propertyAccess.expression.getText(sourceFile);
58
+ const methodName = propertyAccess.name.text;
59
+ if (objectName === "step" && (methodName === "run" || methodName === "waitFor" || methodName === "pause")) {
60
+ const firstArg = node.arguments[0];
61
+ if (firstArg) {
62
+ const { id, isDynamic } = extractStepId(firstArg);
63
+ const stepDefinition = {
64
+ id,
65
+ type: methodName,
66
+ conditional: isInConditional(node),
67
+ loop: isInLoop(node),
68
+ isDynamic
69
+ };
70
+ if (steps.has(id)) {
71
+ throw new Error(`Duplicate step ID detected: '${id}'. Step IDs must be unique within a workflow.`);
72
+ }
73
+ steps.set(id, stepDefinition);
74
+ }
75
+ }
76
+ }
77
+ ts.forEachChild(node, visit);
78
+ }
79
+ visit(sourceFile);
80
+ return { steps: Array.from(steps.values()) };
81
+ }
82
+
83
+ // src/db/migration.ts
84
+ async function runMigrations(db) {
85
+ const tableExistsResult = await db.executeSql(`
86
+ SELECT EXISTS (
87
+ SELECT FROM information_schema.tables
88
+ WHERE table_schema = 'public'
89
+ AND table_name = 'workflow_runs'
90
+ );
91
+ `, []);
92
+ if (!tableExistsResult.rows[0]?.exists) {
93
+ await db.executeSql(`
94
+ CREATE TABLE workflow_runs (
95
+ id varchar(32) PRIMARY KEY NOT NULL,
96
+ created_at timestamp with time zone DEFAULT now() NOT NULL,
97
+ updated_at timestamp with time zone DEFAULT now() NOT NULL,
98
+ resource_id varchar(32),
99
+ workflow_id varchar(32) NOT NULL,
100
+ status text DEFAULT 'pending' NOT NULL,
101
+ input jsonb NOT NULL,
102
+ output jsonb,
103
+ error text,
104
+ current_step_id varchar(256) NOT NULL,
105
+ timeline jsonb DEFAULT '{}'::jsonb NOT NULL,
106
+ paused_at timestamp with time zone,
107
+ resumed_at timestamp with time zone,
108
+ completed_at timestamp with time zone,
109
+ timeout_at timestamp with time zone,
110
+ retry_count integer DEFAULT 0 NOT NULL,
111
+ max_retries integer DEFAULT 0 NOT NULL,
112
+ job_id varchar(256)
113
+ );
114
+ `, []);
115
+ await db.executeSql(`
116
+ CREATE INDEX workflow_runs_workflow_id_idx ON workflow_runs USING btree (workflow_id);
117
+ `, []);
118
+ await db.executeSql(`
119
+ CREATE INDEX workflow_runs_created_at_idx ON workflow_runs USING btree (created_at);
120
+ `, []);
121
+ await db.executeSql(`
122
+ CREATE INDEX workflow_runs_resource_id_idx ON workflow_runs USING btree (resource_id);
123
+ `, []);
124
+ }
125
+ }
126
+
127
+ // src/db/queries.ts
128
+ import ksuid from "ksuid";
129
+ function generateKSUID(prefix) {
130
+ return `${prefix ? `${prefix}_` : ""}${ksuid.randomSync().string}`;
131
+ }
132
+ function mapRowToWorkflowRun(row) {
133
+ return {
134
+ id: row.id,
135
+ createdAt: new Date(row.created_at),
136
+ updatedAt: new Date(row.updated_at),
137
+ resourceId: row.resource_id,
138
+ workflowId: row.workflow_id,
139
+ status: row.status,
140
+ input: typeof row.input === "string" ? JSON.parse(row.input) : row.input,
141
+ output: typeof row.output === "string" ? row.output.trim().startsWith("{") || row.output.trim().startsWith("[") ? JSON.parse(row.output) : row.output : row.output ?? null,
142
+ error: row.error,
143
+ currentStepId: row.current_step_id,
144
+ timeline: typeof row.timeline === "string" ? JSON.parse(row.timeline) : row.timeline,
145
+ pausedAt: row.paused_at ? new Date(row.paused_at) : null,
146
+ resumedAt: row.resumed_at ? new Date(row.resumed_at) : null,
147
+ completedAt: row.completed_at ? new Date(row.completed_at) : null,
148
+ timeoutAt: row.timeout_at ? new Date(row.timeout_at) : null,
149
+ retryCount: row.retry_count,
150
+ maxRetries: row.max_retries,
151
+ jobId: row.job_id
152
+ };
153
+ }
154
+ async function insertWorkflowRun({
155
+ resourceId,
156
+ workflowId,
157
+ currentStepId,
158
+ status,
159
+ input,
160
+ maxRetries,
161
+ timeoutAt
162
+ }, db) {
163
+ const runId = generateKSUID("run");
164
+ const now = new Date;
165
+ const result = await db.executeSql(`INSERT INTO workflow_runs (
166
+ id,
167
+ resource_id,
168
+ workflow_id,
169
+ current_step_id,
170
+ status,
171
+ input,
172
+ max_retries,
173
+ timeout_at,
174
+ created_at,
175
+ updated_at,
176
+ timeline,
177
+ retry_count
178
+ )
179
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
180
+ RETURNING *`, [
181
+ runId,
182
+ resourceId ?? null,
183
+ workflowId,
184
+ currentStepId,
185
+ status,
186
+ JSON.stringify(input),
187
+ maxRetries,
188
+ timeoutAt,
189
+ now,
190
+ now,
191
+ "{}",
192
+ 0
193
+ ]);
194
+ const insertedRun = result.rows[0];
195
+ if (!insertedRun) {
196
+ throw new Error("Failed to insert workflow run");
197
+ }
198
+ return mapRowToWorkflowRun(insertedRun);
199
+ }
200
+ async function getWorkflowRun({
201
+ runId,
202
+ resourceId
203
+ }, { exclusiveLock = false, db }) {
204
+ const lockSuffix = exclusiveLock ? "FOR UPDATE" : "";
205
+ const result = resourceId ? await db.executeSql(`SELECT * FROM workflow_runs
206
+ WHERE id = $1 AND resource_id = $2
207
+ ${lockSuffix}`, [runId, resourceId]) : await db.executeSql(`SELECT * FROM workflow_runs
208
+ WHERE id = $1
209
+ ${lockSuffix}`, [runId]);
210
+ const run = result.rows[0];
211
+ if (!run) {
212
+ return null;
213
+ }
214
+ return mapRowToWorkflowRun(run);
215
+ }
216
+ async function updateWorkflowRun({
217
+ runId,
218
+ resourceId,
219
+ data
220
+ }, db) {
221
+ const now = new Date;
222
+ const updates = ["updated_at = $1"];
223
+ const values = [now];
224
+ let paramIndex = 2;
225
+ if (data.status !== undefined) {
226
+ updates.push(`status = $${paramIndex}`);
227
+ values.push(data.status);
228
+ paramIndex++;
229
+ }
230
+ if (data.currentStepId !== undefined) {
231
+ updates.push(`current_step_id = $${paramIndex}`);
232
+ values.push(data.currentStepId);
233
+ paramIndex++;
234
+ }
235
+ if (data.timeline !== undefined) {
236
+ updates.push(`timeline = $${paramIndex}`);
237
+ values.push(JSON.stringify(data.timeline));
238
+ paramIndex++;
239
+ }
240
+ if (data.pausedAt !== undefined) {
241
+ updates.push(`paused_at = $${paramIndex}`);
242
+ values.push(data.pausedAt);
243
+ paramIndex++;
244
+ }
245
+ if (data.resumedAt !== undefined) {
246
+ updates.push(`resumed_at = $${paramIndex}`);
247
+ values.push(data.resumedAt);
248
+ paramIndex++;
249
+ }
250
+ if (data.completedAt !== undefined) {
251
+ updates.push(`completed_at = $${paramIndex}`);
252
+ values.push(data.completedAt);
253
+ paramIndex++;
254
+ }
255
+ if (data.output !== undefined) {
256
+ updates.push(`output = $${paramIndex}`);
257
+ values.push(JSON.stringify(data.output));
258
+ paramIndex++;
259
+ }
260
+ if (data.error !== undefined) {
261
+ updates.push(`error = $${paramIndex}`);
262
+ values.push(data.error);
263
+ paramIndex++;
264
+ }
265
+ if (data.retryCount !== undefined) {
266
+ updates.push(`retry_count = $${paramIndex}`);
267
+ values.push(data.retryCount);
268
+ paramIndex++;
269
+ }
270
+ if (data.jobId !== undefined) {
271
+ updates.push(`job_id = $${paramIndex}`);
272
+ values.push(data.jobId);
273
+ paramIndex++;
274
+ }
275
+ const whereClause = resourceId ? `WHERE id = $${paramIndex} AND resource_id = $${paramIndex + 1}` : `WHERE id = $${paramIndex}`;
276
+ values.push(runId);
277
+ if (resourceId) {
278
+ values.push(resourceId);
279
+ }
280
+ const query = `
281
+ UPDATE workflow_runs
282
+ SET ${updates.join(", ")}
283
+ ${whereClause}
284
+ RETURNING *
285
+ `;
286
+ const result = await db.executeSql(query, values);
287
+ const run = result.rows[0];
288
+ if (!run) {
289
+ return null;
290
+ }
291
+ return mapRowToWorkflowRun(run);
292
+ }
293
+ async function getWorkflowRuns({
294
+ resourceId,
295
+ startingAfter,
296
+ endingBefore,
297
+ limit = 20,
298
+ statuses,
299
+ workflowId
300
+ }, db) {
301
+ const conditions = [];
302
+ const values = [];
303
+ let paramIndex = 1;
304
+ if (resourceId) {
305
+ conditions.push(`resource_id = $${paramIndex}`);
306
+ values.push(resourceId);
307
+ paramIndex++;
308
+ }
309
+ if (statuses && statuses.length > 0) {
310
+ conditions.push(`status = ANY($${paramIndex})`);
311
+ values.push(statuses);
312
+ paramIndex++;
313
+ }
314
+ if (workflowId) {
315
+ conditions.push(`workflow_id = $${paramIndex}`);
316
+ values.push(workflowId);
317
+ paramIndex++;
318
+ }
319
+ if (startingAfter) {
320
+ const cursorResult = await db.executeSql("SELECT created_at FROM workflow_runs WHERE id = $1 LIMIT 1", [startingAfter]);
321
+ if (cursorResult.rows[0]?.created_at) {
322
+ conditions.push(`created_at < $${paramIndex}`);
323
+ values.push(typeof cursorResult.rows[0].created_at === "string" ? new Date(cursorResult.rows[0].created_at) : cursorResult.rows[0].created_at);
324
+ paramIndex++;
325
+ }
326
+ }
327
+ if (endingBefore) {
328
+ const cursorResult = await db.executeSql("SELECT created_at FROM workflow_runs WHERE id = $1 LIMIT 1", [endingBefore]);
329
+ if (cursorResult.rows[0]?.created_at) {
330
+ conditions.push(`created_at > $${paramIndex}`);
331
+ values.push(typeof cursorResult.rows[0].created_at === "string" ? new Date(cursorResult.rows[0].created_at) : cursorResult.rows[0].created_at);
332
+ paramIndex++;
333
+ }
334
+ }
335
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
336
+ const actualLimit = Math.min(Math.max(limit, 1), 100) + 1;
337
+ const query = `
338
+ SELECT * FROM workflow_runs
339
+ ${whereClause}
340
+ ORDER BY created_at DESC
341
+ LIMIT $${paramIndex}
342
+ `;
343
+ values.push(actualLimit);
344
+ const result = await db.executeSql(query, values);
345
+ const rows = result.rows;
346
+ const hasMore = rows.length > (limit ?? 20);
347
+ const rawItems = hasMore ? rows.slice(0, limit) : rows;
348
+ const items = rawItems.map((row) => mapRowToWorkflowRun(row));
349
+ const hasPrev = !!endingBefore;
350
+ const nextCursor = hasMore && items.length > 0 ? items[items.length - 1]?.id ?? null : null;
351
+ const prevCursor = hasPrev && items.length > 0 ? items[0]?.id ?? null : null;
352
+ return { items, nextCursor, prevCursor, hasMore, hasPrev };
353
+ }
354
+ async function withPostgresTransaction(db, callback) {
355
+ try {
356
+ await db.executeSql("BEGIN", []);
357
+ const result = await callback(db);
358
+ await db.executeSql("COMMIT", []);
359
+ return result;
360
+ } catch (error) {
361
+ await db.executeSql("ROLLBACK", []);
362
+ throw error;
363
+ }
364
+ }
365
+
366
+ // src/error.ts
367
+ class WorkflowEngineError extends Error {
368
+ workflowId;
369
+ runId;
370
+ cause;
371
+ constructor(message, workflowId, runId, cause = undefined) {
372
+ super(message);
373
+ this.workflowId = workflowId;
374
+ this.runId = runId;
375
+ this.cause = cause;
376
+ this.name = "WorkflowEngineError";
377
+ if (Error.captureStackTrace) {
378
+ Error.captureStackTrace(this, WorkflowEngineError);
379
+ }
380
+ }
381
+ }
382
+
383
+ class WorkflowRunNotFoundError extends WorkflowEngineError {
384
+ constructor(runId, workflowId) {
385
+ super("Workflow run not found", workflowId, runId);
386
+ this.name = "WorkflowRunNotFoundError";
387
+ }
388
+ }
389
+
390
+ // src/types.ts
391
+ var WorkflowStatus;
392
+ ((WorkflowStatus2) => {
393
+ WorkflowStatus2["PENDING"] = "pending";
394
+ WorkflowStatus2["RUNNING"] = "running";
395
+ WorkflowStatus2["PAUSED"] = "paused";
396
+ WorkflowStatus2["COMPLETED"] = "completed";
397
+ WorkflowStatus2["FAILED"] = "failed";
398
+ WorkflowStatus2["CANCELLED"] = "cancelled";
399
+ })(WorkflowStatus ||= {});
400
+ var StepType;
401
+ ((StepType2) => {
402
+ StepType2["PAUSE"] = "pause";
403
+ StepType2["RUN"] = "run";
404
+ StepType2["WAIT_FOR"] = "waitFor";
405
+ StepType2["WAIT_UNTIL"] = "waitUntil";
406
+ })(StepType ||= {});
407
+
408
+ // src/engine.ts
409
+ var PAUSE_EVENT_NAME = "__internal_pause";
410
+ var WORKFLOW_RUN_QUEUE_NAME = "workflow-run";
411
+ var LOG_PREFIX = "[WorkflowEngine]";
412
+ var StepTypeToIcon = {
413
+ ["run" /* RUN */]: "λ",
414
+ ["waitFor" /* WAIT_FOR */]: "○",
415
+ ["pause" /* PAUSE */]: "⏸",
416
+ ["waitUntil" /* WAIT_UNTIL */]: "⏲"
417
+ };
418
+ var defaultLogger = {
419
+ log: (_message) => console.warn(_message),
420
+ error: (message, error) => console.error(message, error)
421
+ };
422
+ var defaultExpireInSeconds = process.env.WORKFLOW_RUN_EXPIRE_IN_SECONDS ? Number.parseInt(process.env.WORKFLOW_RUN_EXPIRE_IN_SECONDS, 10) : 5 * 60;
423
+
424
+ class WorkflowEngine {
425
+ boss;
426
+ db;
427
+ unregisteredWorkflows = new Map;
428
+ _started = false;
429
+ workflows = new Map;
430
+ logger;
431
+ constructor({
432
+ workflows,
433
+ logger,
434
+ boss
435
+ } = {}) {
436
+ this.logger = this.buildLogger(logger ?? defaultLogger);
437
+ if (workflows) {
438
+ this.unregisteredWorkflows = new Map(workflows.map((workflow2) => [workflow2.id, workflow2]));
439
+ }
440
+ if (!boss) {
441
+ throw new WorkflowEngineError("PgBoss instance is required in constructor");
442
+ }
443
+ this.boss = boss;
444
+ this.db = boss.getDb();
445
+ }
446
+ async start(asEngine = true, { batchSize } = { batchSize: 1 }) {
447
+ if (this._started) {
448
+ return;
449
+ }
450
+ await this.boss.start();
451
+ await runMigrations(this.boss.getDb());
452
+ if (this.unregisteredWorkflows.size > 0) {
453
+ for (const workflow2 of this.unregisteredWorkflows.values()) {
454
+ await this.registerWorkflow(workflow2);
455
+ }
456
+ }
457
+ await this.boss.createQueue(WORKFLOW_RUN_QUEUE_NAME);
458
+ const numWorkers = +(process.env.WORKFLOW_RUN_WORKERS ?? 3);
459
+ if (asEngine) {
460
+ for (let i = 0;i < numWorkers; i++) {
461
+ await this.boss.work(WORKFLOW_RUN_QUEUE_NAME, { pollingIntervalSeconds: 0.5, batchSize }, (job) => this.handleWorkflowRun(job));
462
+ this.logger.log(`Worker ${i + 1}/${numWorkers} started for queue ${WORKFLOW_RUN_QUEUE_NAME}`);
463
+ }
464
+ }
465
+ this._started = true;
466
+ this.logger.log("Workflow engine started!");
467
+ }
468
+ async stop() {
469
+ await this.boss.stop();
470
+ this._started = false;
471
+ this.logger.log("Workflow engine stopped");
472
+ }
473
+ async registerWorkflow(definition) {
474
+ if (this.workflows.has(definition.id)) {
475
+ throw new WorkflowEngineError(`Workflow ${definition.id} is already registered`, definition.id);
476
+ }
477
+ const { steps } = parseWorkflowHandler(definition.handler);
478
+ this.workflows.set(definition.id, {
479
+ ...definition,
480
+ steps
481
+ });
482
+ this.logger.log(`Registered workflow "${definition.id}" with steps:`);
483
+ for (const step of steps.values()) {
484
+ const tags = [];
485
+ if (step.conditional)
486
+ tags.push("[conditional]");
487
+ if (step.loop)
488
+ tags.push("[loop]");
489
+ if (step.isDynamic)
490
+ tags.push("[dynamic]");
491
+ this.logger.log(` └─ (${StepTypeToIcon[step.type]}) ${step.id} ${tags.join(" ")}`);
492
+ }
493
+ return this;
494
+ }
495
+ async unregisterWorkflow(workflowId) {
496
+ this.workflows.delete(workflowId);
497
+ return this;
498
+ }
499
+ async unregisterAllWorkflows() {
500
+ this.workflows.clear();
501
+ return this;
502
+ }
503
+ async startWorkflow({
504
+ resourceId,
505
+ workflowId,
506
+ input,
507
+ options
508
+ }) {
509
+ if (!this._started) {
510
+ await this.start(false, { batchSize: options?.batchSize ?? 1 });
511
+ }
512
+ const workflow2 = this.workflows.get(workflowId);
513
+ if (!workflow2) {
514
+ throw new WorkflowEngineError(`Unknown workflow ${workflowId}`);
515
+ }
516
+ if (workflow2.steps.length === 0 || !workflow2.steps[0]) {
517
+ throw new WorkflowEngineError(`Workflow ${workflowId} has no steps`, workflowId);
518
+ }
519
+ const initialStepId = workflow2.steps[0]?.id;
520
+ const run = await withPostgresTransaction(this.boss.getDb(), async (db) => {
521
+ const timeoutAt = options?.timeout ? new Date(Date.now() + options.timeout) : workflow2.timeout ? new Date(Date.now() + workflow2.timeout) : null;
522
+ const insertedRun = await insertWorkflowRun({
523
+ resourceId,
524
+ workflowId,
525
+ currentStepId: initialStepId,
526
+ status: "running" /* RUNNING */,
527
+ input,
528
+ maxRetries: options?.retries ?? workflow2.retries ?? 0,
529
+ timeoutAt
530
+ }, this.boss.getDb());
531
+ const job = {
532
+ runId: insertedRun.id,
533
+ resourceId,
534
+ workflowId,
535
+ input
536
+ };
537
+ await this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {
538
+ startAfter: new Date,
539
+ expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds
540
+ });
541
+ return insertedRun;
542
+ });
543
+ this.logger.log("Started workflow run", {
544
+ runId: run.id,
545
+ workflowId
546
+ });
547
+ return run;
548
+ }
549
+ async pauseWorkflow({
550
+ runId,
551
+ resourceId
552
+ }) {
553
+ await this.checkIfHasStarted();
554
+ const run = await this.updateRun({
555
+ runId,
556
+ resourceId,
557
+ data: {
558
+ status: "paused" /* PAUSED */,
559
+ pausedAt: new Date
560
+ }
561
+ });
562
+ this.logger.log("Paused workflow run", {
563
+ runId,
564
+ workflowId: run.workflowId
565
+ });
566
+ return run;
567
+ }
568
+ async resumeWorkflow({
569
+ runId,
570
+ resourceId,
571
+ options
572
+ }) {
573
+ await this.checkIfHasStarted();
574
+ return this.triggerEvent({
575
+ runId,
576
+ resourceId,
577
+ eventName: PAUSE_EVENT_NAME,
578
+ data: {},
579
+ options
580
+ });
581
+ }
582
+ async cancelWorkflow({
583
+ runId,
584
+ resourceId
585
+ }) {
586
+ await this.checkIfHasStarted();
587
+ const run = await this.updateRun({
588
+ runId,
589
+ resourceId,
590
+ data: {
591
+ status: "cancelled" /* CANCELLED */
592
+ }
593
+ });
594
+ this.logger.log(`cancelled workflow run with id ${runId}`);
595
+ return run;
596
+ }
597
+ async triggerEvent({
598
+ runId,
599
+ resourceId,
600
+ eventName,
601
+ data,
602
+ options
603
+ }) {
604
+ await this.checkIfHasStarted();
605
+ const run = await this.getRun({ runId, resourceId });
606
+ const job = {
607
+ runId: run.id,
608
+ resourceId,
609
+ workflowId: run.workflowId,
610
+ input: run.input,
611
+ event: {
612
+ name: eventName,
613
+ data
614
+ }
615
+ };
616
+ this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {
617
+ expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds
618
+ });
619
+ this.logger.log(`event ${eventName} sent for workflow run with id ${runId}`);
620
+ return run;
621
+ }
622
+ async getRun({ runId, resourceId }, { exclusiveLock = false, db } = {}) {
623
+ const run = await getWorkflowRun({ runId, resourceId }, { exclusiveLock, db: db ?? this.db });
624
+ if (!run) {
625
+ throw new WorkflowRunNotFoundError(runId);
626
+ }
627
+ return run;
628
+ }
629
+ async updateRun({
630
+ runId,
631
+ resourceId,
632
+ data
633
+ }, { db } = {}) {
634
+ const run = await updateWorkflowRun({ runId, resourceId, data }, db ?? this.db);
635
+ if (!run) {
636
+ throw new WorkflowRunNotFoundError(runId);
637
+ }
638
+ return run;
639
+ }
640
+ async checkProgress({
641
+ runId,
642
+ resourceId
643
+ }) {
644
+ const run = await this.getRun({ runId, resourceId });
645
+ const workflow2 = this.workflows.get(run.workflowId);
646
+ if (!workflow2) {
647
+ throw new WorkflowEngineError(`Workflow ${run.workflowId} not found`, run.workflowId, runId);
648
+ }
649
+ const steps = workflow2?.steps ?? [];
650
+ let completionPercentage = 0;
651
+ let completedSteps = 0;
652
+ if (steps.length > 0) {
653
+ completedSteps = Object.values(run.timeline).filter((step) => typeof step === "object" && step !== null && ("output" in step) && step.output !== undefined).length;
654
+ if (run.status === "completed" /* COMPLETED */) {
655
+ completionPercentage = 100;
656
+ } else if (run.status === "failed" /* FAILED */ || run.status === "cancelled" /* CANCELLED */) {
657
+ completionPercentage = Math.min(completedSteps / steps.length * 100, 100);
658
+ } else {
659
+ const currentStepIndex = steps.findIndex((step) => step.id === run.currentStepId);
660
+ if (currentStepIndex >= 0) {
661
+ completionPercentage = currentStepIndex / steps.length * 100;
662
+ } else {
663
+ const completedSteps2 = Object.keys(run.timeline).length;
664
+ completionPercentage = Math.min(completedSteps2 / steps.length * 100, 100);
665
+ }
666
+ }
667
+ }
668
+ return {
669
+ ...run,
670
+ completedSteps,
671
+ completionPercentage: Math.round(completionPercentage * 100) / 100,
672
+ totalSteps: steps.length
673
+ };
674
+ }
675
+ async handleWorkflowRun([job]) {
676
+ const { runId, resourceId, workflowId, input, event } = job?.data ?? {};
677
+ if (!runId) {
678
+ throw new WorkflowEngineError("Invalid workflow run job, missing runId", workflowId);
679
+ }
680
+ if (!resourceId) {
681
+ throw new WorkflowEngineError("Invalid workflow run job, missing resourceId", workflowId);
682
+ }
683
+ if (!workflowId) {
684
+ throw new WorkflowEngineError("Invalid workflow run job, missing workflowId", undefined, runId);
685
+ }
686
+ const workflow2 = this.workflows.get(workflowId);
687
+ if (!workflow2) {
688
+ throw new WorkflowEngineError(`Workflow ${workflowId} not found`, workflowId, runId);
689
+ }
690
+ this.logger.log("Processing workflow run...", {
691
+ runId,
692
+ workflowId
693
+ });
694
+ let run = await this.getRun({ runId, resourceId });
695
+ try {
696
+ if (run.status === "cancelled" /* CANCELLED */) {
697
+ this.logger.log(`Workflow run ${runId} is cancelled, skipping`);
698
+ return;
699
+ }
700
+ if (!run.currentStepId) {
701
+ throw new WorkflowEngineError("Missing current step id", workflowId, runId);
702
+ }
703
+ if (run.status === "paused" /* PAUSED */) {
704
+ const waitForStepEntry = run.timeline[`${run.currentStepId}-wait-for`];
705
+ const waitForStep = waitForStepEntry && typeof waitForStepEntry === "object" && "waitFor" in waitForStepEntry ? waitForStepEntry : null;
706
+ const currentStepEntry = run.timeline[run.currentStepId];
707
+ const currentStep = currentStepEntry && typeof currentStepEntry === "object" && "output" in currentStepEntry ? currentStepEntry : null;
708
+ const waitFor = waitForStep?.waitFor;
709
+ const hasCurrentStepOutput = currentStep?.output !== undefined;
710
+ if (waitFor && waitFor.eventName === event?.name && !hasCurrentStepOutput) {
711
+ run = await this.updateRun({
712
+ runId,
713
+ resourceId,
714
+ data: {
715
+ status: "running" /* RUNNING */,
716
+ pausedAt: null,
717
+ resumedAt: new Date,
718
+ timeline: merge(run.timeline, {
719
+ [run.currentStepId]: {
720
+ output: event?.data ?? {},
721
+ timestamp: new Date
722
+ }
723
+ }),
724
+ jobId: job?.id
725
+ }
726
+ });
727
+ } else {
728
+ run = await this.updateRun({
729
+ runId,
730
+ resourceId,
731
+ data: {
732
+ status: "running" /* RUNNING */,
733
+ pausedAt: null,
734
+ resumedAt: new Date,
735
+ jobId: job?.id
736
+ }
737
+ });
738
+ }
739
+ }
740
+ const context = {
741
+ input: run.input,
742
+ workflowId: run.workflowId,
743
+ runId: run.id,
744
+ timeline: run.timeline,
745
+ logger: this.logger,
746
+ step: {
747
+ run: async (stepId, handler) => {
748
+ if (!run) {
749
+ throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
750
+ }
751
+ return this.runStep({
752
+ stepId,
753
+ run,
754
+ handler
755
+ });
756
+ },
757
+ waitFor: async (stepId, { eventName, timeout }) => {
758
+ if (!run) {
759
+ throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
760
+ }
761
+ return this.waitForEvent({
762
+ run,
763
+ stepId,
764
+ eventName,
765
+ timeout
766
+ });
767
+ },
768
+ waitUntil: async ({ date }) => {
769
+ return this.waitUntil(runId, date);
770
+ },
771
+ pause: async (stepId) => {
772
+ if (!run) {
773
+ throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
774
+ }
775
+ return this.pauseStep({
776
+ stepId,
777
+ run
778
+ });
779
+ }
780
+ }
781
+ };
782
+ const result = await workflow2.handler(context);
783
+ run = await this.getRun({ runId, resourceId });
784
+ if (run.status === "running" /* RUNNING */ && run.currentStepId === workflow2.steps[workflow2.steps.length - 1]?.id) {
785
+ const normalizedResult = result === undefined ? {} : result;
786
+ await this.updateRun({
787
+ runId,
788
+ resourceId,
789
+ data: {
790
+ status: "completed" /* COMPLETED */,
791
+ output: normalizedResult,
792
+ completedAt: new Date,
793
+ jobId: job?.id
794
+ }
795
+ });
796
+ this.logger.log("Workflow run completed.", {
797
+ runId,
798
+ workflowId
799
+ });
800
+ }
801
+ } catch (error) {
802
+ if (run.retryCount < run.maxRetries) {
803
+ await this.updateRun({
804
+ runId,
805
+ resourceId,
806
+ data: {
807
+ retryCount: run.retryCount + 1,
808
+ jobId: job?.id
809
+ }
810
+ });
811
+ const retryDelay = 2 ** run.retryCount * 1000;
812
+ const pgBossJob = {
813
+ runId,
814
+ resourceId,
815
+ workflowId,
816
+ input
817
+ };
818
+ await this.boss?.send("workflow-run", pgBossJob, { retryDelay });
819
+ return;
820
+ }
821
+ await this.updateRun({
822
+ runId,
823
+ resourceId,
824
+ data: {
825
+ status: "failed" /* FAILED */,
826
+ error: error instanceof Error ? error.message : String(error),
827
+ jobId: job?.id
828
+ }
829
+ });
830
+ throw error;
831
+ }
832
+ }
833
+ async runStep({
834
+ stepId,
835
+ run,
836
+ handler
837
+ }) {
838
+ return withPostgresTransaction(this.db, async (db) => {
839
+ const persistedRun = await this.getRun({ runId: run.id, resourceId: run.resourceId ?? undefined }, {
840
+ exclusiveLock: true,
841
+ db
842
+ });
843
+ if (persistedRun.status === "cancelled" /* CANCELLED */ || persistedRun.status === "paused" /* PAUSED */ || persistedRun.status === "failed" /* FAILED */) {
844
+ this.logger.log(`Step ${stepId} skipped, workflow run is ${persistedRun.status}`, {
845
+ runId: run.id,
846
+ workflowId: run.workflowId
847
+ });
848
+ return;
849
+ }
850
+ try {
851
+ let result;
852
+ const timelineStepEntry = persistedRun.timeline[stepId];
853
+ const timelineStep = timelineStepEntry && typeof timelineStepEntry === "object" && "output" in timelineStepEntry ? timelineStepEntry : null;
854
+ if (timelineStep?.output !== undefined) {
855
+ result = timelineStep.output;
856
+ } else {
857
+ await this.updateRun({
858
+ runId: run.id,
859
+ resourceId: run.resourceId ?? undefined,
860
+ data: {
861
+ currentStepId: stepId
862
+ }
863
+ }, { db });
864
+ this.logger.log(`Running step ${stepId}...`, {
865
+ runId: run.id,
866
+ workflowId: run.workflowId
867
+ });
868
+ result = await handler();
869
+ run = await this.updateRun({
870
+ runId: run.id,
871
+ resourceId: run.resourceId ?? undefined,
872
+ data: {
873
+ timeline: merge(run.timeline, {
874
+ [stepId]: {
875
+ output: result === undefined ? {} : result,
876
+ timestamp: new Date
877
+ }
878
+ })
879
+ }
880
+ }, { db });
881
+ }
882
+ const finalResult = result === undefined ? {} : result;
883
+ return finalResult;
884
+ } catch (error) {
885
+ this.logger.error(`Step ${stepId} failed:`, error, {
886
+ runId: run.id,
887
+ workflowId: run.workflowId
888
+ });
889
+ await this.updateRun({
890
+ runId: run.id,
891
+ resourceId: run.resourceId ?? undefined,
892
+ data: {
893
+ status: "failed" /* FAILED */,
894
+ error: error instanceof Error ? `${error.message}
895
+ ${error.stack}` : String(error)
896
+ }
897
+ }, { db });
898
+ throw error;
899
+ }
900
+ });
901
+ }
902
+ async waitForEvent({
903
+ run,
904
+ stepId,
905
+ eventName,
906
+ timeout
907
+ }) {
908
+ const persistedRun = await this.getRun({
909
+ runId: run.id,
910
+ resourceId: run.resourceId ?? undefined
911
+ });
912
+ if (persistedRun.status === "cancelled" /* CANCELLED */ || persistedRun.status === "paused" /* PAUSED */ || persistedRun.status === "failed" /* FAILED */) {
913
+ this.logger.log(`Step ${stepId} skipped, workflow run is ${persistedRun.status}`, {
914
+ runId: run.id,
915
+ workflowId: run.workflowId
916
+ });
917
+ return;
918
+ }
919
+ const timelineStepCheckEntry = persistedRun.timeline[stepId];
920
+ const timelineStepCheck = timelineStepCheckEntry && typeof timelineStepCheckEntry === "object" && "output" in timelineStepCheckEntry ? timelineStepCheckEntry : null;
921
+ if (timelineStepCheck?.output !== undefined) {
922
+ return timelineStepCheck.output;
923
+ }
924
+ await this.updateRun({
925
+ runId: run.id,
926
+ resourceId: run.resourceId ?? undefined,
927
+ data: {
928
+ status: "paused" /* PAUSED */,
929
+ currentStepId: stepId,
930
+ timeline: merge(run.timeline, {
931
+ [`${stepId}-wait-for`]: {
932
+ waitFor: {
933
+ eventName,
934
+ timeout
935
+ },
936
+ timestamp: new Date
937
+ }
938
+ }),
939
+ pausedAt: new Date
940
+ }
941
+ });
942
+ this.logger.log(`Running step ${stepId}, waiting for event ${eventName}...`, {
943
+ runId: run.id,
944
+ workflowId: run.workflowId
945
+ });
946
+ }
947
+ async pauseStep({ stepId, run }) {
948
+ await this.waitForEvent({
949
+ run,
950
+ stepId,
951
+ eventName: PAUSE_EVENT_NAME
952
+ });
953
+ }
954
+ async waitUntil(runId, _date) {
955
+ throw new WorkflowEngineError("Not implemented yet", undefined, runId);
956
+ }
957
+ async checkIfHasStarted() {
958
+ if (!this._started) {
959
+ throw new WorkflowEngineError("Workflow engine not started");
960
+ }
961
+ }
962
+ buildLogger(logger) {
963
+ return {
964
+ log: (message, context) => {
965
+ const { runId, workflowId } = context ?? {};
966
+ const parts = [LOG_PREFIX, workflowId, runId].filter(Boolean).join(" ");
967
+ logger.log(`${parts}: ${message}`);
968
+ },
969
+ error: (message, error, context) => {
970
+ const { runId, workflowId } = context ?? {};
971
+ const parts = [LOG_PREFIX, workflowId, runId].filter(Boolean).join(" ");
972
+ logger.error(`${parts}: ${message}`, error);
973
+ }
974
+ };
975
+ }
976
+ async getRuns({
977
+ resourceId,
978
+ startingAfter,
979
+ endingBefore,
980
+ limit = 20,
981
+ statuses,
982
+ workflowId
983
+ }) {
984
+ return getWorkflowRuns({
985
+ resourceId,
986
+ startingAfter,
987
+ endingBefore,
988
+ limit,
989
+ statuses,
990
+ workflowId
991
+ }, this.db);
992
+ }
993
+ }
994
+ export {
995
+ workflow,
996
+ WorkflowStatus,
997
+ WorkflowRunNotFoundError,
998
+ WorkflowEngineError,
999
+ WorkflowEngine,
1000
+ StepType
1001
+ };
1002
+
1003
+ //# debugId=CC18DB5304091C0964756E2164756E21
1004
+ //# sourceMappingURL=index.js.map