@hatchet-dev/typescript-sdk 0.12.4 → 0.13.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.
@@ -57,8 +57,9 @@ class Worker {
57
57
  }
58
58
  : {};
59
59
  this.action_registry = Object.assign(Object.assign(Object.assign({}, this.action_registry), newActions), onFailureAction);
60
- this.action_registry = ((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.name)
61
- ? Object.assign(Object.assign({}, this.action_registry), { [`${workflow.id}:${workflow.concurrency.name}`]: workflow.concurrency.key }) : Object.assign({}, this.action_registry);
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);
62
63
  }
63
64
  getHandler(workflows) {
64
65
  for (const workflow of workflows) {
@@ -82,13 +83,19 @@ class Worker {
82
83
  }
83
84
  registerWorkflow(initWorkflow) {
84
85
  return __awaiter(this, void 0, void 0, function* () {
85
- var _a, _b;
86
+ var _a, _b, _c, _d;
86
87
  const workflow = Object.assign(Object.assign({}, initWorkflow), { id: this.client.config.namespace + initWorkflow.id });
87
88
  try {
88
- const concurrency = ((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.name)
89
+ if (((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.key) && workflow.concurrency.expression) {
90
+ throw new hatchet_error_1.default('Cannot have both key function and expression in workflow concurrency configuration');
91
+ }
92
+ const concurrency = ((_b = workflow.concurrency) === null || _b === void 0 ? void 0 : _b.name) || ((_c = workflow.concurrency) === null || _c === void 0 ? void 0 : _c.expression)
89
93
  ? {
90
- action: `${workflow.id}:${workflow.concurrency.name}`,
94
+ action: !workflow.concurrency.expression
95
+ ? `${workflow.id}:${workflow.concurrency.name}`
96
+ : undefined,
91
97
  maxRuns: workflow.concurrency.maxRuns || 1,
98
+ expression: workflow.concurrency.expression,
92
99
  limitStrategy: workflow.concurrency.limitStrategy || workflows_1.ConcurrencyLimitStrategy.CANCEL_IN_PROGRESS,
93
100
  }
94
101
  : undefined;
@@ -105,7 +112,7 @@ class Worker {
105
112
  parents: [],
106
113
  userData: '{}',
107
114
  retries: workflow.onFailure.retries || 0,
108
- rateLimits: (_b = workflow.onFailure.rate_limits) !== null && _b !== void 0 ? _b : [],
115
+ rateLimits: (_d = workflow.onFailure.rate_limits) !== null && _d !== void 0 ? _d : [],
109
116
  workerLabels: {}, // no worker labels for on failure steps
110
117
  },
111
118
  ],
@@ -26,7 +26,7 @@ const workflow = {
26
26
  },
27
27
  concurrency: {
28
28
  name: 'user-concurrency',
29
- key: (ctx) => ctx.workflowInput().group,
29
+ expression: 'input.group',
30
30
  maxRuns: 2,
31
31
  limitStrategy: workflow_1.ConcurrencyLimitStrategy.GROUP_ROUND_ROBIN,
32
32
  },
@@ -0,0 +1,62 @@
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 workflow_1 = require("../../../workflow");
17
+ const hatchet = sdk_1.default.init();
18
+ const sleep = (ms) => new Promise((resolve) => {
19
+ setTimeout(resolve, ms);
20
+ });
21
+ const workflow = {
22
+ id: 'concurrency-example-rr',
23
+ description: 'test',
24
+ on: {
25
+ event: 'concurrency:create',
26
+ },
27
+ concurrency: {
28
+ name: 'user-concurrency',
29
+ // NOTE: it is recommended to use expression unless you specifically need to use a custom key function
30
+ key: (ctx) => ctx.workflowInput().group,
31
+ maxRuns: 2,
32
+ limitStrategy: workflow_1.ConcurrencyLimitStrategy.GROUP_ROUND_ROBIN,
33
+ },
34
+ steps: [
35
+ {
36
+ name: 'step1',
37
+ run: (ctx) => __awaiter(void 0, void 0, void 0, function* () {
38
+ const { data } = ctx.workflowInput();
39
+ const { signal } = ctx.controller;
40
+ if (signal.aborted)
41
+ throw new Error('step1 was aborted');
42
+ console.log('starting step1 and waiting 5 seconds...', data);
43
+ yield sleep(2000);
44
+ if (signal.aborted)
45
+ throw new Error('step1 was aborted');
46
+ // NOTE: the AbortController signal can be passed to many http libraries to cancel active requests
47
+ // fetch(url, { signal })
48
+ // axios.get(url, { signal })
49
+ console.log('executed step1!');
50
+ return { step1: `step1 results for ${data}!` };
51
+ }),
52
+ },
53
+ ],
54
+ };
55
+ function main() {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ const worker = yield hatchet.worker('example-worker');
58
+ yield worker.registerWorkflow(workflow);
59
+ worker.start();
60
+ });
61
+ }
62
+ main();
@@ -29,7 +29,9 @@ const parentWorkflow = {
29
29
  timeout: '10s',
30
30
  run: (ctx) => __awaiter(void 0, void 0, void 0, function* () {
31
31
  ctx.putStream('spawning children');
32
- const promises = Array.from({ length: 7 }, (_, i) => ctx.spawnWorkflow('child-workflow', { input: `child-input-${i}` }).result());
32
+ const promises = Array.from({ length: 7 }, (_, i) => ctx
33
+ .spawnWorkflow('child-workflow', { input: `child-input-${i}` }, { additionalMetadata: { childKey: 'childValue' } })
34
+ .result());
33
35
  const results = yield Promise.all(promises);
34
36
  console.log('spawned workflow results:', results);
35
37
  console.log('number of spawned workflows:', results.length);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "0.12.4",
3
+ "version": "0.13.0",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -46,7 +46,7 @@
46
46
  "worker:dag": "npm run exec -- ./src/examples/dag-worker.ts",
47
47
  "worker:concurrency": "npm run exec -- ./src/examples/concurrency/cancel-in-progress/concurrency-worker.ts",
48
48
  "event:concurrency": "npm run exec -- ./src/examples/concurrency/cancel-in-progress/concurrency-event.ts",
49
- "worker:concurrency:rr": "npm run exec -- ./src/examples/concurrency/group-round-robin/concurrency-worker.ts",
49
+ "worker:concurrency:rr": "npm run exec -- ./src/examples/concurrency/group-round-robin/concurrency-worker-expression.ts",
50
50
  "event:concurrency:rr": "npm run exec -- ./src/examples/concurrency/group-round-robin/concurrency-event.ts",
51
51
  "worker:playground": "npm run exec -- ./src/examples/playground.ts",
52
52
  "worker:retries": "npm run exec -- ./src/examples/retries-worker.ts",
@@ -92,6 +92,7 @@
92
92
  "typescript": "^5.3.3"
93
93
  },
94
94
  "dependencies": {
95
+ "@bufbuild/protobuf": "^2.1.0",
95
96
  "@types/qs": "^6.9.11",
96
97
  "abort-controller-x": "^0.4.3",
97
98
  "axios": "^1.6.7",