@hatchet-dev/typescript-sdk 0.16.0-alpha.4 → 0.17.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/dispatcher/dispatcher-client.d.ts +2 -1
- package/clients/dispatcher/dispatcher-client.js +10 -1
- package/clients/event/event-client.js +13 -8
- package/clients/hatchet-client/client-config.d.ts +0 -6
- package/clients/hatchet-client/client-config.js +0 -2
- package/clients/hatchet-client/features/cron-client.d.ts +79 -0
- package/clients/hatchet-client/features/cron-client.js +127 -0
- package/clients/hatchet-client/features/schedule-client.d.ts +76 -0
- package/clients/hatchet-client/features/schedule-client.js +120 -0
- package/clients/hatchet-client/hatchet-client.d.ts +4 -2
- package/clients/hatchet-client/hatchet-client.js +6 -25
- package/clients/listener/child-listener-client.js +16 -13
- package/clients/rest/api.d.ts +0 -3
- package/clients/rest/api.js +0 -10
- package/clients/rest/generated/Api.d.ts +152 -2
- package/clients/rest/generated/Api.js +81 -1
- package/clients/rest/generated/data-contracts.d.ts +86 -0
- package/clients/rest/generated/data-contracts.js +27 -1
- package/clients/rest/index.d.ts +2 -2
- package/clients/rest/index.js +21 -10
- package/clients/worker/handler.js +16 -11
- package/clients/worker/worker.d.ts +2 -10
- package/clients/worker/worker.js +24 -38
- package/examples/crons/cron-worker.d.ts +2 -0
- package/examples/crons/cron-worker.js +48 -0
- package/examples/crons/programatic-crons.js +55 -0
- package/examples/on-failure.js +15 -10
- package/examples/retries-with-backoff.d.ts +1 -0
- package/examples/{managed-worker.js → retries-with-backoff.js} +24 -33
- package/examples/scheduled-runs/programatic-schedules.d.ts +1 -0
- package/examples/scheduled-runs/programatic-schedules.js +54 -0
- package/examples/simple-worker.d.ts +2 -1
- package/examples/simple-worker.js +6 -3
- package/package.json +8 -8
- package/protoc/dispatcher/dispatcher.js +1 -1
- package/protoc/events/events.js +1 -1
- package/protoc/google/protobuf/timestamp.js +1 -1
- package/protoc/google/protobuf/wrappers.js +17 -7
- package/protoc/workflows/workflows.d.ts +13 -0
- package/protoc/workflows/workflows.js +160 -7
- package/step.d.ts +17 -121
- package/step.js +23 -9
- package/util/config-loader/config-loader.js +18 -26
- package/util/retrier.js +8 -3
- package/version.d.ts +1 -0
- package/version.js +4 -0
- package/workflow.d.ts +67 -459
- package/workflow.js +17 -7
- package/clients/rest/generated/cloud/Api.d.ts +0 -377
- package/clients/rest/generated/cloud/Api.js +0 -326
- package/clients/rest/generated/cloud/data-contracts.d.ts +0 -468
- package/clients/rest/generated/cloud/data-contracts.js +0 -68
- package/clients/rest/generated/cloud/http-client.d.ts +0 -41
- package/clients/rest/generated/cloud/http-client.js +0 -102
- package/clients/worker/compute/compute-config.d.ts +0 -156
- package/clients/worker/compute/compute-config.js +0 -82
- package/clients/worker/compute/managed-compute.d.ts +0 -15
- package/clients/worker/compute/managed-compute.js +0 -104
- /package/examples/{managed-worker.d.ts → crons/programatic-crons.d.ts} +0 -0
package/clients/worker/worker.js
CHANGED
|
@@ -27,7 +27,6 @@ const workflows_1 = require("../../protoc/workflows");
|
|
|
27
27
|
const logger_1 = require("../../util/logger");
|
|
28
28
|
const handler_1 = require("./handler");
|
|
29
29
|
const step_1 = require("../../step");
|
|
30
|
-
const managed_compute_1 = require("./compute/managed-compute");
|
|
31
30
|
class Worker {
|
|
32
31
|
constructor(client, options) {
|
|
33
32
|
this.workflow_registry = [];
|
|
@@ -49,33 +48,18 @@ class Worker {
|
|
|
49
48
|
registerActions(workflow) {
|
|
50
49
|
var _a;
|
|
51
50
|
const newActions = workflow.steps.reduce((acc, step) => {
|
|
52
|
-
|
|
53
|
-
if (!this.client.config.runnable_actions ||
|
|
54
|
-
this.client.config.runnable_actions.includes(`${workflow.id}:${step.name}`)) {
|
|
55
|
-
acc[`${workflow.id}:${step.name}`] = {
|
|
56
|
-
func: step.run,
|
|
57
|
-
compute: step.compute,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
51
|
+
acc[`${workflow.id}:${step.name}`] = step.run;
|
|
60
52
|
return acc;
|
|
61
53
|
}, {});
|
|
62
54
|
const onFailureAction = workflow.onFailure
|
|
63
55
|
? {
|
|
64
|
-
[`${workflow.id}-on-failure:${workflow.onFailure.name}`]:
|
|
65
|
-
func: workflow.onFailure.run,
|
|
66
|
-
compute: workflow.onFailure.compute,
|
|
67
|
-
},
|
|
68
|
-
}
|
|
69
|
-
: {};
|
|
70
|
-
const concurrencyAction = ((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.name) && workflow.concurrency.key
|
|
71
|
-
? {
|
|
72
|
-
[`${workflow.id}:${workflow.concurrency.name}`]: {
|
|
73
|
-
func: workflow.concurrency.key,
|
|
74
|
-
compute: undefined,
|
|
75
|
-
},
|
|
56
|
+
[`${workflow.id}-on-failure:${workflow.onFailure.name}`]: workflow.onFailure.run,
|
|
76
57
|
}
|
|
77
58
|
: {};
|
|
78
|
-
this.action_registry = Object.assign(Object.assign(Object.assign(
|
|
59
|
+
this.action_registry = Object.assign(Object.assign(Object.assign({}, this.action_registry), newActions), onFailureAction);
|
|
60
|
+
this.action_registry =
|
|
61
|
+
((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.name) && workflow.concurrency.key
|
|
62
|
+
? Object.assign(Object.assign({}, this.action_registry), { [`${workflow.id}:${workflow.concurrency.name}`]: workflow.concurrency.key }) : Object.assign({}, this.action_registry);
|
|
79
63
|
}
|
|
80
64
|
getHandler(workflows) {
|
|
81
65
|
for (const workflow of workflows) {
|
|
@@ -152,7 +136,7 @@ class Worker {
|
|
|
152
136
|
name: workflow.id,
|
|
153
137
|
description: workflow.description,
|
|
154
138
|
steps: workflow.steps.map((step) => {
|
|
155
|
-
var _a;
|
|
139
|
+
var _a, _b, _c;
|
|
156
140
|
return ({
|
|
157
141
|
readableId: step.name,
|
|
158
142
|
action: `${workflow.id}:${step.name}`,
|
|
@@ -163,6 +147,8 @@ class Worker {
|
|
|
163
147
|
retries: step.retries || 0,
|
|
164
148
|
rateLimits: (0, step_1.mapRateLimit)(step.rate_limits),
|
|
165
149
|
workerLabels: toPbWorkerLabel(step.worker_labels),
|
|
150
|
+
backoffFactor: (_b = step.backoff) === null || _b === void 0 ? void 0 : _b.factor,
|
|
151
|
+
backoffMaxSeconds: (_c = step.backoff) === null || _c === void 0 ? void 0 : _c.maxSeconds,
|
|
166
152
|
});
|
|
167
153
|
}),
|
|
168
154
|
},
|
|
@@ -178,11 +164,8 @@ class Worker {
|
|
|
178
164
|
this.registerActions(workflow);
|
|
179
165
|
});
|
|
180
166
|
}
|
|
181
|
-
registerAction(actionId, action
|
|
182
|
-
this.action_registry[actionId] =
|
|
183
|
-
func: action,
|
|
184
|
-
compute,
|
|
185
|
-
};
|
|
167
|
+
registerAction(actionId, action) {
|
|
168
|
+
this.action_registry[actionId] = action;
|
|
186
169
|
}
|
|
187
170
|
handleStartStepRun(action) {
|
|
188
171
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -196,7 +179,7 @@ class Worker {
|
|
|
196
179
|
return;
|
|
197
180
|
}
|
|
198
181
|
const run = () => __awaiter(this, void 0, void 0, function* () {
|
|
199
|
-
return step
|
|
182
|
+
return step(context);
|
|
200
183
|
});
|
|
201
184
|
const success = (result) => __awaiter(this, void 0, void 0, function* () {
|
|
202
185
|
this.logger.info(`Step run ${action.stepRunId} succeeded`);
|
|
@@ -208,16 +191,16 @@ class Worker {
|
|
|
208
191
|
delete this.futures[action.stepRunId];
|
|
209
192
|
}
|
|
210
193
|
catch (actionEventError) {
|
|
211
|
-
this.logger.error(`Could not send completed action event: ${actionEventError.message}`);
|
|
194
|
+
this.logger.error(`Could not send completed action event: ${actionEventError.message || actionEventError}`);
|
|
212
195
|
// send a failure event
|
|
213
196
|
const failureEvent = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, actionEventError.message);
|
|
214
197
|
try {
|
|
215
198
|
yield this.client.dispatcher.sendStepActionEvent(failureEvent);
|
|
216
199
|
}
|
|
217
200
|
catch (failureEventError) {
|
|
218
|
-
this.logger.error(`Could not send failed action event: ${failureEventError.message}`);
|
|
201
|
+
this.logger.error(`Could not send failed action event: ${failureEventError.message || failureEventError}`);
|
|
219
202
|
}
|
|
220
|
-
this.logger.error(`Could not send action event: ${actionEventError.message}`);
|
|
203
|
+
this.logger.error(`Could not send action event: ${actionEventError.message || actionEventError}`);
|
|
221
204
|
}
|
|
222
205
|
});
|
|
223
206
|
const failure = (error) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -256,10 +239,15 @@ class Worker {
|
|
|
256
239
|
this.client.dispatcher.sendStepActionEvent(event).catch((e) => {
|
|
257
240
|
this.logger.error(`Could not send action event: ${e.message}`);
|
|
258
241
|
});
|
|
259
|
-
|
|
242
|
+
try {
|
|
243
|
+
yield future.promise;
|
|
244
|
+
}
|
|
245
|
+
catch (e) {
|
|
246
|
+
this.logger.error(`Could not wait for step run to finish: ${e}`);
|
|
247
|
+
}
|
|
260
248
|
}
|
|
261
249
|
catch (e) {
|
|
262
|
-
this.logger.error(`Could not send action event: ${e
|
|
250
|
+
this.logger.error(`Could not send action event (outer): ${e}`);
|
|
263
251
|
}
|
|
264
252
|
});
|
|
265
253
|
}
|
|
@@ -281,7 +269,7 @@ class Worker {
|
|
|
281
269
|
return;
|
|
282
270
|
}
|
|
283
271
|
const run = () => __awaiter(this, void 0, void 0, function* () {
|
|
284
|
-
return step
|
|
272
|
+
return step(context);
|
|
285
273
|
});
|
|
286
274
|
const success = (result) => {
|
|
287
275
|
this.logger.info(`Step run ${action.stepRunId} succeeded`);
|
|
@@ -375,7 +363,7 @@ class Worker {
|
|
|
375
363
|
}
|
|
376
364
|
}
|
|
377
365
|
catch (e) {
|
|
378
|
-
this.logger.error(`Could not cancel step run: ${e
|
|
366
|
+
this.logger.error(`Could not cancel step run: ${e}`);
|
|
379
367
|
}
|
|
380
368
|
});
|
|
381
369
|
}
|
|
@@ -410,8 +398,6 @@ class Worker {
|
|
|
410
398
|
var _a, e_1, _b, _c;
|
|
411
399
|
// ensure all workflows are registered
|
|
412
400
|
yield Promise.all(this.registeredWorkflowPromises);
|
|
413
|
-
const managedCompute = new managed_compute_1.ManagedCompute(this.action_registry, this.client, this.maxRuns);
|
|
414
|
-
yield managedCompute.cloudRegister();
|
|
415
401
|
try {
|
|
416
402
|
this.listener = yield this.client.dispatcher.getActionListener({
|
|
417
403
|
workerName: this.name,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.simpleCronWorkflow = void 0;
|
|
16
|
+
const sdk_1 = __importDefault(require("../../sdk"));
|
|
17
|
+
const hatchet = sdk_1.default.init();
|
|
18
|
+
// ❓ Workflow Definition Cron Trigger
|
|
19
|
+
// Adding a cron trigger to a workflow is as simple as adding a `cron expression` to the `on` prop of the workflow definition
|
|
20
|
+
exports.simpleCronWorkflow = {
|
|
21
|
+
id: 'simple-cron-workflow',
|
|
22
|
+
on: {
|
|
23
|
+
// 👀 define the cron expression to run every minute
|
|
24
|
+
cron: '* * * * *',
|
|
25
|
+
},
|
|
26
|
+
// ... normal workflow definition
|
|
27
|
+
description: 'return the current time every minute',
|
|
28
|
+
steps: [
|
|
29
|
+
{
|
|
30
|
+
name: 'what-time-is-it',
|
|
31
|
+
run: (ctx) => {
|
|
32
|
+
return { time: new Date().toISOString() };
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
// ,
|
|
37
|
+
};
|
|
38
|
+
// ‼️
|
|
39
|
+
function main() {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
const worker = yield hatchet.worker('example-worker');
|
|
42
|
+
yield worker.registerWorkflow(exports.simpleCronWorkflow);
|
|
43
|
+
worker.start();
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (require.main === module) {
|
|
47
|
+
main();
|
|
48
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const sdk_1 = __importDefault(require("../../sdk"));
|
|
16
|
+
const cron_worker_1 = require("./cron-worker");
|
|
17
|
+
const hatchet = sdk_1.default.init();
|
|
18
|
+
// This example assumes you have a worker already running
|
|
19
|
+
// and registered the cron workflow to it
|
|
20
|
+
function main() {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
// ? Create
|
|
23
|
+
// You can create dynamic cron triggers programmatically via the API
|
|
24
|
+
const createdCron = yield hatchet.cron.create(cron_worker_1.simpleCronWorkflow, // workflow object or string workflow id
|
|
25
|
+
{
|
|
26
|
+
name: 'customer-a-daily-report', // friendly name for the cron trigger
|
|
27
|
+
expression: '0 12 * * *', // every day at noon
|
|
28
|
+
input: {
|
|
29
|
+
name: 'John Doe',
|
|
30
|
+
},
|
|
31
|
+
additionalMetadata: {
|
|
32
|
+
customerId: '123',
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
const { id } = createdCron.metadata; // id which you can later use to reference the cron trigger
|
|
36
|
+
// !!
|
|
37
|
+
// ? Get
|
|
38
|
+
// You can get a specific cron trigger by passing in the cron trigger id
|
|
39
|
+
const cron = yield hatchet.cron.get(id);
|
|
40
|
+
// !!
|
|
41
|
+
// ? Delete
|
|
42
|
+
// You can delete a cron trigger by passing the cron object
|
|
43
|
+
// or a cron Id to the delete method
|
|
44
|
+
yield hatchet.cron.delete(cron);
|
|
45
|
+
// !!
|
|
46
|
+
// ? List
|
|
47
|
+
// You can list all cron triggers by passing in a query object
|
|
48
|
+
const cronList = yield hatchet.cron.list({
|
|
49
|
+
offset: 0,
|
|
50
|
+
limit: 10,
|
|
51
|
+
});
|
|
52
|
+
// !!
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
main();
|
package/examples/on-failure.js
CHANGED
|
@@ -13,36 +13,41 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const sdk_1 = __importDefault(require("../sdk"));
|
|
16
|
-
const hatchet = sdk_1.default.init(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
setTimeout(resolve, ms);
|
|
21
|
-
});
|
|
16
|
+
const hatchet = sdk_1.default.init();
|
|
17
|
+
// ❓ OnFailure Step
|
|
18
|
+
// This workflow will fail because the step will throw an error
|
|
19
|
+
// we define an onFailure step to handle this case
|
|
22
20
|
const workflow = {
|
|
21
|
+
// ... normal workflow definition
|
|
23
22
|
id: 'on-failure-example',
|
|
24
23
|
description: 'test',
|
|
25
24
|
on: {
|
|
26
25
|
event: 'user:create',
|
|
27
26
|
},
|
|
27
|
+
// ,
|
|
28
28
|
steps: [
|
|
29
29
|
{
|
|
30
|
-
name: '
|
|
30
|
+
name: 'step1',
|
|
31
31
|
run: (ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
32
|
-
|
|
33
|
-
yield sleep(1000);
|
|
32
|
+
// 👀 this step will always throw an error
|
|
34
33
|
throw new Error('Step 1 failed');
|
|
35
34
|
}),
|
|
36
35
|
},
|
|
37
36
|
],
|
|
37
|
+
// 👀 After the workflow fails, this special step will run
|
|
38
38
|
onFailure: {
|
|
39
39
|
name: 'on-failure-step',
|
|
40
40
|
run: (ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
41
|
-
|
|
41
|
+
// 👀 we can do things like perform cleanup logic
|
|
42
|
+
// or notify a user here
|
|
42
43
|
return { onFailure: 'step' };
|
|
43
44
|
}),
|
|
44
45
|
},
|
|
45
46
|
};
|
|
47
|
+
// ‼️
|
|
48
|
+
// ❓ OnFailure With Details
|
|
49
|
+
// Coming soon to TypeScript! https://github.com/hatchet-dev/hatchet-typescript/issues/447
|
|
50
|
+
// ‼️
|
|
46
51
|
function main() {
|
|
47
52
|
return __awaiter(this, void 0, void 0, function* () {
|
|
48
53
|
const worker = yield hatchet.worker('example-worker', 1);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -12,52 +12,43 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const data_contracts_1 = require("../clients/rest/generated/cloud/data-contracts");
|
|
16
15
|
const sdk_1 = __importDefault(require("../sdk"));
|
|
17
16
|
const hatchet = sdk_1.default.init();
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
memoryMb: 1024,
|
|
21
|
-
numReplicas: 1,
|
|
22
|
-
cpus: 1,
|
|
23
|
-
regions: [data_contracts_1.ManagedWorkerRegion.Ewr],
|
|
24
|
-
};
|
|
25
|
-
const twoCpuWorkerConfig = {
|
|
26
|
-
cpuKind: 'shared',
|
|
27
|
-
memoryMb: 1024,
|
|
28
|
-
numReplicas: 1,
|
|
29
|
-
cpus: 2,
|
|
30
|
-
regions: [data_contracts_1.ManagedWorkerRegion.Ewr],
|
|
31
|
-
};
|
|
17
|
+
let numRetries = 0;
|
|
18
|
+
// ❓ Backoff
|
|
32
19
|
const workflow = {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
},
|
|
20
|
+
// ... normal workflow definition
|
|
21
|
+
id: 'retries-with-backoff',
|
|
22
|
+
description: 'Backoff',
|
|
23
|
+
// ,
|
|
38
24
|
steps: [
|
|
39
25
|
{
|
|
40
|
-
name: '
|
|
41
|
-
|
|
26
|
+
name: 'backoff-step',
|
|
27
|
+
// ... step definition
|
|
42
28
|
run: (ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
43
|
-
|
|
44
|
-
|
|
29
|
+
if (numRetries < 5) {
|
|
30
|
+
numRetries += 1;
|
|
31
|
+
throw new Error('failed');
|
|
32
|
+
}
|
|
33
|
+
return { backoff: 'completed' };
|
|
45
34
|
}),
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
35
|
+
// ,
|
|
36
|
+
retries: 10,
|
|
37
|
+
// 👀 Backoff configuration
|
|
38
|
+
backoff: {
|
|
39
|
+
// 👀 Maximum number of seconds to wait between retries
|
|
40
|
+
maxSeconds: 60,
|
|
41
|
+
// 👀 Factor to increase the wait time between retries.
|
|
42
|
+
// This sequence will be 2s, 4s, 8s, 16s, 32s, 60s... due to the maxSeconds limit
|
|
43
|
+
factor: 2,
|
|
54
44
|
},
|
|
55
45
|
},
|
|
56
46
|
],
|
|
57
47
|
};
|
|
48
|
+
// ‼️
|
|
58
49
|
function main() {
|
|
59
50
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
const worker = yield hatchet.worker('
|
|
51
|
+
const worker = yield hatchet.worker('backoff-worker');
|
|
61
52
|
yield worker.registerWorkflow(workflow);
|
|
62
53
|
worker.start();
|
|
63
54
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const sdk_1 = __importDefault(require("../../sdk"));
|
|
16
|
+
const simple_worker_1 = require("../simple-worker");
|
|
17
|
+
const hatchet = sdk_1.default.init();
|
|
18
|
+
// This example assumes you have a worker already running
|
|
19
|
+
// and registered simpleWorkflow to it
|
|
20
|
+
function main() {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
// ? Create
|
|
23
|
+
// You can create dynamic scheduled runs programmatically via the API
|
|
24
|
+
const createdScheduledRun = yield hatchet.schedule.create(simple_worker_1.simpleWorkflow, // workflow object or string workflow id
|
|
25
|
+
{
|
|
26
|
+
triggerAt: new Date(Date.now() + 1000 * 60 * 60 * 24), // 24 hours from now
|
|
27
|
+
input: {
|
|
28
|
+
name: 'John Doe',
|
|
29
|
+
},
|
|
30
|
+
additionalMetadata: {
|
|
31
|
+
customerId: '123',
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
const { id } = createdScheduledRun.metadata; // id which you can later use to reference the scheduled run
|
|
35
|
+
// !!
|
|
36
|
+
// ? Get
|
|
37
|
+
// You can get a specific scheduled run by passing in the scheduled run id
|
|
38
|
+
const scheduledRun = yield hatchet.schedule.get(id);
|
|
39
|
+
// !!
|
|
40
|
+
// ? Delete
|
|
41
|
+
// You can delete a scheduled run by passing the scheduled run object
|
|
42
|
+
// or a scheduled run Id to the delete method
|
|
43
|
+
yield hatchet.schedule.delete(scheduledRun);
|
|
44
|
+
// !!
|
|
45
|
+
// ? List
|
|
46
|
+
// You can list all scheduled runs by passing in a query object
|
|
47
|
+
const scheduledRunList = yield hatchet.schedule.list({
|
|
48
|
+
offset: 0,
|
|
49
|
+
limit: 10,
|
|
50
|
+
});
|
|
51
|
+
// !!
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
main();
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { Workflow } from '../workflow';
|
|
2
|
+
export declare const simpleWorkflow: Workflow;
|
|
@@ -12,12 +12,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.simpleWorkflow = void 0;
|
|
15
16
|
const sdk_1 = __importDefault(require("../sdk"));
|
|
16
17
|
const hatchet = sdk_1.default.init();
|
|
17
18
|
const sleep = (ms) => new Promise((resolve) => {
|
|
18
19
|
setTimeout(resolve, ms);
|
|
19
20
|
});
|
|
20
|
-
|
|
21
|
+
exports.simpleWorkflow = {
|
|
21
22
|
id: 'simple-workflow',
|
|
22
23
|
description: 'test',
|
|
23
24
|
on: {
|
|
@@ -48,8 +49,10 @@ const workflow = {
|
|
|
48
49
|
function main() {
|
|
49
50
|
return __awaiter(this, void 0, void 0, function* () {
|
|
50
51
|
const worker = yield hatchet.worker('example-worker');
|
|
51
|
-
yield worker.registerWorkflow(
|
|
52
|
+
yield worker.registerWorkflow(exports.simpleWorkflow);
|
|
52
53
|
worker.start();
|
|
53
54
|
});
|
|
54
55
|
}
|
|
55
|
-
main
|
|
56
|
+
if (require.main === module) {
|
|
57
|
+
main();
|
|
58
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hatchet-dev/typescript-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "Background task orchestration & visibility for developers",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -17,13 +17,13 @@
|
|
|
17
17
|
"scripts": {
|
|
18
18
|
"build": "echo 'build hatchet sdk with `npn run tsc:build` to ensure it is not build during the publish step' && exit 0",
|
|
19
19
|
"prepare": "npm run build",
|
|
20
|
-
"
|
|
20
|
+
"dump-version": "node -e \"console.log('export const HATCHET_VERSION = \\'' + require('./package.json').version + '\\';');\" > src/version.ts",
|
|
21
|
+
"tsc:build": "npm run dump-version && tsc && resolve-tspaths",
|
|
21
22
|
"test:unit": "jest --testMatch='**/*.test.ts'",
|
|
22
23
|
"test:e2e": "jest --testMatch='**/*.e2e.ts'",
|
|
23
24
|
"test:unit:watch": "jest --testMatch='**/*.test.ts' --watch",
|
|
24
25
|
"generate": "pnpm run '/generate-.*/'",
|
|
25
|
-
"generate-api": "npx --yes swagger-cli bundle
|
|
26
|
-
"generate-cloud-api": "npx --yes swagger-cli bundle ./hatchet-cloud/api-contracts/openapi/openapi.yaml --outfile openapi.yaml --type yaml && npx swagger-typescript-api -p openapi.yaml -o src/clients/rest/generated/cloud -n hatchet.ts --modular --axios",
|
|
26
|
+
"generate-api": "npx --yes swagger-cli bundle ../oss/api-contracts/openapi/openapi.yaml --outfile openapi.yaml --type yaml && npx swagger-typescript-api -p openapi.yaml -o src/clients/rest/generated -n hatchet.ts --modular --axios",
|
|
27
27
|
"generate-protoc": "./generate-protoc.sh",
|
|
28
28
|
"lint:check": "npm run eslint:check && npm run prettier:check",
|
|
29
29
|
"lint:fix": "npm run eslint:fix && npm run prettier:fix",
|
|
@@ -39,7 +39,6 @@
|
|
|
39
39
|
"example:rate": "npm run exec -- ./src/examples/rate-limit/events.ts",
|
|
40
40
|
"worker:fanout": "npm run exec -- ./src/examples/fanout-worker.ts",
|
|
41
41
|
"worker:simple": "npm run exec -- ./src/examples/simple-worker.ts",
|
|
42
|
-
"worker:managed": "npm run exec -- ./src/examples/managed-worker.ts",
|
|
43
42
|
"worker:affinity": "npm run exec -- ./src/examples/affinity-workers.ts",
|
|
44
43
|
"worker:sticky": "npm run exec -- ./src/examples/sticky-worker.ts",
|
|
45
44
|
"worker:on-failure": "npm run exec -- ./src/examples/on-failure.ts",
|
|
@@ -55,12 +54,13 @@
|
|
|
55
54
|
"event:concurrency:rr": "npm run exec -- ./src/examples/concurrency/group-round-robin/concurrency-event.ts",
|
|
56
55
|
"worker:playground": "npm run exec -- ./src/examples/playground.ts",
|
|
57
56
|
"worker:retries": "npm run exec -- ./src/examples/retries-worker.ts",
|
|
57
|
+
"worker:retries-with-backoff": "npm run exec -- ./src/examples/retries-with-backoff.ts",
|
|
58
58
|
"worker:multi-workflow": "npm run exec -- ./src/examples/multi-workflow.ts",
|
|
59
59
|
"worker:logger": "npm run exec -- ./src/examples/logger.ts",
|
|
60
60
|
"api": "npm run exec -- ./src/examples/api.ts",
|
|
61
61
|
"prepublish": "cp package.json dist/package.json;",
|
|
62
|
-
"publish:ci": "rm -rf ./dist && npm run tsc:build && npm run prepublish && cd dist && npm publish --access public --no-git-checks",
|
|
63
|
-
"publish:ci:alpha": "rm -rf ./dist && npm run tsc:build && npm run prepublish && cd dist && npm publish --access public --no-git-checks --tag alpha",
|
|
62
|
+
"publish:ci": "rm -rf ./dist && npm run dump-version && npm run tsc:build && npm run prepublish && cd dist && npm publish --access public --no-git-checks",
|
|
63
|
+
"publish:ci:alpha": "rm -rf ./dist && npm run dump-version && npm run tsc:build && npm run prepublish && cd dist && npm publish --access public --no-git-checks --tag alpha",
|
|
64
64
|
"generate-docs": "typedoc"
|
|
65
65
|
},
|
|
66
66
|
"keywords": [],
|
|
@@ -93,7 +93,7 @@
|
|
|
93
93
|
"ts-jest": "^29.1.1",
|
|
94
94
|
"ts-node": "^10.9.2",
|
|
95
95
|
"ts-proto": "^2.0.2",
|
|
96
|
-
"typedoc": "^0.
|
|
96
|
+
"typedoc": "^0.27.1",
|
|
97
97
|
"typedoc-plugin-markdown": "^4.0.2",
|
|
98
98
|
"typescript": "^5.3.3"
|
|
99
99
|
},
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
|
|
3
3
|
// versions:
|
|
4
|
-
// protoc-gen-ts_proto v2.
|
|
4
|
+
// protoc-gen-ts_proto v2.5.0
|
|
5
5
|
// protoc v3.19.1
|
|
6
6
|
// source: dispatcher/dispatcher.proto
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
package/protoc/events/events.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
|
|
3
3
|
// versions:
|
|
4
|
-
// protoc-gen-ts_proto v2.
|
|
4
|
+
// protoc-gen-ts_proto v2.5.0
|
|
5
5
|
// protoc v3.19.1
|
|
6
6
|
// source: events/events.proto
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
|
|
3
3
|
// versions:
|
|
4
|
-
// protoc-gen-ts_proto v2.
|
|
4
|
+
// protoc-gen-ts_proto v2.5.0
|
|
5
5
|
// protoc v3.19.1
|
|
6
6
|
// source: google/protobuf/timestamp.proto
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
25
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
36
|
exports.BytesValue = exports.StringValue = exports.BoolValue = exports.UInt32Value = exports.Int32Value = exports.UInt64Value = exports.Int64Value = exports.FloatValue = exports.DoubleValue = exports.protobufPackage = void 0;
|
|
27
37
|
/* eslint-disable */
|
|
@@ -145,6 +145,10 @@ export interface CreateWorkflowStepOpts {
|
|
|
145
145
|
workerLabels: {
|
|
146
146
|
[key: string]: DesiredWorkerLabels;
|
|
147
147
|
};
|
|
148
|
+
/** (optional) the retry backoff factor for the step */
|
|
149
|
+
backoffFactor?: number | undefined;
|
|
150
|
+
/** (optional) the maximum backoff time for the step */
|
|
151
|
+
backoffMaxSeconds?: number | undefined;
|
|
148
152
|
}
|
|
149
153
|
export interface CreateWorkflowStepOpts_WorkerLabelsEntry {
|
|
150
154
|
key: string;
|
|
@@ -186,6 +190,13 @@ export interface ScheduleWorkflowRequest {
|
|
|
186
190
|
* child key will be a no-op, even if the schedule has changed.
|
|
187
191
|
*/
|
|
188
192
|
childKey?: string | undefined;
|
|
193
|
+
/** (optional) the additional metadata for the workflow */
|
|
194
|
+
additionalMetadata?: string | undefined;
|
|
195
|
+
}
|
|
196
|
+
/** ScheduledWorkflow represents a scheduled workflow. */
|
|
197
|
+
export interface ScheduledWorkflow {
|
|
198
|
+
id: string;
|
|
199
|
+
triggerAt: Date | undefined;
|
|
189
200
|
}
|
|
190
201
|
/** WorkflowVersion represents the WorkflowVersion model. */
|
|
191
202
|
export interface WorkflowVersion {
|
|
@@ -195,6 +206,7 @@ export interface WorkflowVersion {
|
|
|
195
206
|
version: string;
|
|
196
207
|
order: number;
|
|
197
208
|
workflowId: string;
|
|
209
|
+
scheduledWorkflows: ScheduledWorkflow[];
|
|
198
210
|
}
|
|
199
211
|
/** WorkflowTriggerEventRef represents the WorkflowTriggerEventRef model. */
|
|
200
212
|
export interface WorkflowTriggerEventRef {
|
|
@@ -265,6 +277,7 @@ export declare const CreateWorkflowStepOpts_WorkerLabelsEntry: MessageFns<Create
|
|
|
265
277
|
export declare const CreateStepRateLimit: MessageFns<CreateStepRateLimit>;
|
|
266
278
|
export declare const ListWorkflowsRequest: MessageFns<ListWorkflowsRequest>;
|
|
267
279
|
export declare const ScheduleWorkflowRequest: MessageFns<ScheduleWorkflowRequest>;
|
|
280
|
+
export declare const ScheduledWorkflow: MessageFns<ScheduledWorkflow>;
|
|
268
281
|
export declare const WorkflowVersion: MessageFns<WorkflowVersion>;
|
|
269
282
|
export declare const WorkflowTriggerEventRef: MessageFns<WorkflowTriggerEventRef>;
|
|
270
283
|
export declare const WorkflowTriggerCronRef: MessageFns<WorkflowTriggerCronRef>;
|