pg-workflows 0.13.1 → 0.14.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/client.entry.cjs +62 -5
- package/dist/client.entry.d.cts +17 -0
- package/dist/client.entry.d.ts +17 -0
- package/dist/client.entry.js +1 -1
- package/dist/client.entry.js.map +9 -8
- package/dist/index.cjs +125 -77
- package/dist/index.d.cts +20 -1
- package/dist/index.d.ts +20 -1
- package/dist/index.js +67 -74
- package/dist/index.js.map +11 -10
- package/dist/shared/{chunk-5xswmve7.js → chunk-w3qcrrwx.js} +64 -7
- package/dist/shared/chunk-w3qcrrwx.js.map +17 -0
- package/package.json +1 -1
- package/dist/shared/chunk-5xswmve7.js.map +0 -16
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DEFAULT_PGBOSS_SCHEMA,
|
|
3
3
|
PAUSE_EVENT_NAME,
|
|
4
|
+
STEP_BASE_METHOD_TYPES,
|
|
4
5
|
WORKFLOW_RUN_DLQ_QUEUE_NAME,
|
|
5
6
|
WORKFLOW_RUN_QUEUE_NAME,
|
|
6
7
|
WorkflowClient,
|
|
@@ -14,6 +15,7 @@ import {
|
|
|
14
15
|
insertWorkflowRun,
|
|
15
16
|
invokeChildWorkflowTimelineKey,
|
|
16
17
|
isInvokeChildWorkflowTimelineEntry,
|
|
18
|
+
resolvePriority,
|
|
17
19
|
runMigrations,
|
|
18
20
|
scheduleQueueNameFor,
|
|
19
21
|
updateWorkflowRun,
|
|
@@ -22,7 +24,7 @@ import {
|
|
|
22
24
|
waitForTimelineKey,
|
|
23
25
|
withPostgresTransaction,
|
|
24
26
|
workflow
|
|
25
|
-
} from "./shared/chunk-
|
|
27
|
+
} from "./shared/chunk-w3qcrrwx.js";
|
|
26
28
|
// src/engine.ts
|
|
27
29
|
import { merge } from "es-toolkit";
|
|
28
30
|
import pg from "pg";
|
|
@@ -73,11 +75,11 @@ function parseWorkflowHandler(handler) {
|
|
|
73
75
|
const propertyAccess = node.expression;
|
|
74
76
|
const objectName = propertyAccess.expression.getText(sourceFile);
|
|
75
77
|
const methodName = propertyAccess.name.text;
|
|
76
|
-
|
|
78
|
+
const stepType = objectName === "step" ? STEP_BASE_METHOD_TYPES.get(methodName) : undefined;
|
|
79
|
+
if (stepType !== undefined) {
|
|
77
80
|
const firstArg = node.arguments[0];
|
|
78
81
|
if (firstArg) {
|
|
79
82
|
const { id, isDynamic } = extractStepId(firstArg);
|
|
80
|
-
const stepType = methodName === "sleep" ? "delay" /* DELAY */ : methodName;
|
|
81
83
|
const stepDefinition = {
|
|
82
84
|
id,
|
|
83
85
|
type: stepType,
|
|
@@ -381,6 +383,9 @@ class WorkflowEngine {
|
|
|
381
383
|
});
|
|
382
384
|
return run;
|
|
383
385
|
}
|
|
386
|
+
async createChildWorkflowRun(params) {
|
|
387
|
+
return this.createWorkflowRun(params);
|
|
388
|
+
}
|
|
384
389
|
async createWorkflowRun({
|
|
385
390
|
workflowId,
|
|
386
391
|
input,
|
|
@@ -390,6 +395,7 @@ class WorkflowEngine {
|
|
|
390
395
|
parentRunId,
|
|
391
396
|
parentStepId,
|
|
392
397
|
parentResourceId,
|
|
398
|
+
parentPriority,
|
|
393
399
|
scheduledAt,
|
|
394
400
|
enqueue = true,
|
|
395
401
|
db
|
|
@@ -420,6 +426,7 @@ class WorkflowEngine {
|
|
|
420
426
|
status: "running" /* RUNNING */,
|
|
421
427
|
input,
|
|
422
428
|
maxRetries: options?.retries ?? workflow2.retries ?? 0,
|
|
429
|
+
priority: resolvePriority(options?.priority, workflow2.priority, parentPriority),
|
|
423
430
|
timeoutAt,
|
|
424
431
|
idempotencyKey,
|
|
425
432
|
parentRunId,
|
|
@@ -447,6 +454,7 @@ class WorkflowEngine {
|
|
|
447
454
|
await this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {
|
|
448
455
|
startAfter: new Date,
|
|
449
456
|
expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds,
|
|
457
|
+
priority: run.priority,
|
|
450
458
|
...retrySendOptions(run.maxRetries),
|
|
451
459
|
...db ? { db } : {}
|
|
452
460
|
});
|
|
@@ -596,6 +604,7 @@ class WorkflowEngine {
|
|
|
596
604
|
};
|
|
597
605
|
await this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {
|
|
598
606
|
expireInSeconds: options?.expireInSeconds ?? defaultExpireInSeconds,
|
|
607
|
+
priority: run.priority,
|
|
599
608
|
...retrySendOptions(run.maxRetries)
|
|
600
609
|
});
|
|
601
610
|
this.logger.log(`event ${eventName} sent for workflow run with id ${runId}`);
|
|
@@ -767,75 +776,7 @@ class WorkflowEngine {
|
|
|
767
776
|
}, { db });
|
|
768
777
|
}, this.pool);
|
|
769
778
|
}
|
|
770
|
-
|
|
771
|
-
run: async (stepId, handler) => {
|
|
772
|
-
if (!run) {
|
|
773
|
-
throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
|
|
774
|
-
}
|
|
775
|
-
return this.runStep({ stepId, run, handler });
|
|
776
|
-
},
|
|
777
|
-
waitFor: async (stepId, { eventName, timeout }) => {
|
|
778
|
-
if (!run) {
|
|
779
|
-
throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
|
|
780
|
-
}
|
|
781
|
-
const timeoutDate = timeout ? new Date(Date.now() + timeout) : undefined;
|
|
782
|
-
return this.waitStep({ run, stepId, eventName, timeoutDate });
|
|
783
|
-
},
|
|
784
|
-
waitUntil: async (stepId, dateOrOptions) => {
|
|
785
|
-
if (!run) {
|
|
786
|
-
throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
|
|
787
|
-
}
|
|
788
|
-
const date = dateOrOptions instanceof Date ? dateOrOptions : typeof dateOrOptions === "string" ? new Date(dateOrOptions) : dateOrOptions.date instanceof Date ? dateOrOptions.date : new Date(dateOrOptions.date);
|
|
789
|
-
await this.waitStep({ run, stepId, timeoutDate: date });
|
|
790
|
-
},
|
|
791
|
-
pause: async (stepId) => {
|
|
792
|
-
if (!run) {
|
|
793
|
-
throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
|
|
794
|
-
}
|
|
795
|
-
await this.waitStep({ run, stepId, eventName: PAUSE_EVENT_NAME });
|
|
796
|
-
},
|
|
797
|
-
delay: async (stepId, duration) => {
|
|
798
|
-
if (!run) {
|
|
799
|
-
throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
|
|
800
|
-
}
|
|
801
|
-
await this.waitStep({
|
|
802
|
-
run,
|
|
803
|
-
stepId,
|
|
804
|
-
timeoutDate: new Date(Date.now() + parseDuration(duration))
|
|
805
|
-
});
|
|
806
|
-
},
|
|
807
|
-
get sleep() {
|
|
808
|
-
return this.delay;
|
|
809
|
-
},
|
|
810
|
-
poll: async (stepId, conditionFn, options) => {
|
|
811
|
-
if (!run) {
|
|
812
|
-
throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
|
|
813
|
-
}
|
|
814
|
-
const intervalMs = parseDuration(options?.interval ?? "30s");
|
|
815
|
-
if (intervalMs < 30000) {
|
|
816
|
-
throw new WorkflowEngineError(`step.poll interval must be at least 30s (got ${intervalMs}ms)`, workflowId, runId);
|
|
817
|
-
}
|
|
818
|
-
const timeoutMs = options?.timeout ? parseDuration(options.timeout) : undefined;
|
|
819
|
-
return this.pollStep({ run, stepId, conditionFn, intervalMs, timeoutMs });
|
|
820
|
-
},
|
|
821
|
-
invokeChildWorkflow: async (stepId, refOrParams, inputArg, optionsArg) => {
|
|
822
|
-
if (!run) {
|
|
823
|
-
throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
|
|
824
|
-
}
|
|
825
|
-
const resolvedChildCall = this.resolveWorkflowRunParameters(refOrParams, inputArg, optionsArg);
|
|
826
|
-
const childWorkflowInvocation = {
|
|
827
|
-
run,
|
|
828
|
-
stepId,
|
|
829
|
-
workflowId: resolvedChildCall.workflowId,
|
|
830
|
-
input: resolvedChildCall.input,
|
|
831
|
-
options: resolvedChildCall.options,
|
|
832
|
-
resourceId: resolvedChildCall.resourceId,
|
|
833
|
-
idempotencyKey: resolvedChildCall.idempotencyKey
|
|
834
|
-
};
|
|
835
|
-
return this.invokeChildWorkflowStep(childWorkflowInvocation);
|
|
836
|
-
}
|
|
837
|
-
};
|
|
838
|
-
let step = { ...baseStep };
|
|
779
|
+
let step = this.createBaseStep(run, workflowId, runId);
|
|
839
780
|
const plugins = workflow2.plugins ?? [];
|
|
840
781
|
const context = {
|
|
841
782
|
input: run.input,
|
|
@@ -925,6 +866,55 @@ class WorkflowEngine {
|
|
|
925
866
|
workflowId: run.workflowId
|
|
926
867
|
});
|
|
927
868
|
}
|
|
869
|
+
createBaseStep(run, workflowId, runId) {
|
|
870
|
+
return {
|
|
871
|
+
run: async (stepId, handler) => {
|
|
872
|
+
return this.runStep({ stepId, run, handler });
|
|
873
|
+
},
|
|
874
|
+
waitFor: async (stepId, { eventName, timeout }) => {
|
|
875
|
+
const timeoutDate = timeout ? new Date(Date.now() + timeout) : undefined;
|
|
876
|
+
return this.waitStep({ run, stepId, eventName, timeoutDate });
|
|
877
|
+
},
|
|
878
|
+
waitUntil: async (stepId, dateOrOptions) => {
|
|
879
|
+
const date = dateOrOptions instanceof Date ? dateOrOptions : typeof dateOrOptions === "string" ? new Date(dateOrOptions) : dateOrOptions.date instanceof Date ? dateOrOptions.date : new Date(dateOrOptions.date);
|
|
880
|
+
await this.waitStep({ run, stepId, timeoutDate: date });
|
|
881
|
+
},
|
|
882
|
+
pause: async (stepId) => {
|
|
883
|
+
await this.waitStep({ run, stepId, eventName: PAUSE_EVENT_NAME });
|
|
884
|
+
},
|
|
885
|
+
delay: async (stepId, duration) => {
|
|
886
|
+
await this.waitStep({
|
|
887
|
+
run,
|
|
888
|
+
stepId,
|
|
889
|
+
timeoutDate: new Date(Date.now() + parseDuration(duration))
|
|
890
|
+
});
|
|
891
|
+
},
|
|
892
|
+
get sleep() {
|
|
893
|
+
return this.delay;
|
|
894
|
+
},
|
|
895
|
+
poll: async (stepId, conditionFn, options) => {
|
|
896
|
+
const intervalMs = parseDuration(options?.interval ?? "30s");
|
|
897
|
+
if (intervalMs < 30000) {
|
|
898
|
+
throw new WorkflowEngineError(`step.poll interval must be at least 30s (got ${intervalMs}ms)`, workflowId, runId);
|
|
899
|
+
}
|
|
900
|
+
const timeoutMs = options?.timeout ? parseDuration(options.timeout) : undefined;
|
|
901
|
+
return this.pollStep({ run, stepId, conditionFn, intervalMs, timeoutMs });
|
|
902
|
+
},
|
|
903
|
+
invokeChildWorkflow: async (stepId, refOrParams, inputArg, optionsArg) => {
|
|
904
|
+
const resolvedChildCall = this.resolveWorkflowRunParameters(refOrParams, inputArg, optionsArg);
|
|
905
|
+
const childWorkflowInvocation = {
|
|
906
|
+
run,
|
|
907
|
+
stepId,
|
|
908
|
+
workflowId: resolvedChildCall.workflowId,
|
|
909
|
+
input: resolvedChildCall.input,
|
|
910
|
+
options: resolvedChildCall.options,
|
|
911
|
+
resourceId: resolvedChildCall.resourceId,
|
|
912
|
+
idempotencyKey: resolvedChildCall.idempotencyKey
|
|
913
|
+
};
|
|
914
|
+
return this.invokeChildWorkflowStep(childWorkflowInvocation);
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
}
|
|
928
918
|
getCachedStepEntry(timeline, stepId) {
|
|
929
919
|
const stepEntry = timeline[stepId];
|
|
930
920
|
return stepEntry && typeof stepEntry === "object" && "output" in stepEntry ? stepEntry : null;
|
|
@@ -1015,7 +1005,7 @@ class WorkflowEngine {
|
|
|
1015
1005
|
});
|
|
1016
1006
|
return;
|
|
1017
1007
|
}
|
|
1018
|
-
const result = await this.
|
|
1008
|
+
const result = await this.createChildWorkflowRun({
|
|
1019
1009
|
workflowId,
|
|
1020
1010
|
input,
|
|
1021
1011
|
resourceId: childResourceId,
|
|
@@ -1024,6 +1014,7 @@ class WorkflowEngine {
|
|
|
1024
1014
|
parentRunId: run.id,
|
|
1025
1015
|
parentStepId: stepId,
|
|
1026
1016
|
parentResourceId: run.resourceId ?? undefined,
|
|
1017
|
+
parentPriority: run.priority,
|
|
1027
1018
|
enqueue: true,
|
|
1028
1019
|
db
|
|
1029
1020
|
});
|
|
@@ -1222,6 +1213,7 @@ ${error.stack}` : String(error)
|
|
|
1222
1213
|
await this.boss.send(WORKFLOW_RUN_QUEUE_NAME, job, {
|
|
1223
1214
|
startAfter: timeoutDate.getTime() <= Date.now() ? new Date : timeoutDate,
|
|
1224
1215
|
expireInSeconds: defaultExpireInSeconds,
|
|
1216
|
+
priority: run.priority,
|
|
1225
1217
|
...retrySendOptions(run.maxRetries)
|
|
1226
1218
|
});
|
|
1227
1219
|
} catch (error) {
|
|
@@ -1340,6 +1332,7 @@ ${error.stack}` : String(error)
|
|
|
1340
1332
|
}, {
|
|
1341
1333
|
startAfter: new Date(Date.now() + intervalMs),
|
|
1342
1334
|
expireInSeconds: defaultExpireInSeconds,
|
|
1335
|
+
priority: run.priority,
|
|
1343
1336
|
...retrySendOptions(run.maxRetries)
|
|
1344
1337
|
});
|
|
1345
1338
|
} catch (error) {
|
|
@@ -1581,5 +1574,5 @@ export {
|
|
|
1581
1574
|
WorkflowClient
|
|
1582
1575
|
};
|
|
1583
1576
|
|
|
1584
|
-
//# debugId=
|
|
1577
|
+
//# debugId=A6C8F39FA22E601264756E2164756E21
|
|
1585
1578
|
//# sourceMappingURL=index.js.map
|