@hatchet-dev/typescript-sdk 1.19.2-alpha.0 → 1.19.2-alpha.2
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 +1 -1
- package/v1/client/worker/context.d.ts +2 -0
- package/v1/client/worker/context.js +18 -8
- package/v1/declaration.js +11 -14
- package/v1/parent-run-context-vars.d.ts +1 -1
- package/v1/parent-run-context-vars.js +4 -3
- package/version.d.ts +1 -1
- package/version.js +1 -1
package/package.json
CHANGED
|
@@ -81,8 +81,10 @@ export declare class Context<T, K = {}> {
|
|
|
81
81
|
worker: ContextWorker;
|
|
82
82
|
overridesData: Record<string, any>;
|
|
83
83
|
_logger: Logger;
|
|
84
|
+
/** @deprecated — kept for backward compat; prefer {@link nextChildIndex}. */
|
|
84
85
|
spawnIndex: number;
|
|
85
86
|
streamIndex: number;
|
|
87
|
+
protected nextChildIndex(n?: number): number;
|
|
86
88
|
constructor(action: Action, v1: HatchetClient, worker: InternalWorker);
|
|
87
89
|
get abortController(): AbortController;
|
|
88
90
|
get cancelled(): boolean;
|
|
@@ -30,6 +30,7 @@ const transformer_1 = require("../../conditions/transformer");
|
|
|
30
30
|
const condition_1 = require("../../../protoc/v1/shared/condition");
|
|
31
31
|
const apply_namespace_1 = require("../../../util/apply-namespace");
|
|
32
32
|
const abort_error_1 = require("../../../util/abort-error");
|
|
33
|
+
const parent_run_context_vars_1 = require("../../parent-run-context-vars");
|
|
33
34
|
const crypto_1 = require("crypto");
|
|
34
35
|
const duration_1 = require("../duration");
|
|
35
36
|
const engine_version_1 = require("./engine-version");
|
|
@@ -74,10 +75,19 @@ class ContextWorker {
|
|
|
74
75
|
}
|
|
75
76
|
exports.ContextWorker = ContextWorker;
|
|
76
77
|
class Context {
|
|
78
|
+
nextChildIndex(n = 1) {
|
|
79
|
+
var _a;
|
|
80
|
+
const ctx = parent_run_context_vars_1.parentRunContextManager.getContext();
|
|
81
|
+
const idx = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.childIndex) !== null && _a !== void 0 ? _a : this.spawnIndex;
|
|
82
|
+
parent_run_context_vars_1.parentRunContextManager.incrementChildIndex(n);
|
|
83
|
+
this.spawnIndex = idx + n;
|
|
84
|
+
return idx;
|
|
85
|
+
}
|
|
77
86
|
constructor(action, v1, worker) {
|
|
78
87
|
// @deprecated use ctx.abortController instead
|
|
79
88
|
this.controller = new AbortController();
|
|
80
89
|
this.overridesData = {};
|
|
90
|
+
/** @deprecated — kept for backward compat; prefer {@link nextChildIndex}. */
|
|
81
91
|
this.spawnIndex = 0;
|
|
82
92
|
this.streamIndex = 0;
|
|
83
93
|
try {
|
|
@@ -379,8 +389,8 @@ class Context {
|
|
|
379
389
|
throw new hatchet_error_1.default(`Cannot run with sticky: workflow ${workflowName} is not registered on the worker`);
|
|
380
390
|
}
|
|
381
391
|
const { workflowRunId, taskRunExternalId } = this.action;
|
|
382
|
-
const
|
|
383
|
-
this.
|
|
392
|
+
const childIndex = this.nextChildIndex();
|
|
393
|
+
const finalOpts = Object.assign(Object.assign({}, opts), { parentId: workflowRunId, parentTaskRunExternalId: taskRunExternalId, childIndex, childKey: options === null || options === void 0 ? void 0 : options.key, desiredWorkerId: sticky ? this.worker.id() : undefined, _standaloneTaskName: workflow instanceof declaration_1.TaskWorkflowDeclaration ? workflow._standalone_task_name : undefined });
|
|
384
394
|
return { workflowName, opts: finalOpts };
|
|
385
395
|
}
|
|
386
396
|
spawn(workflow, input, options) {
|
|
@@ -561,12 +571,12 @@ class Context {
|
|
|
561
571
|
// `signal` must never be sent over the wire.
|
|
562
572
|
const optsWithoutSignal = Object.assign({}, opts);
|
|
563
573
|
delete optsWithoutSignal.signal;
|
|
574
|
+
const childIndex = this.nextChildIndex();
|
|
564
575
|
const resp = {
|
|
565
576
|
workflowName: name,
|
|
566
577
|
input,
|
|
567
|
-
options: Object.assign(Object.assign({}, optsWithoutSignal), { parentId: workflowRunId, parentTaskRunExternalId: taskRunExternalId, childIndex
|
|
578
|
+
options: Object.assign(Object.assign({}, optsWithoutSignal), { parentId: workflowRunId, parentTaskRunExternalId: taskRunExternalId, childIndex, desiredWorkerId: sticky ? this.worker.id() : undefined }),
|
|
568
579
|
};
|
|
569
|
-
this.spawnIndex += 1;
|
|
570
580
|
return resp;
|
|
571
581
|
});
|
|
572
582
|
try {
|
|
@@ -614,8 +624,8 @@ class Context {
|
|
|
614
624
|
throw new hatchet_error_1.default(`cannot run with sticky: workflow ${name} is not registered on the worker`);
|
|
615
625
|
}
|
|
616
626
|
try {
|
|
617
|
-
const
|
|
618
|
-
this.
|
|
627
|
+
const childIndex = this.nextChildIndex();
|
|
628
|
+
const resp = yield this.v1.admin.runWorkflow(name, input, Object.assign({ parentId: workflowRunId, parentTaskRunExternalId: taskRunExternalId, childIndex, desiredWorkerId: sticky ? this.worker.id() : undefined }, opts));
|
|
619
629
|
if (workflow instanceof declaration_1.TaskWorkflowDeclaration) {
|
|
620
630
|
resp._standaloneTaskName = workflow._standalone_task_name;
|
|
621
631
|
}
|
|
@@ -796,12 +806,13 @@ class DurableContext extends Context {
|
|
|
796
806
|
workflowName = workflow.name;
|
|
797
807
|
}
|
|
798
808
|
workflowName = (0, apply_namespace_1.applyNamespace)(workflowName, this.v1.config.namespace).toLowerCase();
|
|
809
|
+
const childIndex = this.nextChildIndex();
|
|
799
810
|
const triggerOpts = {
|
|
800
811
|
name: workflowName,
|
|
801
812
|
input: JSON.stringify(input || {}),
|
|
802
813
|
parentId: this.action.workflowRunId,
|
|
803
814
|
parentTaskRunExternalId: this.action.taskRunExternalId,
|
|
804
|
-
childIndex
|
|
815
|
+
childIndex,
|
|
805
816
|
childKey: options === null || options === void 0 ? void 0 : options.key,
|
|
806
817
|
additionalMetadata: (options === null || options === void 0 ? void 0 : options.additionalMetadata)
|
|
807
818
|
? JSON.stringify(options.additionalMetadata)
|
|
@@ -810,7 +821,6 @@ class DurableContext extends Context {
|
|
|
810
821
|
priority: options === null || options === void 0 ? void 0 : options.priority,
|
|
811
822
|
desiredWorkerLabels: {},
|
|
812
823
|
};
|
|
813
|
-
this.spawnIndex += 1;
|
|
814
824
|
return { workflowName, triggerOpts };
|
|
815
825
|
}
|
|
816
826
|
/**
|
package/v1/declaration.js
CHANGED
|
@@ -64,11 +64,6 @@ class BaseWorkflowDeclaration {
|
|
|
64
64
|
throw UNBOUND_ERR;
|
|
65
65
|
}
|
|
66
66
|
const parentRunContext = parent_run_context_vars_1.parentRunContextManager.getContext();
|
|
67
|
-
// Snapshot the child index BEFORE incrementing — getContext() returns a mutable
|
|
68
|
-
// reference, so any later increment would corrupt this value.
|
|
69
|
-
const childIndex = parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.childIndex;
|
|
70
|
-
const inputCount = Array.isArray(input) ? input.length : 1;
|
|
71
|
-
parent_run_context_vars_1.parentRunContextManager.incrementChildIndex(inputCount);
|
|
72
67
|
if (!parentRunContext && ((options === null || options === void 0 ? void 0 : options.childKey) || (options === null || options === void 0 ? void 0 : options.sticky))) {
|
|
73
68
|
this.client.admin.logger.warn('ignoring childKey or sticky because run is not being spawned from a parent task');
|
|
74
69
|
}
|
|
@@ -82,19 +77,21 @@ class BaseWorkflowDeclaration {
|
|
|
82
77
|
: undefined,
|
|
83
78
|
warn: (message) => this.client.admin.logger.warn(message),
|
|
84
79
|
});
|
|
85
|
-
|
|
80
|
+
// Snapshot childIndex before incrementing — shared with Context.spawnIndex
|
|
81
|
+
// via parentRunContextManager so both spawning APIs produce unique indices.
|
|
82
|
+
const baseChildIndex = parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.childIndex;
|
|
83
|
+
const inputCount = Array.isArray(input) ? input.length : 1;
|
|
84
|
+
parent_run_context_vars_1.parentRunContextManager.incrementChildIndex(inputCount);
|
|
85
|
+
const runOpts = Object.assign(Object.assign({}, (options !== null && options !== void 0 ? options : {})), { parentId: parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.parentId, parentTaskRunExternalId: parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.parentTaskRunExternalId, childIndex: baseChildIndex, 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 });
|
|
86
86
|
if (Array.isArray(input)) {
|
|
87
87
|
let resp = [];
|
|
88
88
|
for (let i = 0; i < input.length; i += 500) {
|
|
89
89
|
const batch = input.slice(i, i + 500);
|
|
90
|
-
const batchResp = yield this.client.admin.runWorkflows(batch.map((inp, batchIdx) => {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
options: Object.assign(Object.assign({}, runOpts), { childIndex: ((_a = runOpts.childIndex) !== null && _a !== void 0 ? _a : 0) + i + batchIdx }),
|
|
96
|
-
});
|
|
97
|
-
}));
|
|
90
|
+
const batchResp = yield this.client.admin.runWorkflows(batch.map((inp, batchIdx) => ({
|
|
91
|
+
workflowName: this.definition.name,
|
|
92
|
+
input: inp,
|
|
93
|
+
options: Object.assign(Object.assign({}, runOpts), { childIndex: (baseChildIndex !== null && baseChildIndex !== void 0 ? baseChildIndex : 0) + i + batchIdx }),
|
|
94
|
+
})));
|
|
98
95
|
resp = resp.concat(batchResp);
|
|
99
96
|
}
|
|
100
97
|
const res = [];
|
|
@@ -16,15 +16,16 @@ class ParentRunContextManager {
|
|
|
16
16
|
var _a;
|
|
17
17
|
const parentRunContext = this.getContext();
|
|
18
18
|
if (parentRunContext) {
|
|
19
|
-
|
|
19
|
+
parentRunContext.parentId = opts.parentId;
|
|
20
|
+
parentRunContext.parentTaskRunExternalId = opts.parentTaskRunExternalId;
|
|
21
|
+
parentRunContext.childIndex = ((_a = parentRunContext.childIndex) !== null && _a !== void 0 ? _a : 0) + 1;
|
|
22
|
+
this.setContext(parentRunContext);
|
|
20
23
|
}
|
|
21
24
|
}
|
|
22
25
|
incrementChildIndex(n) {
|
|
23
26
|
var _a;
|
|
24
27
|
const parentRunContext = this.getContext();
|
|
25
28
|
if (parentRunContext) {
|
|
26
|
-
// Build a fresh object to avoid mutating the live store reference, which
|
|
27
|
-
// would corrupt any values snapshotted before this call.
|
|
28
29
|
this.setContext(Object.assign(Object.assign({}, parentRunContext), { childIndex: ((_a = parentRunContext.childIndex) !== null && _a !== void 0 ? _a : 0) + n }));
|
|
29
30
|
}
|
|
30
31
|
}
|
package/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const HATCHET_VERSION = "1.19.2-alpha.
|
|
1
|
+
export declare const HATCHET_VERSION = "1.19.2-alpha.2";
|
package/version.js
CHANGED