@hatchet-dev/typescript-sdk 1.0.0-alpha1 → 1.0.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/clients/admin/admin-client.d.ts +15 -20
- package/clients/admin/admin-client.js +21 -17
- package/clients/hatchet-client/hatchet-client.d.ts +5 -4
- package/clients/hatchet-client/hatchet-client.js +6 -4
- package/clients/listeners/durable-listener/durable-listener-client.d.ts +25 -0
- package/clients/listeners/durable-listener/durable-listener-client.js +30 -0
- package/clients/listeners/durable-listener/pooled-durable-listener-client.d.ts +43 -0
- package/clients/listeners/durable-listener/pooled-durable-listener-client.js +241 -0
- package/clients/{listener/listener-client.d.ts → listeners/run-listener/child-listener-client.d.ts} +8 -8
- package/clients/{listener/listener-client.js → listeners/run-listener/child-listener-client.js} +8 -19
- package/clients/{listener/child-listener-client.d.ts → listeners/run-listener/pooled-child-listener-client.d.ts} +5 -5
- package/clients/{listener/child-listener-client.js → listeners/run-listener/pooled-child-listener-client.js} +5 -5
- package/clients/rest/generated/Api.d.ts +49 -2
- package/clients/rest/generated/Api.js +30 -0
- package/clients/rest/generated/data-contracts.d.ts +88 -82
- package/clients/rest/generated/data-contracts.js +8 -1
- package/clients/worker/worker.d.ts +5 -1
- package/clients/worker/worker.js +173 -2
- package/examples/affinity-workers.js +5 -1
- package/examples/api.js +1 -1
- package/examples/bulk-fanout-worker.js +1 -1
- package/examples/byo-logger.js +4 -0
- package/examples/concurrency/cancel-in-progress/concurrency-event.js +2 -2
- package/examples/concurrency/group-round-robin/concurrency-event.js +1 -1
- package/examples/crons/programatic-crons.js +4 -4
- package/examples/dag-worker.js +0 -1
- package/examples/example-event.js +2 -2
- package/examples/logger.js +2 -0
- package/examples/on-failure.js +1 -1
- package/examples/rate-limit/events.js +3 -3
- package/examples/scheduled-runs/programatic-schedules.js +4 -4
- package/examples/sticky-worker-with-check.js +2 -2
- package/examples/sticky-worker.js +5 -0
- package/index.d.ts +1 -3
- package/index.js +1 -3
- package/package.json +2 -2
- package/protoc/dispatcher/dispatcher.d.ts +2 -0
- package/protoc/dispatcher/dispatcher.js +1 -1
- package/protoc/events/events.js +1 -1
- package/protoc/google/protobuf/timestamp.js +1 -1
- package/protoc/v1/dispatcher.d.ts +77 -0
- package/protoc/v1/dispatcher.js +341 -0
- package/protoc/v1/shared/condition.d.ts +59 -0
- package/protoc/v1/shared/condition.js +549 -0
- package/protoc/v1/workflows.d.ts +263 -0
- package/protoc/v1/workflows.js +1823 -0
- package/protoc/workflows/workflows.js +1 -1
- package/step.d.ts +173 -47
- package/step.js +208 -64
- package/util/sleep.d.ts +7 -0
- package/util/sleep.js +7 -0
- package/util/workflow-run-ref.d.ts +8 -2
- package/util/workflow-run-ref.js +13 -0
- package/v1/client/client.d.ts +125 -7
- package/v1/client/client.interface.d.ts +9 -1
- package/v1/client/client.js +148 -17
- package/v1/client/duration.d.ts +7 -0
- package/v1/client/duration.js +2 -0
- package/v1/client/features/index.d.ts +5 -0
- package/v1/client/features/index.js +21 -0
- package/v1/client/features/metrics.d.ts +14 -0
- package/v1/client/features/metrics.js +45 -0
- package/v1/client/features/ratelimits.d.ts +20 -0
- package/v1/client/features/ratelimits.js +40 -0
- package/v1/client/features/runs.d.ts +15 -0
- package/v1/client/features/runs.js +60 -0
- package/v1/client/features/workers.d.ts +14 -0
- package/v1/client/features/workers.js +56 -0
- package/v1/client/features/workflows.d.ts +17 -0
- package/v1/client/features/workflows.js +93 -0
- package/v1/client/worker.d.ts +25 -15
- package/v1/client/worker.js +76 -47
- package/v1/conditions/base.d.ts +18 -0
- package/v1/conditions/base.js +18 -0
- package/v1/conditions/index.d.ts +31 -0
- package/v1/conditions/index.js +91 -0
- package/v1/conditions/parent-condition.d.ts +40 -0
- package/v1/conditions/parent-condition.js +36 -0
- package/v1/conditions/sleep-condition.d.ts +47 -0
- package/v1/conditions/sleep-condition.js +38 -0
- package/v1/conditions/transformer.d.ts +5 -0
- package/v1/conditions/transformer.js +52 -0
- package/v1/conditions/user-event-condition.d.ts +50 -0
- package/v1/conditions/user-event-condition.js +39 -0
- package/v1/declaration.d.ts +309 -0
- package/v1/declaration.js +296 -0
- package/v1/examples/child_workflows/worker.js +2 -2
- package/v1/examples/child_workflows/workflow.d.ts +2 -2
- package/v1/examples/child_workflows/workflow.js +5 -3
- package/v1/examples/concurrency-rr/load.js +2 -2
- package/v1/examples/concurrency-rr/worker.js +2 -2
- package/v1/examples/concurrency-rr/workflow.d.ts +1 -1
- package/v1/examples/concurrency-rr/workflow.js +2 -3
- package/v1/examples/dag/worker.js +2 -2
- package/v1/examples/dag/workflow.d.ts +1 -1
- package/v1/examples/dag/workflow.js +3 -3
- package/v1/examples/dag_match_condition/event.js +28 -0
- package/v1/examples/dag_match_condition/run.d.ts +1 -0
- package/v1/examples/dag_match_condition/run.js +25 -0
- package/v1/examples/dag_match_condition/worker.d.ts +1 -0
- package/{examples/playground.js → v1/examples/dag_match_condition/worker.js} +7 -22
- package/v1/examples/dag_match_condition/workflow.d.ts +11 -0
- package/v1/examples/dag_match_condition/workflow.js +41 -0
- package/v1/examples/deep/worker.js +2 -2
- package/v1/examples/deep/workflow.d.ts +6 -6
- package/v1/examples/deep/workflow.js +7 -7
- package/v1/examples/durable-sleep/event.d.ts +1 -0
- package/v1/examples/durable-sleep/event.js +28 -0
- package/v1/examples/durable-sleep/run.d.ts +1 -0
- package/v1/examples/durable-sleep/run.js +30 -0
- package/v1/examples/durable-sleep/worker.d.ts +1 -0
- package/v1/examples/durable-sleep/worker.js +24 -0
- package/v1/examples/durable-sleep/workflow.d.ts +1 -0
- package/v1/examples/durable-sleep/workflow.js +37 -0
- package/v1/examples/inferred-typing/run.d.ts +1 -0
- package/v1/examples/inferred-typing/run.js +41 -0
- package/v1/examples/inferred-typing/worker.d.ts +1 -0
- package/v1/examples/inferred-typing/worker.js +24 -0
- package/v1/examples/inferred-typing/workflow.d.ts +15 -0
- package/v1/examples/inferred-typing/workflow.js +44 -0
- package/v1/examples/landing_page/durable-excution.d.ts +3 -0
- package/v1/examples/landing_page/durable-excution.js +40 -0
- package/v1/examples/landing_page/event-signaling.d.ts +6 -0
- package/v1/examples/landing_page/event-signaling.js +16 -0
- package/v1/examples/landing_page/flow-control.d.ts +6 -0
- package/v1/examples/landing_page/flow-control.js +26 -0
- package/v1/examples/landing_page/queues.d.ts +3 -0
- package/v1/examples/landing_page/queues.js +33 -0
- package/v1/examples/landing_page/scheduling.d.ts +1 -0
- package/v1/examples/landing_page/scheduling.js +12 -0
- package/v1/examples/landing_page/task-routing.d.ts +6 -0
- package/v1/examples/landing_page/task-routing.js +25 -0
- package/v1/examples/legacy/run.js +2 -2
- package/v1/examples/legacy/worker.js +2 -2
- package/v1/examples/on_cron/worker.d.ts +1 -0
- package/v1/examples/on_cron/worker.js +24 -0
- package/v1/examples/on_cron/workflow.d.ts +10 -0
- package/v1/examples/on_cron/workflow.js +21 -0
- package/v1/examples/on_event/event.js +3 -2
- package/v1/examples/on_event/worker.js +2 -2
- package/v1/examples/on_event/workflow.d.ts +4 -4
- package/v1/examples/on_event/workflow.js +6 -3
- package/v1/examples/on_event copy/event.d.ts +1 -0
- package/v1/examples/on_event copy/event.js +26 -0
- package/v1/examples/on_event copy/worker.d.ts +1 -0
- package/v1/examples/on_event copy/worker.js +24 -0
- package/v1/examples/on_event copy/workflow.d.ts +16 -0
- package/v1/examples/on_event copy/workflow.js +35 -0
- package/v1/examples/on_failure/worker.js +2 -2
- package/v1/examples/on_failure/workflow.d.ts +1 -1
- package/v1/examples/on_failure/workflow.js +18 -8
- package/v1/examples/on_success/run.d.ts +1 -0
- package/v1/examples/on_success/run.js +31 -0
- package/v1/examples/on_success/worker.d.ts +1 -0
- package/v1/examples/on_success/worker.js +24 -0
- package/v1/examples/on_success/workflow.d.ts +4 -0
- package/v1/examples/on_success/workflow.js +59 -0
- package/v1/examples/retries/run.d.ts +1 -0
- package/v1/examples/retries/run.js +29 -0
- package/v1/examples/retries/worker.d.ts +1 -0
- package/v1/examples/retries/worker.js +24 -0
- package/v1/examples/retries/workflow.d.ts +5 -0
- package/v1/examples/retries/workflow.js +53 -0
- package/v1/examples/simple/client-run.d.ts +1 -0
- package/v1/examples/simple/client-run.js +16 -0
- package/v1/examples/simple/cron.js +6 -2
- package/v1/examples/simple/delay.js +2 -2
- package/v1/examples/simple/enqueue.d.ts +1 -0
- package/v1/examples/simple/enqueue.js +43 -0
- package/v1/examples/simple/run.js +8 -3
- package/v1/examples/simple/schedule.js +11 -5
- package/v1/examples/simple/stub-workflow.d.ts +9 -0
- package/v1/examples/simple/stub-workflow.js +17 -0
- package/v1/examples/simple/worker.js +7 -2
- package/v1/examples/simple/workflow.d.ts +4 -3
- package/v1/examples/simple/workflow.js +5 -5
- package/v1/examples/sticky/run.d.ts +1 -0
- package/v1/examples/sticky/run.js +29 -0
- package/v1/examples/sticky/worker.d.ts +1 -0
- package/v1/examples/sticky/worker.js +24 -0
- package/v1/examples/sticky/workflow.d.ts +7 -0
- package/v1/examples/sticky/workflow.js +32 -0
- package/v1/examples/with_timeouts/workflow.d.ts +9 -0
- package/v1/examples/with_timeouts/workflow.js +62 -0
- package/v1/index.d.ts +5 -0
- package/v1/index.js +20 -0
- package/v1/task.d.ts +144 -14
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/v1/workflow.d.ts +0 -158
- package/v1/workflow.js +0 -145
- /package/{examples/playground.d.ts → v1/examples/dag_match_condition/event.d.ts} +0 -0
- /package/v1/examples/{client.d.ts → hatchet-client.d.ts} +0 -0
- /package/v1/examples/{client.js → hatchet-client.js} +0 -0
package/step.js
CHANGED
|
@@ -45,13 +45,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
45
45
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
46
46
|
};
|
|
47
47
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
-
exports.Context = exports.ContextWorker = exports.CreateStepSchema = exports.DesiredWorkerLabelSchema = exports.CreateRateLimitSchema = void 0;
|
|
48
|
+
exports.DurableContext = exports.Context = exports.ContextWorker = exports.CreateStepSchema = exports.DesiredWorkerLabelSchema = exports.CreateRateLimitSchema = void 0;
|
|
49
49
|
exports.mapRateLimit = mapRateLimit;
|
|
50
50
|
/* eslint-disable max-classes-per-file */
|
|
51
51
|
const hatchet_error_1 = __importDefault(require("./util/errors/hatchet-error"));
|
|
52
52
|
const z = __importStar(require("zod"));
|
|
53
53
|
const parse_1 = require("./util/parse");
|
|
54
54
|
const workflows_1 = require("./protoc/workflows");
|
|
55
|
+
const conditions_1 = require("./v1/conditions");
|
|
56
|
+
const condition_1 = require("./protoc/v1/shared/condition");
|
|
57
|
+
const transformer_1 = require("./v1/conditions/transformer");
|
|
55
58
|
exports.CreateRateLimitSchema = z.object({
|
|
56
59
|
key: z.string().optional(),
|
|
57
60
|
staticKey: z.string().optional(),
|
|
@@ -93,15 +96,33 @@ class ContextWorker {
|
|
|
93
96
|
constructor(worker) {
|
|
94
97
|
this.worker = worker;
|
|
95
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Gets the ID of the worker.
|
|
101
|
+
* @returns The ID of the worker.
|
|
102
|
+
*/
|
|
96
103
|
id() {
|
|
97
104
|
return this.worker.workerId;
|
|
98
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Checks if the worker has a registered workflow.
|
|
108
|
+
* @param workflowName - The name of the workflow to check.
|
|
109
|
+
* @returns True if the workflow is registered, otherwise false.
|
|
110
|
+
*/
|
|
99
111
|
hasWorkflow(workflowName) {
|
|
100
|
-
return !!this.worker.workflow_registry.find((workflow) => workflow.id === workflowName);
|
|
112
|
+
return !!this.worker.workflow_registry.find((workflow) => 'id' in workflow ? workflow.id === workflowName : workflow.name === workflowName);
|
|
101
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Gets the current state of the worker labels.
|
|
116
|
+
* @returns The labels of the worker.
|
|
117
|
+
*/
|
|
102
118
|
labels() {
|
|
103
119
|
return this.worker.labels;
|
|
104
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Upserts the a set of labels on the worker.
|
|
123
|
+
* @param labels - The labels to upsert.
|
|
124
|
+
* @returns A promise that resolves when the labels have been upserted.
|
|
125
|
+
*/
|
|
105
126
|
upsertLabels(labels) {
|
|
106
127
|
return this.worker.upsertLabels(labels);
|
|
107
128
|
}
|
|
@@ -132,66 +153,130 @@ class Context {
|
|
|
132
153
|
throw new hatchet_error_1.default(`Could not parse payload: ${e.message}`);
|
|
133
154
|
}
|
|
134
155
|
}
|
|
135
|
-
|
|
136
|
-
|
|
156
|
+
/**
|
|
157
|
+
* Retrieves the output of a parent task.
|
|
158
|
+
* @param task - The name of the task or a CreateTaskOpts object.
|
|
159
|
+
* @returns The output of the specified parent task.
|
|
160
|
+
* @throws An error if the task output is not found.
|
|
161
|
+
*
|
|
162
|
+
*/
|
|
163
|
+
parentOutput(task) {
|
|
137
164
|
return __awaiter(this, void 0, void 0, function* () {
|
|
165
|
+
// NOTE: parentOutput is async since we plan on potentially making this a cacheable server call
|
|
138
166
|
if (typeof task === 'string') {
|
|
139
167
|
return this.stepOutput(task);
|
|
140
168
|
}
|
|
141
169
|
return this.stepOutput(task.name);
|
|
142
170
|
});
|
|
143
171
|
}
|
|
144
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Get the output of a task.
|
|
174
|
+
* @param task - The name of the task to get the output for.
|
|
175
|
+
* @returns The output of the task.
|
|
176
|
+
* @throws An error if the task output is not found.
|
|
177
|
+
* @deprecated use ctx.parentOutput instead
|
|
178
|
+
*/
|
|
145
179
|
stepOutput(step) {
|
|
146
180
|
if (!this.data.parents) {
|
|
147
|
-
throw new hatchet_error_1.default('
|
|
181
|
+
throw new hatchet_error_1.default('output not found');
|
|
148
182
|
}
|
|
149
183
|
if (!this.data.parents[step]) {
|
|
150
|
-
throw new hatchet_error_1.default(`
|
|
184
|
+
throw new hatchet_error_1.default(`output for '${step}' not found`);
|
|
151
185
|
}
|
|
152
186
|
return this.data.parents[step];
|
|
153
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Returns errors from any task runs in the workflow.
|
|
190
|
+
* @returns A record mapping task names to error messages.
|
|
191
|
+
* @throws A warning if no errors are found (this method should be used in on-failure tasks).
|
|
192
|
+
* @deprecated use ctx.errors instead
|
|
193
|
+
*/
|
|
154
194
|
stepRunErrors() {
|
|
195
|
+
return this.errors();
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Returns errors from any task runs in the workflow.
|
|
199
|
+
* @returns A record mapping task names to error messages.
|
|
200
|
+
* @throws A warning if no errors are found (this method should be used in on-failure tasks).
|
|
201
|
+
*/
|
|
202
|
+
errors() {
|
|
155
203
|
const errors = this.data.step_run_errors || {};
|
|
156
204
|
if (Object.keys(errors).length === 0) {
|
|
157
|
-
this.logger.error('No
|
|
205
|
+
this.logger.error('No run errors found. `ctx.errors` is intended to be run in an on-failure task, and will only work on engine versions more recent than v0.53.10');
|
|
158
206
|
}
|
|
159
207
|
return errors;
|
|
160
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Gets the dag conditional triggers for the current workflow run.
|
|
211
|
+
* @returns The triggers for the current workflow.
|
|
212
|
+
*/
|
|
213
|
+
triggers() {
|
|
214
|
+
return this.data.triggers;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Determines if the workflow was triggered by an event.
|
|
218
|
+
* @returns True if the workflow was triggered by an event, otherwise false.
|
|
219
|
+
*/
|
|
161
220
|
triggeredByEvent() {
|
|
162
221
|
var _a;
|
|
163
222
|
return ((_a = this.data) === null || _a === void 0 ? void 0 : _a.triggered_by) === 'event';
|
|
164
223
|
}
|
|
224
|
+
/**
|
|
225
|
+
* Gets the input data for the current workflow.
|
|
226
|
+
* @returns The input data for the workflow.
|
|
227
|
+
*/
|
|
165
228
|
workflowInput() {
|
|
166
229
|
return this.input;
|
|
167
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Gets the name of the current workflow.
|
|
233
|
+
* @returns The name of the workflow.
|
|
234
|
+
*/
|
|
168
235
|
workflowName() {
|
|
169
236
|
return this.action.jobName;
|
|
170
237
|
}
|
|
238
|
+
/**
|
|
239
|
+
* Gets the user data associated with the workflow.
|
|
240
|
+
* @returns The user data.
|
|
241
|
+
*/
|
|
171
242
|
userData() {
|
|
172
243
|
var _a;
|
|
173
244
|
return (_a = this.data) === null || _a === void 0 ? void 0 : _a.user_data;
|
|
174
245
|
}
|
|
246
|
+
/**
|
|
247
|
+
* Gets the name of the current task.
|
|
248
|
+
* @returns The name of the task.
|
|
249
|
+
* @deprecated use ctx.taskName instead
|
|
250
|
+
*/
|
|
175
251
|
stepName() {
|
|
252
|
+
return this.taskName();
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Gets the name of the current running task.
|
|
256
|
+
* @returns The name of the task.
|
|
257
|
+
*/
|
|
258
|
+
taskName() {
|
|
176
259
|
return this.action.stepName;
|
|
177
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Gets the ID of the current workflow run.
|
|
263
|
+
* @returns The workflow run ID.
|
|
264
|
+
*/
|
|
178
265
|
workflowRunId() {
|
|
179
266
|
return this.action.workflowRunId;
|
|
180
267
|
}
|
|
268
|
+
/**
|
|
269
|
+
* Gets the number of times the current task has been retried.
|
|
270
|
+
* @returns The retry count.
|
|
271
|
+
*/
|
|
181
272
|
retryCount() {
|
|
182
273
|
return this.action.retryCount;
|
|
183
274
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
stepRunId: this.action.stepRunId,
|
|
190
|
-
path: name,
|
|
191
|
-
value: JSON.stringify(defaultValue),
|
|
192
|
-
});
|
|
193
|
-
return defaultValue;
|
|
194
|
-
}
|
|
275
|
+
/**
|
|
276
|
+
* Logs a message from the current task.
|
|
277
|
+
* @param message - The message to log.
|
|
278
|
+
* @param level - The log level (optional).
|
|
279
|
+
*/
|
|
195
280
|
log(message, level) {
|
|
196
281
|
const { stepRunId } = this.action;
|
|
197
282
|
if (!stepRunId) {
|
|
@@ -202,10 +287,9 @@ class Context {
|
|
|
202
287
|
this.client.event.putLog(stepRunId, message, level);
|
|
203
288
|
}
|
|
204
289
|
/**
|
|
205
|
-
* Refreshes the timeout for the current
|
|
290
|
+
* Refreshes the timeout for the current task.
|
|
206
291
|
* @param incrementBy - The interval by which to increment the timeout.
|
|
207
|
-
*
|
|
208
|
-
* '1m' for 1 minute, or '1d' for 1 day.
|
|
292
|
+
* The interval should be specified in the format of '10s' for 10 seconds, '1m' for 1 minute, or '1d' for 1 day.
|
|
209
293
|
*/
|
|
210
294
|
refreshTimeout(incrementBy) {
|
|
211
295
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -218,6 +302,11 @@ class Context {
|
|
|
218
302
|
yield this.client.dispatcher.refreshTimeout(incrementBy, stepRunId);
|
|
219
303
|
});
|
|
220
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* Releases a worker slot for a task run such that the worker can pick up another task.
|
|
307
|
+
* Note: this is an advanced feature that may lead to unexpected behavior if used incorrectly.
|
|
308
|
+
* @returns A promise that resolves when the slot has been released.
|
|
309
|
+
*/
|
|
221
310
|
releaseSlot() {
|
|
222
311
|
return __awaiter(this, void 0, void 0, function* () {
|
|
223
312
|
yield this.client.dispatcher.client.releaseSlot({
|
|
@@ -225,6 +314,11 @@ class Context {
|
|
|
225
314
|
});
|
|
226
315
|
});
|
|
227
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* Streams data from the current task run.
|
|
319
|
+
* @param data - The data to stream (string or binary).
|
|
320
|
+
* @returns A promise that resolves when the data has been streamed.
|
|
321
|
+
*/
|
|
228
322
|
putStream(data) {
|
|
229
323
|
return __awaiter(this, void 0, void 0, function* () {
|
|
230
324
|
const { stepRunId } = this.action;
|
|
@@ -237,31 +331,31 @@ class Context {
|
|
|
237
331
|
});
|
|
238
332
|
}
|
|
239
333
|
/**
|
|
240
|
-
*
|
|
241
|
-
* @param children
|
|
242
|
-
* @returns
|
|
334
|
+
* Runs multiple children workflows in parallel without waiting for their results.
|
|
335
|
+
* @param children - An array of objects containing the workflow name, input data, and options for each workflow.
|
|
336
|
+
* @returns A list of workflow run references to the enqueued runs.
|
|
243
337
|
*/
|
|
244
|
-
|
|
338
|
+
bulkRunNoWaitChildren(children) {
|
|
245
339
|
return this.spawnWorkflows(children);
|
|
246
340
|
}
|
|
247
341
|
/**
|
|
248
|
-
* Runs multiple children workflows in parallel.
|
|
249
|
-
* @param children
|
|
250
|
-
* @returns
|
|
342
|
+
* Runs multiple children workflows in parallel and waits for all results.
|
|
343
|
+
* @param children - An array of objects containing the workflow name, input data, and options for each workflow.
|
|
344
|
+
* @returns A list of results from the children workflows.
|
|
251
345
|
*/
|
|
252
346
|
bulkRunChildren(children) {
|
|
253
347
|
return __awaiter(this, void 0, void 0, function* () {
|
|
254
|
-
const runs = yield this.
|
|
255
|
-
const res = runs.map((run) => run.
|
|
348
|
+
const runs = yield this.bulkRunNoWaitChildren(children);
|
|
349
|
+
const res = runs.map((run) => run.output);
|
|
256
350
|
return Promise.all(res);
|
|
257
351
|
});
|
|
258
352
|
}
|
|
259
353
|
/**
|
|
260
354
|
* Spawns multiple workflows.
|
|
261
355
|
*
|
|
262
|
-
* @param workflows
|
|
263
|
-
* @returns
|
|
264
|
-
* @deprecated
|
|
356
|
+
* @param workflows - An array of objects containing the workflow name, input data, and options for each workflow.
|
|
357
|
+
* @returns A list of references to the spawned workflow runs.
|
|
358
|
+
* @deprecated Use bulkRunNoWaitChildren or bulkRunChildren instead.
|
|
265
359
|
*/
|
|
266
360
|
spawnWorkflows(workflows) {
|
|
267
361
|
const { workflowRunId, stepRunId } = this.action;
|
|
@@ -309,50 +403,38 @@ class Context {
|
|
|
309
403
|
}
|
|
310
404
|
}
|
|
311
405
|
/**
|
|
312
|
-
* Runs a new workflow.
|
|
406
|
+
* Runs a new workflow and waits for its result.
|
|
313
407
|
*
|
|
314
|
-
* @param workflow
|
|
315
|
-
* @param input
|
|
316
|
-
* @param options
|
|
317
|
-
* @
|
|
318
|
-
* @param <P> the type of the output data
|
|
319
|
-
* @return the result of the workflow
|
|
408
|
+
* @param workflow - The workflow to run (name, Workflow instance, or WorkflowV1 instance).
|
|
409
|
+
* @param input - The input data for the workflow.
|
|
410
|
+
* @param options - Additional options for spawning the workflow. If a string is provided, it is used as the key.
|
|
411
|
+
* @returns The result of the workflow.
|
|
320
412
|
*/
|
|
321
413
|
runChild(workflow, input, options) {
|
|
322
414
|
return __awaiter(this, void 0, void 0, function* () {
|
|
323
415
|
const run = yield this.spawnWorkflow(workflow, input, options);
|
|
324
|
-
return run.
|
|
416
|
+
return run.output;
|
|
325
417
|
});
|
|
326
418
|
}
|
|
327
419
|
/**
|
|
328
|
-
* Enqueues a new workflow.
|
|
420
|
+
* Enqueues a new workflow without waiting for its result.
|
|
329
421
|
*
|
|
330
|
-
* @param
|
|
331
|
-
* @param input
|
|
332
|
-
* @param options
|
|
333
|
-
*
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
* @param <Q> the type of the input data
|
|
337
|
-
* @param <P> the type of the output data
|
|
338
|
-
* @return a reference to the spawned workflow run
|
|
339
|
-
*/
|
|
340
|
-
enqueueChild(workflow, input, options) {
|
|
422
|
+
* @param workflow - The workflow to enqueue (name, Workflow instance, or WorkflowV1 instance).
|
|
423
|
+
* @param input - The input data for the workflow.
|
|
424
|
+
* @param options - Additional options for spawning the workflow.
|
|
425
|
+
* @returns A reference to the spawned workflow run.
|
|
426
|
+
*/
|
|
427
|
+
runNoWaitChild(workflow, input, options) {
|
|
341
428
|
return this.spawnWorkflow(workflow, input, options);
|
|
342
429
|
}
|
|
343
430
|
/**
|
|
344
431
|
* Spawns a new workflow.
|
|
345
432
|
*
|
|
346
|
-
* @param
|
|
347
|
-
* @param input
|
|
348
|
-
* @param options
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
* - sticky: a boolean indicating whether to use sticky execution
|
|
352
|
-
* @param <Q> the type of the input data
|
|
353
|
-
* @param <P> the type of the output data
|
|
354
|
-
* @return a reference to the spawned workflow run
|
|
355
|
-
* @deprecated use runChild or enqueueChild instead
|
|
433
|
+
* @param workflow - The workflow to spawn (name, Workflow instance, or WorkflowV1 instance).
|
|
434
|
+
* @param input - The input data for the workflow.
|
|
435
|
+
* @param options - Additional options for spawning the workflow.
|
|
436
|
+
* @returns A reference to the spawned workflow run.
|
|
437
|
+
* @deprecated Use runChild or runNoWaitChild instead.
|
|
356
438
|
*/
|
|
357
439
|
spawnWorkflow(workflow, input, options) {
|
|
358
440
|
const { workflowRunId, stepRunId } = this.action;
|
|
@@ -395,6 +477,10 @@ class Context {
|
|
|
395
477
|
throw new hatchet_error_1.default(e.message);
|
|
396
478
|
}
|
|
397
479
|
}
|
|
480
|
+
/**
|
|
481
|
+
* Retrieves additional metadata associated with the current workflow run.
|
|
482
|
+
* @returns A record of metadata key-value pairs.
|
|
483
|
+
*/
|
|
398
484
|
additionalMetadata() {
|
|
399
485
|
if (!this.action.additionalMetadata) {
|
|
400
486
|
return {};
|
|
@@ -403,17 +489,75 @@ class Context {
|
|
|
403
489
|
const res = (0, parse_1.parseJSON)(this.action.additionalMetadata);
|
|
404
490
|
return res;
|
|
405
491
|
}
|
|
492
|
+
/**
|
|
493
|
+
* Gets the index of this workflow if it was spawned as part of a bulk operation.
|
|
494
|
+
* @returns The child index number, or undefined if not set.
|
|
495
|
+
*/
|
|
406
496
|
childIndex() {
|
|
407
497
|
return this.action.childWorkflowIndex;
|
|
408
498
|
}
|
|
499
|
+
/**
|
|
500
|
+
* Gets the key associated with this workflow if it was spawned as a child workflow.
|
|
501
|
+
* @returns The child key, or undefined if not set.
|
|
502
|
+
*/
|
|
409
503
|
childKey() {
|
|
410
504
|
return this.action.childWorkflowKey;
|
|
411
505
|
}
|
|
506
|
+
/**
|
|
507
|
+
* Gets the ID of the parent workflow run if this workflow was spawned as a child.
|
|
508
|
+
* @returns The parent workflow run ID, or undefined if not a child workflow.
|
|
509
|
+
*/
|
|
412
510
|
parentWorkflowRunId() {
|
|
413
511
|
return this.action.parentWorkflowRunId;
|
|
414
512
|
}
|
|
415
513
|
}
|
|
416
514
|
exports.Context = Context;
|
|
515
|
+
class DurableContext extends Context {
|
|
516
|
+
constructor() {
|
|
517
|
+
super(...arguments);
|
|
518
|
+
this.waitKey = 0;
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Pauses execution for the specified duration.
|
|
522
|
+
* Duration is "global" meaning it will wait in real time regardless of transient failures like worker restarts.
|
|
523
|
+
* @param duration - The duration to sleep for.
|
|
524
|
+
* @returns A promise that resolves when the sleep duration has elapsed.
|
|
525
|
+
*/
|
|
526
|
+
sleepFor(duration, readableDataKey) {
|
|
527
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
528
|
+
return this.waitFor({ sleepFor: duration, readableDataKey });
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Pauses execution until the specified conditions are met.
|
|
533
|
+
* Conditions are "global" meaning they will wait in real time regardless of transient failures like worker restarts.
|
|
534
|
+
* @param conditions - The conditions to wait for.
|
|
535
|
+
* @returns A promise that resolves with the event that satisfied the conditions.
|
|
536
|
+
*/
|
|
537
|
+
waitFor(conditions) {
|
|
538
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
539
|
+
const pbConditions = (0, transformer_1.conditionsToPb)((0, conditions_1.Render)(condition_1.Action.CREATE, conditions));
|
|
540
|
+
// eslint-disable-next-line no-plusplus
|
|
541
|
+
const key = `waitFor-${this.waitKey++}`;
|
|
542
|
+
yield this.client.durableListener.registerDurableEvent({
|
|
543
|
+
taskId: this.action.stepRunId,
|
|
544
|
+
signalKey: key,
|
|
545
|
+
sleepConditions: pbConditions.sleepConditions,
|
|
546
|
+
userEventConditions: pbConditions.userEventConditions,
|
|
547
|
+
});
|
|
548
|
+
const listener = this.client.durableListener.subscribe({
|
|
549
|
+
taskId: this.action.stepRunId,
|
|
550
|
+
signalKey: key,
|
|
551
|
+
});
|
|
552
|
+
const event = yield listener.get();
|
|
553
|
+
// Convert event.data from Uint8Array to string if needed
|
|
554
|
+
const eventData = event.data instanceof Uint8Array ? new TextDecoder().decode(event.data) : event.data;
|
|
555
|
+
const res = JSON.parse(eventData);
|
|
556
|
+
return res.CREATE;
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
exports.DurableContext = DurableContext;
|
|
417
561
|
function mapRateLimit(limits) {
|
|
418
562
|
if (!limits)
|
|
419
563
|
return [];
|
package/util/sleep.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sleeps for a given number of milliseconds without blocking the event loop
|
|
3
|
+
* WARNING: This is not a durable sleep. It will not be honored if the worker is
|
|
4
|
+
* restarted or crashes.
|
|
5
|
+
* @param ms - The number of milliseconds to sleep
|
|
6
|
+
* @returns A promise that resolves after the given number of milliseconds
|
|
7
|
+
*/
|
|
1
8
|
declare const sleep: (ms: number) => Promise<unknown>;
|
|
2
9
|
export default sleep;
|
package/util/sleep.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Sleeps for a given number of milliseconds without blocking the event loop
|
|
5
|
+
* WARNING: This is not a durable sleep. It will not be honored if the worker is
|
|
6
|
+
* restarted or crashes.
|
|
7
|
+
* @param ms - The number of milliseconds to sleep
|
|
8
|
+
* @returns A promise that resolves after the given number of milliseconds
|
|
9
|
+
*/
|
|
3
10
|
const sleep = (ms) => new Promise((resolve) => {
|
|
4
11
|
setTimeout(resolve, ms);
|
|
5
12
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RunListenerClient, StepRunEvent } from '../clients/listeners/run-listener/child-listener-client';
|
|
2
2
|
type EventualWorkflowRunId = string | Promise<string> | Promise<{
|
|
3
3
|
workflowRunId: string;
|
|
4
4
|
}>;
|
|
@@ -11,9 +11,15 @@ export default class WorkflowRunRef<T> {
|
|
|
11
11
|
private client;
|
|
12
12
|
constructor(workflowRunId: string | Promise<string> | Promise<{
|
|
13
13
|
workflowRunId: string;
|
|
14
|
-
}>, client:
|
|
14
|
+
}>, client: RunListenerClient, parentWorkflowRunId?: string);
|
|
15
|
+
get runId(): Promise<string>;
|
|
15
16
|
getWorkflowRunId(): Promise<string>;
|
|
16
17
|
stream(): Promise<AsyncGenerator<StepRunEvent, void, unknown>>;
|
|
18
|
+
get output(): Promise<T>;
|
|
19
|
+
/**
|
|
20
|
+
* @alias output
|
|
21
|
+
* @deprecated use output
|
|
22
|
+
*/
|
|
17
23
|
result(): Promise<T>;
|
|
18
24
|
toJSON(): Promise<string>;
|
|
19
25
|
}
|
package/util/workflow-run-ref.js
CHANGED
|
@@ -55,6 +55,11 @@ class WorkflowRunRef {
|
|
|
55
55
|
this.parentWorkflowRunId = parentWorkflowRunId;
|
|
56
56
|
this.client = client;
|
|
57
57
|
}
|
|
58
|
+
// TODO docstrings
|
|
59
|
+
get runId() {
|
|
60
|
+
return this.getWorkflowRunId();
|
|
61
|
+
}
|
|
62
|
+
// @deprecated use runId
|
|
58
63
|
getWorkflowRunId() {
|
|
59
64
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
65
|
return getWorkflowRunId(this.workflowRunId);
|
|
@@ -66,6 +71,14 @@ class WorkflowRunRef {
|
|
|
66
71
|
return this.client.stream(workflowRunId);
|
|
67
72
|
});
|
|
68
73
|
}
|
|
74
|
+
// TODO not sure if i want this to be a get since it might be blocking for a long time..
|
|
75
|
+
get output() {
|
|
76
|
+
return this.result();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @alias output
|
|
80
|
+
* @deprecated use output
|
|
81
|
+
*/
|
|
69
82
|
result() {
|
|
70
83
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
84
|
const workflowRunId = yield getWorkflowRunId(this.workflowRunId);
|