@hatchet-dev/typescript-sdk 1.1.1 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -33
- package/clients/worker/worker.d.ts +1 -1
- package/clients/worker/worker.js +6 -5
- package/package.json +3 -34
- package/step.js +3 -3
- package/v1/client/client.d.ts +12 -0
- package/v1/client/client.js +18 -0
- package/v1/client/features/runs.d.ts +32 -2
- package/v1/client/features/runs.js +25 -6
- package/v1/examples/cancellations/workflow.d.ts +1 -0
- package/v1/examples/cancellations/workflow.js +25 -3
- package/v1/examples/child_workflows/worker.js +1 -0
- package/v1/examples/dag_match_condition/complex-workflow.d.ts +1 -0
- package/v1/examples/dag_match_condition/complex-workflow.js +107 -0
- package/v1/examples/{rate_limit/run.js → durable-event/event.js} +10 -11
- package/v1/examples/durable-event/run.js +30 -0
- package/v1/examples/durable-event/worker.d.ts +1 -0
- package/v1/examples/{rate_limit → durable-event}/worker.js +2 -2
- package/v1/examples/durable-event/workflow.d.ts +6 -0
- package/v1/examples/durable-event/workflow.js +46 -0
- package/v1/examples/durable-sleep/workflow.js +2 -8
- package/v1/examples/on_event/event.js +2 -2
- package/v1/examples/on_event/workflow.js +2 -4
- package/v1/examples/rate_limit/workflow.d.ts +0 -4
- package/v1/examples/rate_limit/workflow.js +11 -9
- package/v1/examples/simple/cron.js +10 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- /package/v1/examples/{rate_limit/run.d.ts → durable-event/event.d.ts} +0 -0
- /package/v1/examples/{rate_limit/worker.d.ts → durable-event/run.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -32,39 +32,7 @@ pnpm add @hatchet-dev/typescript-sdk
|
|
|
32
32
|
|
|
33
33
|
## Quick Start
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
```typescript
|
|
38
|
-
import { HatchetClient } from '@hatchet-dev/typescript-sdk';
|
|
39
|
-
|
|
40
|
-
export const hatchet = HatchetClient.init();
|
|
41
|
-
|
|
42
|
-
export type SimpleInput = {
|
|
43
|
-
Message: string;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export const simple = hatchet.task({
|
|
47
|
-
name: 'simple',
|
|
48
|
-
fn: (input: SimpleInput) => {
|
|
49
|
-
return {
|
|
50
|
-
TransformedMessage: input.Message.toLowerCase(),
|
|
51
|
-
};
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
async function main() {
|
|
56
|
-
const worker = await hatchet.worker('simple-worker', {
|
|
57
|
-
workflows: [simple],
|
|
58
|
-
slots: 100,
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
await worker.start();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (require.main === module) {
|
|
65
|
-
main();
|
|
66
|
-
}
|
|
67
|
-
```
|
|
35
|
+
For examples of how to use the Hatchet TypeScript SDK, including worker setup and task execution, please see our [official documentation](https://docs.hatchet.run/home/setup).
|
|
68
36
|
|
|
69
37
|
## Features
|
|
70
38
|
|
|
@@ -51,7 +51,7 @@ export declare class V0Worker {
|
|
|
51
51
|
registerAction<T, K>(actionId: string, action: StepRunFunction<T, K>): void;
|
|
52
52
|
handleStartStepRun(action: Action): Promise<void>;
|
|
53
53
|
handleStartGroupKeyRun(action: Action): Promise<void>;
|
|
54
|
-
getStepActionEvent(action: Action, eventType: StepActionEventType, shouldNotRetry: boolean, payload?: any): StepActionEvent;
|
|
54
|
+
getStepActionEvent(action: Action, eventType: StepActionEventType, shouldNotRetry: boolean, payload?: any, retryCount?: number): StepActionEvent;
|
|
55
55
|
getGroupKeyActionEvent(action: Action, eventType: GroupKeyActionEventType, payload?: any): GroupKeyActionEvent;
|
|
56
56
|
handleCancelStepRun(action: Action): Promise<void>;
|
|
57
57
|
stop(): Promise<void>;
|
package/clients/worker/worker.js
CHANGED
|
@@ -346,13 +346,13 @@ class V0Worker {
|
|
|
346
346
|
this.logger.info(`Step run ${action.stepRunId} succeeded`);
|
|
347
347
|
try {
|
|
348
348
|
// Send the action event to the dispatcher
|
|
349
|
-
const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_COMPLETED, false, result || null);
|
|
349
|
+
const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_COMPLETED, false, result || null, action.retryCount);
|
|
350
350
|
yield this.client.dispatcher.sendStepActionEvent(event);
|
|
351
351
|
}
|
|
352
352
|
catch (actionEventError) {
|
|
353
353
|
this.logger.error(`Could not send completed action event: ${actionEventError.message || actionEventError}`);
|
|
354
354
|
// send a failure event
|
|
355
|
-
const failureEvent = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, false, actionEventError.message);
|
|
355
|
+
const failureEvent = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, false, actionEventError.message, action.retryCount);
|
|
356
356
|
try {
|
|
357
357
|
yield this.client.dispatcher.sendStepActionEvent(failureEvent);
|
|
358
358
|
}
|
|
@@ -378,7 +378,7 @@ class V0Worker {
|
|
|
378
378
|
const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, shouldNotRetry, {
|
|
379
379
|
message: error === null || error === void 0 ? void 0 : error.message,
|
|
380
380
|
stack: error === null || error === void 0 ? void 0 : error.stack,
|
|
381
|
-
});
|
|
381
|
+
}, action.retryCount);
|
|
382
382
|
yield this.client.dispatcher.sendStepActionEvent(event);
|
|
383
383
|
}
|
|
384
384
|
catch (e) {
|
|
@@ -403,7 +403,7 @@ class V0Worker {
|
|
|
403
403
|
}))());
|
|
404
404
|
this.futures[action.stepRunId] = future;
|
|
405
405
|
// Send the action event to the dispatcher
|
|
406
|
-
const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_STARTED, false);
|
|
406
|
+
const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_STARTED, false, undefined, action.retryCount);
|
|
407
407
|
this.client.dispatcher.sendStepActionEvent(event).catch((e) => {
|
|
408
408
|
this.logger.error(`Could not send action event: ${e.message}`);
|
|
409
409
|
});
|
|
@@ -489,7 +489,7 @@ class V0Worker {
|
|
|
489
489
|
}
|
|
490
490
|
});
|
|
491
491
|
}
|
|
492
|
-
getStepActionEvent(action, eventType, shouldNotRetry, payload = '') {
|
|
492
|
+
getStepActionEvent(action, eventType, shouldNotRetry, payload = '', retryCount = 0) {
|
|
493
493
|
return {
|
|
494
494
|
workerId: this.name,
|
|
495
495
|
jobId: action.jobId,
|
|
@@ -501,6 +501,7 @@ class V0Worker {
|
|
|
501
501
|
eventType,
|
|
502
502
|
eventPayload: JSON.stringify(payload),
|
|
503
503
|
shouldNotRetry,
|
|
504
|
+
retryCount,
|
|
504
505
|
};
|
|
505
506
|
}
|
|
506
507
|
getGroupKeyActionEvent(action, eventType, payload = '') {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hatchet-dev/typescript-sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Background task orchestration & visibility for developers",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -32,42 +32,11 @@
|
|
|
32
32
|
"eslint:fix": "eslint \"{src,tests}/**/*.{ts,tsx,js}\" --fix",
|
|
33
33
|
"prettier:check": "prettier \"src/**/*.{ts,tsx}\" --list-different",
|
|
34
34
|
"prettier:fix": "prettier \"src/**/*.{ts,tsx}\" --write",
|
|
35
|
-
"exec": "npx dotenv -- ts-node -r tsconfig-paths/register --project tsconfig.json",
|
|
36
|
-
"example:event": "npm run exec -- ./src/examples/example-event.ts",
|
|
37
|
-
"example:event-listen": "npm run exec -- ./src/examples/example-event-with-results.ts",
|
|
38
|
-
"worker:namespaced": "npm run exec -- ./src/examples/namespaced-worker.ts",
|
|
39
|
-
"worker:rate": "npm run exec -- ./src/examples/rate-limit/worker.ts",
|
|
40
|
-
"example:rate": "npm run exec -- ./src/examples/rate-limit/events.ts",
|
|
41
|
-
"worker:fanout": "npm run exec -- ./src/examples/fanout-worker.ts",
|
|
42
|
-
"worker:simple": "npm run exec -- ./src/examples/simple-worker.ts",
|
|
43
|
-
"worker:affinity": "npm run exec -- ./src/examples/affinity-workers.ts",
|
|
44
|
-
"worker:sticky": "npm run exec -- ./src/examples/sticky-worker.ts",
|
|
45
|
-
"worker:sticky-with-check": "npm run exec -- ./src/examples/sticky-worker-with-check.ts",
|
|
46
|
-
"trigger:sticky": "npm run exec -- ./src/examples/sticky-trigger.ts",
|
|
47
|
-
"worker:on-failure": "npm run exec -- ./src/examples/on-failure.ts",
|
|
48
|
-
"manual:trigger": "npm run exec -- ./src/examples/manual-trigger.ts",
|
|
49
|
-
"manual:meta": "npm run exec -- ./src/examples/stream-by-additional-meta.ts",
|
|
50
|
-
"bulk:trigger": "npm run exec -- ./src/examples/bulk-trigger.ts",
|
|
51
|
-
"bulk:fanout:worker": "npm run exec -- ./src/examples/bulk-fanout-worker.ts",
|
|
52
|
-
"bulk:fanout:trigger": "npm run exec -- ./src/examples/bulk-fanout-trigger.ts",
|
|
53
|
-
"worker:dag": "npm run exec -- ./src/examples/dag-worker.ts",
|
|
54
|
-
"worker:concurrency": "npm run exec -- ./src/examples/concurrency/cancel-in-progress/concurrency-worker.ts",
|
|
55
|
-
"event:concurrency": "npm run exec -- ./src/examples/concurrency/cancel-in-progress/concurrency-event.ts",
|
|
56
|
-
"worker:concurrency:rr": "npm run exec -- ./src/examples/concurrency/group-round-robin/concurrency-worker-expression.ts",
|
|
57
|
-
"event:concurrency:rr": "npm run exec -- ./src/examples/concurrency/group-round-robin/concurrency-event.ts",
|
|
58
|
-
"worker:playground": "npm run exec -- ./src/examples/playground.ts",
|
|
59
|
-
"worker:retries": "npm run exec -- ./src/examples/retries-worker.ts",
|
|
60
|
-
"worker:retries-with-backoff": "npm run exec -- ./src/examples/retries-with-backoff.ts",
|
|
61
|
-
"worker:multi-workflow": "npm run exec -- ./src/examples/multi-workflow.ts",
|
|
62
|
-
"worker:logger": "npm run exec -- ./src/examples/logger.ts",
|
|
63
|
-
"worker:byo-logger": "npm run exec -- ./src/examples/byo-logger.ts",
|
|
64
|
-
"worker:no-retry": "npm run exec -- ./src/v1/examples/non_retryable/worker.ts",
|
|
65
|
-
"worker:no-retry:trigger": "npm run exec -- ./src/v1/examples/non_retryable/run.ts",
|
|
66
|
-
"api": "npm run exec -- ./src/examples/api.ts",
|
|
67
35
|
"prepublish": "cp package.json dist/package.json; cp README.md dist/",
|
|
68
36
|
"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",
|
|
69
37
|
"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",
|
|
70
|
-
"generate-docs": "typedoc"
|
|
38
|
+
"generate-docs": "typedoc",
|
|
39
|
+
"exec": "npx dotenv -- ts-node -r tsconfig-paths/register --project tsconfig.json"
|
|
71
40
|
},
|
|
72
41
|
"keywords": [],
|
|
73
42
|
"author": "",
|
package/step.js
CHANGED
|
@@ -413,10 +413,10 @@ class Context {
|
|
|
413
413
|
return resp;
|
|
414
414
|
});
|
|
415
415
|
try {
|
|
416
|
-
|
|
416
|
+
const batchSize = 1000;
|
|
417
417
|
let resp = [];
|
|
418
|
-
for (let i = 0; i < workflowRuns.length; i +=
|
|
419
|
-
const batch = workflowRuns.slice(i, i +
|
|
418
|
+
for (let i = 0; i < workflowRuns.length; i += batchSize) {
|
|
419
|
+
const batch = workflowRuns.slice(i, i + batchSize);
|
|
420
420
|
const batchResp = yield this.client.admin.runWorkflows(batch);
|
|
421
421
|
resp = resp.concat(batchResp);
|
|
422
422
|
}
|
package/v1/client/client.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { WorkflowsClient } from './features/workflows';
|
|
|
13
13
|
import { RunsClient } from './features/runs';
|
|
14
14
|
import { CreateStandaloneDurableTaskOpts } from '../task';
|
|
15
15
|
import { InputType, OutputType, UnknownInputType, StrictWorkflowOutputType } from '../types';
|
|
16
|
+
import { RatelimitsClient } from './features';
|
|
16
17
|
/**
|
|
17
18
|
* HatchetV1 implements the main client interface for interacting with the Hatchet workflow engine.
|
|
18
19
|
* It provides methods for creating and executing workflows, as well as managing workers.
|
|
@@ -159,6 +160,12 @@ export declare class HatchetClient implements IHatchetClient {
|
|
|
159
160
|
* @returns A metrics client instance
|
|
160
161
|
*/
|
|
161
162
|
get metrics(): MetricsClient;
|
|
163
|
+
private _ratelimits;
|
|
164
|
+
/**
|
|
165
|
+
* Get the rate limits client for creating and managing rate limits
|
|
166
|
+
* @returns A rate limits client instance
|
|
167
|
+
*/
|
|
168
|
+
get ratelimits(): RatelimitsClient;
|
|
162
169
|
private _runs;
|
|
163
170
|
/**
|
|
164
171
|
* Get the runs client for creating and managing runs
|
|
@@ -171,6 +178,11 @@ export declare class HatchetClient implements IHatchetClient {
|
|
|
171
178
|
* @returns A workflows client instance
|
|
172
179
|
*/
|
|
173
180
|
get workflows(): WorkflowsClient;
|
|
181
|
+
/**
|
|
182
|
+
* Get the tasks client for creating and managing tasks
|
|
183
|
+
* @returns A tasks client instance
|
|
184
|
+
*/
|
|
185
|
+
get tasks(): WorkflowsClient;
|
|
174
186
|
private _workers;
|
|
175
187
|
/**
|
|
176
188
|
* Get the workers client for creating and managing workers
|
package/v1/client/client.js
CHANGED
|
@@ -27,6 +27,7 @@ const metrics_1 = require("./features/metrics");
|
|
|
27
27
|
const workers_1 = require("./features/workers");
|
|
28
28
|
const workflows_1 = require("./features/workflows");
|
|
29
29
|
const runs_1 = require("./features/runs");
|
|
30
|
+
const features_1 = require("./features");
|
|
30
31
|
/**
|
|
31
32
|
* HatchetV1 implements the main client interface for interacting with the Hatchet workflow engine.
|
|
32
33
|
* It provides methods for creating and executing workflows, as well as managing workers.
|
|
@@ -210,6 +211,16 @@ class HatchetClient {
|
|
|
210
211
|
}
|
|
211
212
|
return this._metrics;
|
|
212
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Get the rate limits client for creating and managing rate limits
|
|
216
|
+
* @returns A rate limits client instance
|
|
217
|
+
*/
|
|
218
|
+
get ratelimits() {
|
|
219
|
+
if (!this._ratelimits) {
|
|
220
|
+
this._ratelimits = new features_1.RatelimitsClient(this);
|
|
221
|
+
}
|
|
222
|
+
return this._ratelimits;
|
|
223
|
+
}
|
|
213
224
|
/**
|
|
214
225
|
* Get the runs client for creating and managing runs
|
|
215
226
|
* @returns A runs client instance
|
|
@@ -230,6 +241,13 @@ class HatchetClient {
|
|
|
230
241
|
}
|
|
231
242
|
return this._workflows;
|
|
232
243
|
}
|
|
244
|
+
/**
|
|
245
|
+
* Get the tasks client for creating and managing tasks
|
|
246
|
+
* @returns A tasks client instance
|
|
247
|
+
*/
|
|
248
|
+
get tasks() {
|
|
249
|
+
return this.workflows;
|
|
250
|
+
}
|
|
233
251
|
/**
|
|
234
252
|
* Get the workers client for creating and managing workers
|
|
235
253
|
* @returns A workers client instance
|
|
@@ -3,7 +3,7 @@ import { V1TaskStatus } from '../../../clients/rest/generated/data-contracts';
|
|
|
3
3
|
import { WorkflowsClient } from './workflows';
|
|
4
4
|
import { HatchetClient } from '../client';
|
|
5
5
|
export type RunFilter = {
|
|
6
|
-
since
|
|
6
|
+
since?: Date;
|
|
7
7
|
until?: Date;
|
|
8
8
|
statuses?: V1TaskStatus[];
|
|
9
9
|
workflowNames?: string[];
|
|
@@ -17,6 +17,35 @@ export type ReplayRunOpts = {
|
|
|
17
17
|
ids?: string[];
|
|
18
18
|
filters?: RunFilter;
|
|
19
19
|
};
|
|
20
|
+
export interface ListRunsOpts extends RunFilter {
|
|
21
|
+
/**
|
|
22
|
+
* The number to skip
|
|
23
|
+
* @format int64
|
|
24
|
+
*/
|
|
25
|
+
offset?: number;
|
|
26
|
+
/**
|
|
27
|
+
* The number to limit by
|
|
28
|
+
* @format int64
|
|
29
|
+
*/
|
|
30
|
+
limit?: number;
|
|
31
|
+
/** A list of statuses to filter by */
|
|
32
|
+
/**
|
|
33
|
+
* The worker id to filter by
|
|
34
|
+
* @format uuid
|
|
35
|
+
* @minLength 36
|
|
36
|
+
* @maxLength 36
|
|
37
|
+
*/
|
|
38
|
+
workerId?: string;
|
|
39
|
+
/** Whether to include DAGs or only to include tasks */
|
|
40
|
+
onlyTasks: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* The parent task external id to filter by
|
|
43
|
+
* @format uuid
|
|
44
|
+
* @minLength 36
|
|
45
|
+
* @maxLength 36
|
|
46
|
+
*/
|
|
47
|
+
parentTaskExternalId?: string;
|
|
48
|
+
}
|
|
20
49
|
/**
|
|
21
50
|
* RunsClient is used to list and manage runs
|
|
22
51
|
*/
|
|
@@ -27,8 +56,9 @@ export declare class RunsClient {
|
|
|
27
56
|
constructor(client: HatchetClient);
|
|
28
57
|
get<T = any>(run: string | WorkflowRunRef<T>): Promise<import("../../../clients/rest/generated/data-contracts").WorkflowRun>;
|
|
29
58
|
getDetails<T = any>(run: string | WorkflowRunRef<T>): Promise<import("../../../clients/rest/generated/data-contracts").WorkflowRunShape>;
|
|
30
|
-
list(opts?:
|
|
59
|
+
list(opts?: Partial<ListRunsOpts>): Promise<import("../../../clients/rest/generated/data-contracts").V1TaskSummaryList>;
|
|
31
60
|
cancel(opts: CancelRunOpts): Promise<import("axios").AxiosResponse<void, any>>;
|
|
32
61
|
replay(opts: ReplayRunOpts): Promise<import("axios").AxiosResponse<void, any>>;
|
|
33
62
|
private prepareFilter;
|
|
63
|
+
private prepareListFilter;
|
|
34
64
|
}
|
|
@@ -35,14 +35,13 @@ class RunsClient {
|
|
|
35
35
|
}
|
|
36
36
|
list(opts) {
|
|
37
37
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
-
|
|
39
|
-
const { data } = yield this.api.workflowRunList(this.tenantId, opts);
|
|
38
|
+
const { data } = yield this.api.v1WorkflowRunList(this.tenantId, Object.assign({}, (yield this.prepareListFilter(opts || {}))));
|
|
40
39
|
return data;
|
|
41
40
|
});
|
|
42
41
|
}
|
|
43
42
|
cancel(opts) {
|
|
44
43
|
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
const filter =
|
|
44
|
+
const filter = yield this.prepareFilter(opts.filters || {});
|
|
46
45
|
return this.api.v1TaskCancel(this.tenantId, {
|
|
47
46
|
externalIds: opts.ids,
|
|
48
47
|
filter,
|
|
@@ -51,7 +50,7 @@ class RunsClient {
|
|
|
51
50
|
}
|
|
52
51
|
replay(opts) {
|
|
53
52
|
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
const filter =
|
|
53
|
+
const filter = yield this.prepareFilter(opts.filters || {});
|
|
55
54
|
return this.api.v1TaskReplay(this.tenantId, {
|
|
56
55
|
externalIds: opts.ids,
|
|
57
56
|
filter,
|
|
@@ -60,12 +59,32 @@ class RunsClient {
|
|
|
60
59
|
}
|
|
61
60
|
prepareFilter(_a) {
|
|
62
61
|
return __awaiter(this, arguments, void 0, function* ({ since, until, statuses, workflowNames, additionalMetadata, }) {
|
|
62
|
+
const am = Object.entries(additionalMetadata || {}).map(([key, value]) => `${key}:${value}`);
|
|
63
63
|
return {
|
|
64
|
-
|
|
64
|
+
// default to 1 hour ago
|
|
65
|
+
since: since ? since.toISOString() : new Date(Date.now() - 1000 * 60 * 60).toISOString(),
|
|
65
66
|
until: until === null || until === void 0 ? void 0 : until.toISOString(),
|
|
66
67
|
statuses,
|
|
67
68
|
workflowIds: yield Promise.all((workflowNames === null || workflowNames === void 0 ? void 0 : workflowNames.map((name) => __awaiter(this, void 0, void 0, function* () { return (yield this.workflows.get(name)).metadata.id; }))) || []),
|
|
68
|
-
additionalMetadata:
|
|
69
|
+
additionalMetadata: am,
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
prepareListFilter(opts) {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
const am = Object.entries(opts.additionalMetadata || {}).map(([key, value]) => `${key}:${value}`);
|
|
77
|
+
return {
|
|
78
|
+
// default to 1 hour ago
|
|
79
|
+
since: opts.since
|
|
80
|
+
? opts.since.toISOString()
|
|
81
|
+
: new Date(Date.now() - 1000 * 60 * 60).toISOString(),
|
|
82
|
+
until: (_a = opts.until) === null || _a === void 0 ? void 0 : _a.toISOString(),
|
|
83
|
+
statuses: opts.statuses,
|
|
84
|
+
worker_id: opts.workerId,
|
|
85
|
+
workflow_ids: yield Promise.all(((_b = opts.workflowNames) === null || _b === void 0 ? void 0 : _b.map((name) => __awaiter(this, void 0, void 0, function* () { return (yield this.workflows.get(name)).metadata.id; }))) || []),
|
|
86
|
+
additional_metadata: am,
|
|
87
|
+
only_tasks: opts.onlyTasks || false,
|
|
69
88
|
};
|
|
70
89
|
});
|
|
71
90
|
}
|
|
@@ -12,11 +12,11 @@ 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.cancellation = void 0;
|
|
16
|
-
// ❓ Declaring a Task
|
|
15
|
+
exports.abortSignal = exports.cancellation = void 0;
|
|
17
16
|
const sleep_1 = __importDefault(require("../../../util/sleep"));
|
|
17
|
+
const axios_1 = __importDefault(require("axios"));
|
|
18
18
|
const hatchet_client_1 = require("../hatchet-client");
|
|
19
|
-
//
|
|
19
|
+
// ❓ Declaring a Task
|
|
20
20
|
exports.cancellation = hatchet_client_1.hatchet.task({
|
|
21
21
|
name: 'cancellation',
|
|
22
22
|
fn: (_1, _a) => __awaiter(void 0, [_1, _a], void 0, function* (_, { cancelled }) {
|
|
@@ -30,4 +30,26 @@ exports.cancellation = hatchet_client_1.hatchet.task({
|
|
|
30
30
|
}),
|
|
31
31
|
});
|
|
32
32
|
// !!
|
|
33
|
+
// ❓ Abort Signal
|
|
34
|
+
exports.abortSignal = hatchet_client_1.hatchet.task({
|
|
35
|
+
name: 'abort-signal',
|
|
36
|
+
fn: (_1, _a) => __awaiter(void 0, [_1, _a], void 0, function* (_, { controller }) {
|
|
37
|
+
try {
|
|
38
|
+
const response = yield axios_1.default.get('https://api.example.com/data', {
|
|
39
|
+
signal: controller.signal,
|
|
40
|
+
});
|
|
41
|
+
// Handle the response
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
if (axios_1.default.isCancel(error)) {
|
|
45
|
+
// Request was canceled
|
|
46
|
+
console.log('Request canceled');
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Handle other errors
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}),
|
|
53
|
+
});
|
|
54
|
+
// !!
|
|
33
55
|
// see ./worker.ts and ./run.ts for how to run the workflow
|
|
@@ -15,6 +15,7 @@ function main() {
|
|
|
15
15
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
16
|
const worker = yield hatchet_client_1.hatchet.worker('child-workflow-worker', {
|
|
17
17
|
workflows: [workflow_1.parent, workflow_1.child],
|
|
18
|
+
slots: 100,
|
|
18
19
|
});
|
|
19
20
|
yield worker.start();
|
|
20
21
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const taskConditionWorkflow: import("../..").WorkflowDeclaration<import("../..").UnknownInputType, {}>;
|
|
@@ -0,0 +1,107 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.taskConditionWorkflow = void 0;
|
|
13
|
+
// ❓ Create a workflow
|
|
14
|
+
const conditions_1 = require("../../conditions");
|
|
15
|
+
const parent_condition_1 = require("../../conditions/parent-condition");
|
|
16
|
+
const hatchet_client_1 = require("../hatchet-client");
|
|
17
|
+
exports.taskConditionWorkflow = hatchet_client_1.hatchet.workflow({
|
|
18
|
+
name: 'TaskConditionWorkflow',
|
|
19
|
+
});
|
|
20
|
+
// !!
|
|
21
|
+
// ❓ Add base task
|
|
22
|
+
const start = exports.taskConditionWorkflow.task({
|
|
23
|
+
name: 'start',
|
|
24
|
+
fn: () => {
|
|
25
|
+
return {
|
|
26
|
+
randomNumber: Math.floor(Math.random() * 100) + 1,
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
// !!
|
|
31
|
+
// ❓ Add wait for sleep
|
|
32
|
+
const waitForSleep = exports.taskConditionWorkflow.task({
|
|
33
|
+
name: 'waitForSleep',
|
|
34
|
+
parents: [start],
|
|
35
|
+
waitFor: [new conditions_1.SleepCondition('10s')],
|
|
36
|
+
fn: () => {
|
|
37
|
+
return {
|
|
38
|
+
randomNumber: Math.floor(Math.random() * 100) + 1,
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
// !!
|
|
43
|
+
// ❓ Add skip on event
|
|
44
|
+
const skipOnEvent = exports.taskConditionWorkflow.task({
|
|
45
|
+
name: 'skipOnEvent',
|
|
46
|
+
parents: [start],
|
|
47
|
+
waitFor: [new conditions_1.SleepCondition('10s')],
|
|
48
|
+
skipIf: [new conditions_1.UserEventCondition('skip_on_event:skip', 'true')],
|
|
49
|
+
fn: () => {
|
|
50
|
+
return {
|
|
51
|
+
randomNumber: Math.floor(Math.random() * 100) + 1,
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
// !!
|
|
56
|
+
// ❓ Add branching
|
|
57
|
+
const leftBranch = exports.taskConditionWorkflow.task({
|
|
58
|
+
name: 'leftBranch',
|
|
59
|
+
parents: [waitForSleep],
|
|
60
|
+
skipIf: [new parent_condition_1.ParentCondition(waitForSleep, 'output.randomNumber > 50')],
|
|
61
|
+
fn: () => {
|
|
62
|
+
return {
|
|
63
|
+
randomNumber: Math.floor(Math.random() * 100) + 1,
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
const rightBranch = exports.taskConditionWorkflow.task({
|
|
68
|
+
name: 'rightBranch',
|
|
69
|
+
parents: [waitForSleep],
|
|
70
|
+
skipIf: [new parent_condition_1.ParentCondition(waitForSleep, 'output.randomNumber <= 50')],
|
|
71
|
+
fn: () => {
|
|
72
|
+
return {
|
|
73
|
+
randomNumber: Math.floor(Math.random() * 100) + 1,
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
// !!
|
|
78
|
+
// ❓ Add wait for event
|
|
79
|
+
const waitForEvent = exports.taskConditionWorkflow.task({
|
|
80
|
+
name: 'waitForEvent',
|
|
81
|
+
parents: [start],
|
|
82
|
+
waitFor: [(0, conditions_1.Or)(new conditions_1.SleepCondition('1m'), new conditions_1.UserEventCondition('wait_for_event:start', 'true'))],
|
|
83
|
+
fn: () => {
|
|
84
|
+
return {
|
|
85
|
+
randomNumber: Math.floor(Math.random() * 100) + 1,
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
// !!
|
|
90
|
+
// ❓ Add sum
|
|
91
|
+
exports.taskConditionWorkflow.task({
|
|
92
|
+
name: 'sum',
|
|
93
|
+
parents: [start, waitForSleep, waitForEvent, skipOnEvent, leftBranch, rightBranch],
|
|
94
|
+
fn: (_, ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
95
|
+
var _a, _b, _c;
|
|
96
|
+
const one = (yield ctx.parentOutput(start)).randomNumber;
|
|
97
|
+
const two = (yield ctx.parentOutput(waitForEvent)).randomNumber;
|
|
98
|
+
const three = (yield ctx.parentOutput(waitForSleep)).randomNumber;
|
|
99
|
+
const four = ((_a = (yield ctx.parentOutput(skipOnEvent))) === null || _a === void 0 ? void 0 : _a.randomNumber) || 0;
|
|
100
|
+
const five = ((_b = (yield ctx.parentOutput(leftBranch))) === null || _b === void 0 ? void 0 : _b.randomNumber) || 0;
|
|
101
|
+
const six = ((_c = (yield ctx.parentOutput(rightBranch))) === null || _c === void 0 ? void 0 : _c.randomNumber) || 0;
|
|
102
|
+
return {
|
|
103
|
+
sum: one + two + three + four + five + six,
|
|
104
|
+
};
|
|
105
|
+
}),
|
|
106
|
+
});
|
|
107
|
+
// !!
|
|
@@ -9,21 +9,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
|
|
13
|
-
const workflow_1 = require("./workflow");
|
|
12
|
+
const hatchet_client_1 = require("../hatchet-client");
|
|
14
13
|
function main() {
|
|
15
14
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
catch (e) {
|
|
21
|
-
console.log('error', e);
|
|
22
|
-
}
|
|
15
|
+
const event = yield hatchet_client_1.hatchet.events.push('user:update', {
|
|
16
|
+
userId: '1234',
|
|
17
|
+
});
|
|
23
18
|
});
|
|
24
19
|
}
|
|
25
20
|
if (require.main === module) {
|
|
26
21
|
main()
|
|
27
|
-
.
|
|
28
|
-
.
|
|
22
|
+
.then(() => process.exit(0))
|
|
23
|
+
.catch((error) => {
|
|
24
|
+
// eslint-disable-next-line no-console
|
|
25
|
+
console.error('Error:', error);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
});
|
|
29
28
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const workflow_1 = require("./workflow");
|
|
13
|
+
function main() {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const timeStart = Date.now();
|
|
16
|
+
const res = yield workflow_1.durableEvent.run({});
|
|
17
|
+
const timeEnd = Date.now();
|
|
18
|
+
// eslint-disable-next-line no-console
|
|
19
|
+
console.log(`Time taken: ${timeEnd - timeStart}ms`);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
if (require.main === module) {
|
|
23
|
+
main()
|
|
24
|
+
.then(() => process.exit(0))
|
|
25
|
+
.catch((error) => {
|
|
26
|
+
// eslint-disable-next-line no-console
|
|
27
|
+
console.error('Error:', error);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -13,8 +13,8 @@ const hatchet_client_1 = require("../hatchet-client");
|
|
|
13
13
|
const workflow_1 = require("./workflow");
|
|
14
14
|
function main() {
|
|
15
15
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
-
const worker = yield hatchet_client_1.hatchet.worker('
|
|
17
|
-
workflows: [workflow_1.
|
|
16
|
+
const worker = yield hatchet_client_1.hatchet.worker('durable-event-worker', {
|
|
17
|
+
workflows: [workflow_1.durableEvent],
|
|
18
18
|
});
|
|
19
19
|
yield worker.start();
|
|
20
20
|
});
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.durableEventWithFilter = exports.durableEvent = void 0;
|
|
13
|
+
// import sleep from '../../../util/sleep.js';
|
|
14
|
+
const hatchet_client_1 = require("../hatchet-client");
|
|
15
|
+
// ❓ Durable Event
|
|
16
|
+
exports.durableEvent = hatchet_client_1.hatchet.durableTask({
|
|
17
|
+
name: 'durable-event',
|
|
18
|
+
executionTimeout: '10m',
|
|
19
|
+
fn: (_, ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
const res = ctx.waitFor({
|
|
21
|
+
eventKey: 'user:update',
|
|
22
|
+
});
|
|
23
|
+
console.log('res', res);
|
|
24
|
+
return {
|
|
25
|
+
Value: 'done',
|
|
26
|
+
};
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
// !!
|
|
30
|
+
exports.durableEventWithFilter = hatchet_client_1.hatchet.durableTask({
|
|
31
|
+
name: 'durable-event-with-filter',
|
|
32
|
+
executionTimeout: '10m',
|
|
33
|
+
fn: (_, ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
// ❓ Durable Event With Filter
|
|
35
|
+
const res = ctx.waitFor({
|
|
36
|
+
eventKey: 'user:update',
|
|
37
|
+
expression: "input.userId == '1234'",
|
|
38
|
+
});
|
|
39
|
+
// !!
|
|
40
|
+
console.log('res', res);
|
|
41
|
+
return {
|
|
42
|
+
Value: 'done',
|
|
43
|
+
};
|
|
44
|
+
}),
|
|
45
|
+
});
|
|
46
|
+
// !!
|
|
@@ -11,11 +11,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.durableSleep = void 0;
|
|
13
13
|
// import sleep from '../../../util/sleep.js';
|
|
14
|
-
const conditions_1 = require("../../conditions");
|
|
15
14
|
const hatchet_client_1 = require("../hatchet-client");
|
|
16
15
|
exports.durableSleep = hatchet_client_1.hatchet.workflow({
|
|
17
16
|
name: 'durable-sleep',
|
|
18
17
|
});
|
|
18
|
+
// ❓ Durable Sleep
|
|
19
19
|
exports.durableSleep.durableTask({
|
|
20
20
|
name: 'durable-sleep',
|
|
21
21
|
executionTimeout: '10m',
|
|
@@ -23,15 +23,9 @@ exports.durableSleep.durableTask({
|
|
|
23
23
|
console.log('sleeping for 5s');
|
|
24
24
|
const sleepRes = yield ctx.sleepFor('5s');
|
|
25
25
|
console.log('done sleeping for 5s', sleepRes);
|
|
26
|
-
// wait for either an event or a sleep
|
|
27
|
-
const res = yield ctx.waitFor((0, conditions_1.Or)({
|
|
28
|
-
eventKey: 'user:event',
|
|
29
|
-
}, {
|
|
30
|
-
sleepFor: '1m',
|
|
31
|
-
}));
|
|
32
|
-
console.log('res', res);
|
|
33
26
|
return {
|
|
34
27
|
Value: 'done',
|
|
35
28
|
};
|
|
36
29
|
}),
|
|
37
30
|
});
|
|
31
|
+
// !!
|
|
@@ -10,10 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
const hatchet_client_1 = require("../hatchet-client");
|
|
13
|
-
const workflow_1 = require("./workflow");
|
|
14
13
|
function main() {
|
|
15
14
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
-
|
|
15
|
+
// ❓ Pushing an Event
|
|
16
|
+
const res = yield hatchet_client_1.hatchet.events.push('simple-event:create', {
|
|
17
17
|
Message: 'hello',
|
|
18
18
|
});
|
|
19
19
|
// !!
|
|
@@ -6,10 +6,8 @@ exports.SIMPLE_EVENT = 'simple-event:create';
|
|
|
6
6
|
// ❓ Run workflow on event
|
|
7
7
|
exports.lower = hatchet_client_1.hatchet.workflow({
|
|
8
8
|
name: 'lower',
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
event: exports.SIMPLE_EVENT,
|
|
12
|
-
},
|
|
9
|
+
// 👀 Declare the event that will trigger the workflow
|
|
10
|
+
onEvents: ['simple-event:create'],
|
|
13
11
|
});
|
|
14
12
|
// !!
|
|
15
13
|
exports.lower.task({
|
|
@@ -1,29 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rateLimitWorkflow = void 0;
|
|
4
3
|
const workflows_1 = require("../../../protoc/v1/workflows");
|
|
5
4
|
const hatchet_client_1 = require("../hatchet-client");
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
// ❓ Upsert Rate Limit
|
|
6
|
+
hatchet_client_1.hatchet.ratelimits.upsert({
|
|
7
|
+
key: 'api-service-rate-limit',
|
|
8
|
+
limit: 10,
|
|
9
|
+
duration: workflows_1.RateLimitDuration.SECOND,
|
|
8
10
|
});
|
|
9
11
|
// !!
|
|
10
12
|
// ❓ Static
|
|
11
|
-
const RATE_LIMIT_KEY = '
|
|
12
|
-
const task1 =
|
|
13
|
+
const RATE_LIMIT_KEY = 'api-service-rate-limit';
|
|
14
|
+
const task1 = hatchet_client_1.hatchet.task({
|
|
13
15
|
name: 'task1',
|
|
14
|
-
fn: (input) => {
|
|
15
|
-
console.log('executed task1');
|
|
16
|
-
},
|
|
17
16
|
rateLimits: [
|
|
18
17
|
{
|
|
19
18
|
staticKey: RATE_LIMIT_KEY,
|
|
20
19
|
units: 1,
|
|
21
20
|
},
|
|
22
21
|
],
|
|
22
|
+
fn: (input) => {
|
|
23
|
+
console.log('executed task1');
|
|
24
|
+
},
|
|
23
25
|
});
|
|
24
26
|
// !!
|
|
25
27
|
// ❓ Dynamic
|
|
26
|
-
const task2 =
|
|
28
|
+
const task2 = hatchet_client_1.hatchet.task({
|
|
27
29
|
name: 'task2',
|
|
28
30
|
fn: (input) => {
|
|
29
31
|
console.log('executed task2 for user: ', input.userId);
|
|
@@ -22,7 +22,16 @@ function main() {
|
|
|
22
22
|
// !!
|
|
23
23
|
// eslint-disable-next-line no-console
|
|
24
24
|
console.log(cron.metadata.id);
|
|
25
|
-
|
|
25
|
+
// ❓ Delete
|
|
26
|
+
yield hatchet_client_1.hatchet.crons.delete(cronId);
|
|
27
|
+
// !!
|
|
28
|
+
// ❓ List
|
|
29
|
+
const crons = yield hatchet_client_1.hatchet.crons.list({
|
|
30
|
+
workflowId: workflow_1.simple.id,
|
|
31
|
+
});
|
|
32
|
+
// !!
|
|
33
|
+
// eslint-disable-next-line no-console
|
|
34
|
+
console.log(crons);
|
|
26
35
|
});
|
|
27
36
|
}
|
|
28
37
|
if (require.main === module) {
|
package/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const HATCHET_VERSION = "1.1.
|
|
1
|
+
export declare const HATCHET_VERSION = "1.1.3";
|
package/version.js
CHANGED
|
File without changes
|
|
File without changes
|