@hatchet-dev/typescript-sdk 1.7.1 → 1.8.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "1.7.1",
3
+ "version": "1.8.1",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -94,8 +94,10 @@ class WorkflowRunRef {
94
94
  _f = false;
95
95
  const event = _c;
96
96
  if (event.eventType === dispatcher_1.WorkflowRunEventType.WORKFLOW_RUN_EVENT_TYPE_FINISHED) {
97
- if (event.results.some((r) => !!r.error)) {
98
- reject(event.results);
97
+ if (event.results.some((r) => r.error !== undefined)) {
98
+ // HACK: this might replace intentional empty errors but this is the more common case
99
+ const errors = event.results.map((r) => r.error !== '' ? r.error : 'task was cancelled');
100
+ reject(errors);
99
101
  return;
100
102
  }
101
103
  if (event.results.length === 0) {
@@ -31,6 +31,7 @@ const transformer_1 = require("../../conditions/transformer");
31
31
  const step_1 = require("../../../step");
32
32
  const apply_namespace_1 = require("../../../util/apply-namespace");
33
33
  const context_1 = require("./context");
34
+ const parent_run_context_vars_1 = require("../../parent-run-context-vars");
34
35
  class V1Worker {
35
36
  constructor(client, options) {
36
37
  this.workflow_registry = [];
@@ -253,7 +254,12 @@ class V1Worker {
253
254
  catch (e) {
254
255
  throw new hatchet_error_1.default(`Could not register workflow: ${e.message}`);
255
256
  }
256
- this.registerActionsV1(workflow);
257
+ if (!durable) {
258
+ this.registerActionsV1(workflow);
259
+ }
260
+ else {
261
+ this.registerDurableActionsV1(workflow);
262
+ }
257
263
  });
258
264
  }
259
265
  registerWorkflow(initWorkflow) {
@@ -356,6 +362,12 @@ class V1Worker {
356
362
  return;
357
363
  }
358
364
  const run = () => __awaiter(this, void 0, void 0, function* () {
365
+ parent_run_context_vars_1.parentRunContextManager.setContext({
366
+ parentId: action.workflowRunId,
367
+ parentRunId: action.stepRunId,
368
+ childIndex: 0,
369
+ desiredWorkerId: this.workerId || '',
370
+ });
359
371
  return step(context);
360
372
  });
361
373
  const success = (result) => __awaiter(this, void 0, void 0, function* () {
@@ -60,7 +60,6 @@ class Worker {
60
60
  this.durable = new worker_internal_1.V1Worker(this._v1, opts);
61
61
  yield this.durable.registerWorkflowV1(wf, true);
62
62
  }
63
- this.durable.registerDurableActionsV1(wf.definition);
64
63
  }
65
64
  }
66
65
  else {
@@ -30,6 +30,16 @@ export type RunOpts = {
30
30
  * values: Priority.LOW, Priority.MEDIUM, Priority.HIGH (1, 2, or 3 )
31
31
  */
32
32
  priority?: Priority;
33
+ /**
34
+ * (optional) if the task run should be run on the same worker.
35
+ * only used if spawned from within a parent task.
36
+ */
37
+ sticky?: boolean;
38
+ /**
39
+ * (optional) the child key for the workflow run.
40
+ * only used if spawned from within a parent task.
41
+ */
42
+ childKey?: string;
33
43
  };
34
44
  /**
35
45
  * Helper type to safely extract output types from task results
package/v1/declaration.js CHANGED
@@ -13,6 +13,7 @@ exports.TaskWorkflowDeclaration = exports.WorkflowDeclaration = exports.BaseWork
13
13
  exports.CreateTaskWorkflow = CreateTaskWorkflow;
14
14
  exports.CreateWorkflow = CreateWorkflow;
15
15
  exports.CreateDurableTaskWorkflow = CreateDurableTaskWorkflow;
16
+ const parent_run_context_vars_1 = require("./parent-run-context-vars");
16
17
  const UNBOUND_ERR = new Error('workflow unbound to hatchet client, hint: use client.run instead');
17
18
  // eslint-disable-next-line no-shadow
18
19
  var Priority;
@@ -41,15 +42,25 @@ class BaseWorkflowDeclaration {
41
42
  if (!this.client) {
42
43
  throw UNBOUND_ERR;
43
44
  }
45
+ // set the parent run context
46
+ const parentRunContext = parent_run_context_vars_1.parentRunContextManager.getContext();
47
+ parent_run_context_vars_1.parentRunContextManager.incrementChildIndex(Array.isArray(input) ? input.length : 1);
48
+ if (!parentRunContext && ((options === null || options === void 0 ? void 0 : options.childKey) || (options === null || options === void 0 ? void 0 : options.sticky))) {
49
+ this.client.admin.logger.warn('ignoring childKey or sticky because run is not being spawned from a parent task');
50
+ }
51
+ const runOpts = Object.assign(Object.assign({}, options), { parentId: parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.parentId, parentStepRunId: parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.parentRunId, childIndex: parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.childIndex, sticky: (options === null || options === void 0 ? void 0 : options.sticky) ? parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.desiredWorkerId : undefined, childKey: options === null || options === void 0 ? void 0 : options.childKey });
44
52
  if (Array.isArray(input)) {
45
53
  let resp = [];
46
54
  for (let i = 0; i < input.length; i += 500) {
47
55
  const batch = input.slice(i, i + 500);
48
- const batchResp = yield this.client.admin.runWorkflows(batch.map((inp) => ({
49
- workflowName: this.definition.name,
50
- input: inp,
51
- options,
52
- })));
56
+ const batchResp = yield this.client.admin.runWorkflows(batch.map((inp) => {
57
+ var _a;
58
+ return ({
59
+ workflowName: this.definition.name,
60
+ input: inp,
61
+ options: Object.assign(Object.assign({}, runOpts), { childIndex: ((_a = runOpts.childIndex) !== null && _a !== void 0 ? _a : 0) + i }),
62
+ });
63
+ }));
53
64
  resp = resp.concat(batchResp);
54
65
  }
55
66
  const res = [];
@@ -67,7 +78,7 @@ class BaseWorkflowDeclaration {
67
78
  });
68
79
  return res;
69
80
  }
70
- const res = yield this.client.admin.runWorkflow(this.definition.name, input, options);
81
+ const res = yield this.client.admin.runWorkflow(this.definition.name, input, runOpts);
71
82
  if (_standaloneTaskName) {
72
83
  res._standaloneTaskName = _standaloneTaskName;
73
84
  }
@@ -27,7 +27,7 @@ exports.parent = hatchet_client_1.hatchet.task({
27
27
  const n = input.N;
28
28
  const promises = [];
29
29
  for (let i = 0; i < n; i++) {
30
- promises.push(ctx.runChild(exports.child, { N: i }));
30
+ promises.push(exports.child.run({ N: i }));
31
31
  }
32
32
  const childRes = yield Promise.all(promises);
33
33
  const sum = childRes.reduce((acc, curr) => acc + curr.Value, 0);
@@ -13,10 +13,11 @@ exports.extra = extra;
13
13
  /* eslint-disable no-console */
14
14
  const hatchet_client_1 = require("../hatchet-client");
15
15
  const workflow_1 = require("./workflow");
16
+ const workflow_with_child_1 = require("./workflow-with-child");
16
17
  function main() {
17
18
  return __awaiter(this, void 0, void 0, function* () {
18
19
  // > Running a Task
19
- const res = yield workflow_1.simple.run({
20
+ const res = yield workflow_with_child_1.parent.run({
20
21
  Message: 'HeLlO WoRlD',
21
22
  }, {
22
23
  additionalMetadata: {
@@ -42,11 +43,11 @@ function extra() {
42
43
  console.log(results[1].TransformedMessage);
43
44
  // !!
44
45
  // > Spawning Tasks from within a Task
45
- const parent = hatchet_client_1.hatchet.task({
46
+ const parentTask = hatchet_client_1.hatchet.task({
46
47
  name: 'parent',
47
48
  fn: (input, ctx) => __awaiter(this, void 0, void 0, function* () {
48
- // Simply call ctx.runChild with the task you want to run
49
- const child = yield ctx.runChild(workflow_1.simple, {
49
+ // Simply the task and it will be spawned from the parent task
50
+ const child = yield workflow_1.simple.run({
50
51
  Message: 'HeLlO WoRlD',
51
52
  });
52
53
  return {
@@ -58,5 +59,9 @@ function extra() {
58
59
  });
59
60
  }
60
61
  if (require.main === module) {
61
- main();
62
+ main()
63
+ .catch(console.error)
64
+ .finally(() => {
65
+ process.exit(0);
66
+ });
62
67
  }
@@ -21,7 +21,7 @@ exports.sticky = hatchet_client_1.hatchet.task({
21
21
  sticky: workflows_1.StickyStrategy.SOFT,
22
22
  fn: (_, ctx) => __awaiter(void 0, void 0, void 0, function* () {
23
23
  // specify a child workflow to run on the same worker
24
- const result = yield ctx.runChild(workflow_1.child, {
24
+ const result = yield workflow_1.child.run({
25
25
  N: 1,
26
26
  }, { sticky: true });
27
27
  return {
@@ -0,0 +1,15 @@
1
+ export interface ParentRunContext {
2
+ parentId: string;
3
+ parentRunId: string;
4
+ desiredWorkerId: string;
5
+ childIndex?: number;
6
+ }
7
+ export declare class ParentRunContextManager {
8
+ private storage;
9
+ constructor();
10
+ setContext(opts: ParentRunContext): void;
11
+ setParentRunIdAndIncrementChildIndex(opts: ParentRunContext): void;
12
+ incrementChildIndex(n: number): void;
13
+ getContext(): ParentRunContext | undefined;
14
+ }
15
+ export declare const parentRunContextManager: ParentRunContextManager;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parentRunContextManager = exports.ParentRunContextManager = void 0;
4
+ const async_hooks_1 = require("async_hooks");
5
+ class ParentRunContextManager {
6
+ constructor() {
7
+ this.storage = new async_hooks_1.AsyncLocalStorage();
8
+ }
9
+ setContext(opts) {
10
+ this.storage.enterWith(Object.assign({}, opts));
11
+ }
12
+ setParentRunIdAndIncrementChildIndex(opts) {
13
+ var _a;
14
+ const parentRunContext = this.getContext();
15
+ if (parentRunContext) {
16
+ parentRunContext.parentId = opts.parentId;
17
+ parentRunContext.childIndex = ((_a = parentRunContext.childIndex) !== null && _a !== void 0 ? _a : 0) + 1;
18
+ this.setContext(parentRunContext);
19
+ }
20
+ }
21
+ incrementChildIndex(n) {
22
+ var _a;
23
+ const parentRunContext = this.getContext();
24
+ if (parentRunContext) {
25
+ parentRunContext.childIndex = ((_a = parentRunContext.childIndex) !== null && _a !== void 0 ? _a : 0) + n;
26
+ this.setContext(parentRunContext);
27
+ }
28
+ }
29
+ getContext() {
30
+ return this.storage.getStore();
31
+ }
32
+ }
33
+ exports.ParentRunContextManager = ParentRunContextManager;
34
+ // Export a default instance for backward compatibility and convenience
35
+ exports.parentRunContextManager = new ParentRunContextManager();
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "1.7.1";
1
+ export declare const HATCHET_VERSION = "1.8.1";
package/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HATCHET_VERSION = void 0;
4
- exports.HATCHET_VERSION = '1.7.1';
4
+ exports.HATCHET_VERSION = '1.8.1';