@hatchet-dev/typescript-sdk 1.16.0 → 1.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/README.md +14 -2
- package/clients/admin/admin-client.d.ts +2 -2
- package/clients/dispatcher/action-listener.d.ts +3 -6
- package/clients/dispatcher/action-listener.js +12 -17
- package/clients/listeners/durable-listener/durable-listener-client.d.ts +115 -15
- package/clients/listeners/durable-listener/durable-listener-client.js +769 -19
- package/clients/listeners/durable-listener/pooled-durable-listener-client.js +1 -14
- package/clients/listeners/run-listener/pooled-child-listener-client.js +1 -14
- package/clients/rest/generated/Api.d.ts +5 -1
- package/clients/rest/generated/data-contracts.d.ts +16 -2
- package/clients/rest/generated/data-contracts.js +7 -3
- package/legacy/examples/affinity-workers.js +2 -2
- package/legacy/legacy-client.js +1 -1
- package/legacy/step.d.ts +2 -2
- package/legacy/step.js +3 -2
- package/legacy/workflow.d.ts +25 -25
- package/package.json +1 -1
- package/protoc/dispatcher/dispatcher.d.ts +20 -0
- package/protoc/dispatcher/dispatcher.js +136 -2
- package/protoc/v1/dispatcher.d.ts +168 -0
- package/protoc/v1/dispatcher.js +1920 -1
- package/protoc/v1/shared/trigger.d.ts +89 -0
- package/protoc/v1/shared/trigger.js +493 -0
- package/protoc/v1/workflows.d.ts +34 -34
- package/protoc/v1/workflows.js +252 -200
- package/protoc/workflows/workflows.d.ts +2 -75
- package/protoc/workflows/workflows.js +16 -491
- package/util/abort-error.d.ts +10 -0
- package/util/abort-error.js +15 -0
- package/util/errors/eviction-not-supported-error.d.ts +5 -0
- package/util/errors/eviction-not-supported-error.js +18 -0
- package/util/errors/non-determinism-error.d.ts +7 -0
- package/util/errors/non-determinism-error.js +21 -0
- package/util/errors/task-run-terminated-error.d.ts +6 -0
- package/util/errors/task-run-terminated-error.js +15 -0
- package/util/hatchet-promise/hatchet-promise.d.ts +2 -1
- package/util/hatchet-promise/hatchet-promise.js +10 -1
- package/util/sleep.d.ts +3 -2
- package/util/sleep.js +6 -4
- package/v1/client/admin.d.ts +2 -2
- package/v1/client/client.js +1 -1
- package/v1/client/duration.d.ts +11 -1
- package/v1/client/duration.js +44 -0
- package/v1/client/features/runs.d.ts +16 -3
- package/v1/client/features/runs.js +38 -3
- package/v1/client/worker/context.d.ts +101 -6
- package/v1/client/worker/context.js +247 -21
- package/v1/client/worker/deprecated/index.d.ts +1 -1
- package/v1/client/worker/deprecated/index.js +2 -1
- package/v1/client/worker/deprecated/legacy-worker.d.ts +5 -0
- package/v1/client/worker/deprecated/legacy-worker.js +32 -23
- package/v1/client/worker/deprecated/pre-eviction.d.ts +12 -0
- package/v1/client/worker/deprecated/pre-eviction.js +37 -0
- package/v1/client/worker/engine-version.d.ts +5 -0
- package/v1/client/worker/engine-version.js +14 -0
- package/v1/client/worker/eviction/eviction-cache.d.ts +33 -0
- package/v1/client/worker/eviction/eviction-cache.js +139 -0
- package/v1/client/worker/eviction/eviction-manager.d.ts +42 -0
- package/v1/client/worker/eviction/eviction-manager.js +132 -0
- package/v1/client/worker/eviction/eviction-policy.d.ts +19 -0
- package/v1/client/worker/eviction/eviction-policy.js +8 -0
- package/v1/client/worker/eviction/index.d.ts +3 -0
- package/v1/client/worker/eviction/index.js +11 -0
- package/v1/client/worker/worker-internal.d.ts +23 -4
- package/v1/client/worker/worker-internal.js +177 -138
- package/v1/client/worker/worker.d.ts +1 -0
- package/v1/client/worker/worker.js +34 -1
- package/v1/conditions/sleep-condition.js +2 -1
- package/v1/conditions/transformer.js +2 -1
- package/v1/declaration.d.ts +5 -3
- package/v1/declaration.js +8 -0
- package/v1/examples/__e2e__/harness.d.ts +5 -0
- package/v1/examples/__e2e__/harness.js +13 -0
- package/v1/examples/concurrency_workflow_level/workflow.d.ts +1 -1
- package/v1/examples/concurrency_workflow_level/workflow.js +1 -1
- package/v1/examples/durable/workflow.d.ts +57 -0
- package/v1/examples/durable/workflow.js +162 -7
- package/v1/examples/durable-event/workflow.js +2 -7
- package/v1/examples/durable_event/workflow.d.ts +1 -0
- package/v1/examples/durable_event/workflow.js +4 -9
- package/v1/examples/durable_eviction/capacity-worker.d.ts +1 -0
- package/v1/examples/durable_eviction/capacity-worker.js +31 -0
- package/v1/examples/durable_eviction/worker.d.ts +1 -0
- package/v1/examples/durable_eviction/worker.js +34 -0
- package/v1/examples/durable_eviction/workflow.d.ts +44 -0
- package/v1/examples/durable_eviction/workflow.js +129 -0
- package/v1/examples/e2e-worker.js +42 -19
- package/v1/index.d.ts +5 -0
- package/v1/index.js +10 -0
- package/v1/parent-run-context-vars.d.ts +6 -0
- package/v1/task.d.ts +10 -2
- package/v1/task.js +2 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.EvictionNotSupportedError = void 0;
|
|
7
|
+
const v1_1 = require("../../v1");
|
|
8
|
+
const hatchet_error_1 = __importDefault(require("./hatchet-error"));
|
|
9
|
+
class EvictionNotSupportedError extends hatchet_error_1.default {
|
|
10
|
+
constructor(engineVersion) {
|
|
11
|
+
const versionInfo = engineVersion ? ` (engine ${engineVersion})` : '';
|
|
12
|
+
super(`Durable eviction is not supported by the connected engine${versionInfo}. ` +
|
|
13
|
+
`Please upgrade your Hatchet engine to ${v1_1.MinEngineVersion.DURABLE_EVICTION} or later.`);
|
|
14
|
+
this.name = 'EvictionNotSupportedError';
|
|
15
|
+
this.engineVersion = engineVersion;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.EvictionNotSupportedError = EvictionNotSupportedError;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import HatchetError from './hatchet-error';
|
|
2
|
+
export declare class NonDeterminismError extends HatchetError {
|
|
3
|
+
taskExternalId: string;
|
|
4
|
+
invocationCount: number;
|
|
5
|
+
nodeId: number;
|
|
6
|
+
constructor(taskExternalId: string, invocationCount: number, nodeId: number, message: string);
|
|
7
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.NonDeterminismError = void 0;
|
|
7
|
+
const hatchet_error_1 = __importDefault(require("./hatchet-error"));
|
|
8
|
+
class NonDeterminismError extends hatchet_error_1.default {
|
|
9
|
+
constructor(taskExternalId, invocationCount, nodeId, message) {
|
|
10
|
+
const detail = message
|
|
11
|
+
? message
|
|
12
|
+
: `Non-determinism detected in task ${taskExternalId} on invocation ${invocationCount} at node ${nodeId}`;
|
|
13
|
+
super(`${detail}\n` +
|
|
14
|
+
`Check out our documentation for more details on expectations of durable tasks: https://docs.hatchet.run/v1/patterns/mixing-patterns`);
|
|
15
|
+
this.name = 'NonDeterminismError';
|
|
16
|
+
this.taskExternalId = taskExternalId;
|
|
17
|
+
this.invocationCount = invocationCount;
|
|
18
|
+
this.nodeId = nodeId;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.NonDeterminismError = NonDeterminismError;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type TaskRunTerminationReason = 'cancelled' | 'evicted';
|
|
2
|
+
export declare class TaskRunTerminatedError extends Error {
|
|
3
|
+
readonly reason: TaskRunTerminationReason;
|
|
4
|
+
constructor(reason: TaskRunTerminationReason, message?: string);
|
|
5
|
+
}
|
|
6
|
+
export declare function isTaskRunTerminatedError(err: unknown): err is TaskRunTerminatedError;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TaskRunTerminatedError = void 0;
|
|
4
|
+
exports.isTaskRunTerminatedError = isTaskRunTerminatedError;
|
|
5
|
+
class TaskRunTerminatedError extends Error {
|
|
6
|
+
constructor(reason, message) {
|
|
7
|
+
super(message !== null && message !== void 0 ? message : reason);
|
|
8
|
+
this.name = 'TaskRunTerminatedError';
|
|
9
|
+
this.reason = reason;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.TaskRunTerminatedError = TaskRunTerminatedError;
|
|
13
|
+
function isTaskRunTerminatedError(err) {
|
|
14
|
+
return err instanceof TaskRunTerminatedError;
|
|
15
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/** Canonical reasons when cancelling a HatchetPromise (e.g. worker shutdown). */
|
|
2
2
|
export declare enum CancellationReason {
|
|
3
|
-
CANCELLED_BY_WORKER = "Cancelled by worker"
|
|
3
|
+
CANCELLED_BY_WORKER = "Cancelled by worker",
|
|
4
|
+
EVICTED_BY_WORKER = "Evicted by worker"
|
|
4
5
|
}
|
|
5
6
|
declare class HatchetPromise<T> {
|
|
6
7
|
cancel: (reason?: CancellationReason) => void;
|
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CancellationReason = void 0;
|
|
4
|
+
const task_run_terminated_error_1 = require("../errors/task-run-terminated-error");
|
|
4
5
|
/** Canonical reasons when cancelling a HatchetPromise (e.g. worker shutdown). */
|
|
5
6
|
var CancellationReason;
|
|
6
7
|
(function (CancellationReason) {
|
|
7
8
|
CancellationReason["CANCELLED_BY_WORKER"] = "Cancelled by worker";
|
|
9
|
+
CancellationReason["EVICTED_BY_WORKER"] = "Evicted by worker";
|
|
8
10
|
})(CancellationReason || (exports.CancellationReason = CancellationReason = {}));
|
|
11
|
+
const reasonToTermination = {
|
|
12
|
+
[CancellationReason.CANCELLED_BY_WORKER]: 'cancelled',
|
|
13
|
+
[CancellationReason.EVICTED_BY_WORKER]: 'evicted',
|
|
14
|
+
};
|
|
9
15
|
class HatchetPromise {
|
|
10
16
|
constructor(promise) {
|
|
11
17
|
this.cancel = (_reason) => { };
|
|
12
18
|
this.inner = Promise.resolve(promise);
|
|
13
19
|
this.promise = new Promise((resolve, reject) => {
|
|
14
|
-
this.cancel =
|
|
20
|
+
this.cancel = (reason) => {
|
|
21
|
+
const termination = reason ? reasonToTermination[reason] : 'cancelled';
|
|
22
|
+
reject(new task_run_terminated_error_1.TaskRunTerminatedError(termination, reason));
|
|
23
|
+
};
|
|
15
24
|
this.inner.then(resolve).catch(reject);
|
|
16
25
|
});
|
|
17
26
|
}
|
package/util/sleep.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
import { Duration } from '../v1/client/duration';
|
|
1
2
|
/**
|
|
2
3
|
* Sleeps for a given number of milliseconds without blocking the event loop
|
|
3
4
|
*
|
|
4
5
|
* WARNING: This is not a durable sleep. It will not be honored if the worker is
|
|
5
6
|
* restarted or crashes.
|
|
6
7
|
*
|
|
7
|
-
* @param
|
|
8
|
+
* @param duration - The number of milliseconds to sleep, or a Duration (e.g. "5s", \{ seconds: 5 \})
|
|
8
9
|
* @param signal - Optional AbortSignal; if aborted, the promise rejects with Error('Cancelled').
|
|
9
10
|
* Use in task handlers so cancellation can interrupt long sleeps.
|
|
10
11
|
* @returns A promise that resolves after the given number of milliseconds (or rejects on abort)
|
|
11
12
|
*/
|
|
12
|
-
declare function sleep(
|
|
13
|
+
declare function sleep(duration: number | Duration, signal?: AbortSignal): Promise<void>;
|
|
13
14
|
export default sleep;
|
package/util/sleep.js
CHANGED
|
@@ -1,27 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const duration_1 = require("../v1/client/duration");
|
|
3
4
|
/**
|
|
4
5
|
* Sleeps for a given number of milliseconds without blocking the event loop
|
|
5
6
|
*
|
|
6
7
|
* WARNING: This is not a durable sleep. It will not be honored if the worker is
|
|
7
8
|
* restarted or crashes.
|
|
8
9
|
*
|
|
9
|
-
* @param
|
|
10
|
+
* @param duration - The number of milliseconds to sleep, or a Duration (e.g. "5s", \{ seconds: 5 \})
|
|
10
11
|
* @param signal - Optional AbortSignal; if aborted, the promise rejects with Error('Cancelled').
|
|
11
12
|
* Use in task handlers so cancellation can interrupt long sleeps.
|
|
12
13
|
* @returns A promise that resolves after the given number of milliseconds (or rejects on abort)
|
|
13
14
|
*/
|
|
14
|
-
function sleep(
|
|
15
|
+
function sleep(duration, signal) {
|
|
16
|
+
const timeout = typeof duration === 'number' ? duration : (0, duration_1.durationToMs)(duration);
|
|
15
17
|
if (!signal) {
|
|
16
18
|
return new Promise((resolve) => {
|
|
17
|
-
setTimeout(resolve,
|
|
19
|
+
setTimeout(resolve, timeout);
|
|
18
20
|
});
|
|
19
21
|
}
|
|
20
22
|
return new Promise((resolve, reject) => {
|
|
21
23
|
const timer = setTimeout(() => {
|
|
22
24
|
signal.removeEventListener('abort', onAbort);
|
|
23
25
|
resolve();
|
|
24
|
-
},
|
|
26
|
+
}, timeout);
|
|
25
27
|
const onAbort = () => {
|
|
26
28
|
clearTimeout(timer);
|
|
27
29
|
reject(new Error('Cancelled'));
|
package/v1/client/admin.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ClientConfig } from '../../clients/hatchet-client/client-config';
|
|
2
2
|
import WorkflowRunRef from '../../util/workflow-run-ref';
|
|
3
|
-
import { Priority, RateLimitDuration, RunsClient } from './..';
|
|
3
|
+
import { Priority, RateLimitDuration, RunsClient, WorkerLabelComparator } from './..';
|
|
4
4
|
import { RunListenerClient } from '../../clients/listeners/run-listener/child-listener-client';
|
|
5
5
|
import { Api } from '../../clients/rest/generated/Api';
|
|
6
|
-
import {
|
|
6
|
+
import { WorkflowServiceClient } from '../../protoc/workflows';
|
|
7
7
|
import { AdminServiceClient, CreateWorkflowVersionRequest } from '../../protoc/v1/workflows';
|
|
8
8
|
import { Logger } from '../../util/logger';
|
|
9
9
|
type DesiredWorkerLabelOpt = {
|
package/v1/client/client.js
CHANGED
|
@@ -304,7 +304,7 @@ class HatchetClient {
|
|
|
304
304
|
*/
|
|
305
305
|
get durableListener() {
|
|
306
306
|
if (!this._durableListener) {
|
|
307
|
-
this._durableListener = new durable_listener_client_1.DurableListenerClient(this._config, (0, grpc_helpers_1.channelFactory)(this._config, this._credentials), this._clientFactory
|
|
307
|
+
this._durableListener = new durable_listener_client_1.DurableListenerClient(this._config, (0, grpc_helpers_1.channelFactory)(this._config, this._credentials), this._clientFactory);
|
|
308
308
|
}
|
|
309
309
|
return this._durableListener;
|
|
310
310
|
}
|
package/v1/client/duration.d.ts
CHANGED
|
@@ -3,5 +3,15 @@ type MinutesDuration = `${number}m`;
|
|
|
3
3
|
type HoursDuration = `${number}h`;
|
|
4
4
|
type TwoUnitDurations = `${number}h${number}m` | `${number}h${number}s` | `${number}m${number}s`;
|
|
5
5
|
type ThreeUnitDurations = `${number}h${number}m${number}s`;
|
|
6
|
-
|
|
6
|
+
type DurationString = SecondsDuration | MinutesDuration | HoursDuration | TwoUnitDurations | ThreeUnitDurations;
|
|
7
|
+
export interface DurationObject {
|
|
8
|
+
hours?: number;
|
|
9
|
+
minutes?: number;
|
|
10
|
+
seconds?: number;
|
|
11
|
+
}
|
|
12
|
+
/** A number is treated as milliseconds. */
|
|
13
|
+
export type Duration = DurationString | DurationObject | number;
|
|
14
|
+
/** Normalizes a Duration to Go-style string format (e.g. "1h30m5s"). */
|
|
15
|
+
export declare function durationToString(d: Duration): string;
|
|
16
|
+
export declare function durationToMs(d: Duration): number;
|
|
7
17
|
export {};
|
package/v1/client/duration.js
CHANGED
|
@@ -1,2 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.durationToString = durationToString;
|
|
4
|
+
exports.durationToMs = durationToMs;
|
|
5
|
+
const DURATION_RE = /^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$/;
|
|
6
|
+
/** Normalizes a Duration to Go-style string format (e.g. "1h30m5s"). */
|
|
7
|
+
function durationToString(d) {
|
|
8
|
+
if (typeof d === 'string')
|
|
9
|
+
return d;
|
|
10
|
+
if (typeof d === 'number') {
|
|
11
|
+
const totalSeconds = Math.floor(d / 1000);
|
|
12
|
+
const h = Math.floor(totalSeconds / 3600);
|
|
13
|
+
const m = Math.floor((totalSeconds % 3600) / 60);
|
|
14
|
+
const s = totalSeconds % 60;
|
|
15
|
+
let out = '';
|
|
16
|
+
if (h)
|
|
17
|
+
out += `${h}h`;
|
|
18
|
+
if (m)
|
|
19
|
+
out += `${m}m`;
|
|
20
|
+
if (s || !out)
|
|
21
|
+
out += `${s}s`;
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
let s = '';
|
|
25
|
+
if (d.hours)
|
|
26
|
+
s += `${d.hours}h`;
|
|
27
|
+
if (d.minutes)
|
|
28
|
+
s += `${d.minutes}m`;
|
|
29
|
+
if (d.seconds)
|
|
30
|
+
s += `${d.seconds}s`;
|
|
31
|
+
return s || '0s';
|
|
32
|
+
}
|
|
33
|
+
function durationToMs(d) {
|
|
34
|
+
var _a, _b, _c;
|
|
35
|
+
if (typeof d === 'number')
|
|
36
|
+
return d;
|
|
37
|
+
if (typeof d === 'object') {
|
|
38
|
+
return (((_a = d.hours) !== null && _a !== void 0 ? _a : 0) * 3600 + ((_b = d.minutes) !== null && _b !== void 0 ? _b : 0) * 60 + ((_c = d.seconds) !== null && _c !== void 0 ? _c : 0)) * 1000;
|
|
39
|
+
}
|
|
40
|
+
const match = d.match(DURATION_RE);
|
|
41
|
+
if (!match) {
|
|
42
|
+
throw new Error(`Invalid duration string: "${d}". Expected format like "1h30m5s", "10m", "30s".`);
|
|
43
|
+
}
|
|
44
|
+
const [, h, m, s] = match;
|
|
45
|
+
return ((parseInt(h !== null && h !== void 0 ? h : '0', 10) * 3600 + parseInt(m !== null && m !== void 0 ? m : '0', 10) * 60 + parseInt(s !== null && s !== void 0 ? s : '0', 10)) * 1000);
|
|
46
|
+
}
|
|
@@ -106,10 +106,23 @@ export declare class RunsClient {
|
|
|
106
106
|
private prepareFilter;
|
|
107
107
|
private prepareListFilter;
|
|
108
108
|
/**
|
|
109
|
-
*
|
|
110
|
-
* @param
|
|
111
|
-
* @returns A promise that resolves to the run reference.
|
|
109
|
+
* Restore an evicted durable task so it can resume execution.
|
|
110
|
+
* @param taskExternalId - The external ID of the evicted task.
|
|
112
111
|
*/
|
|
112
|
+
restoreTask(taskExternalId: string): Promise<import("axios").AxiosResponse<import("../../../clients/rest/generated/data-contracts").V1RestoreTaskResponse, any, {}>>;
|
|
113
|
+
/**
|
|
114
|
+
* Fork (reset) a durable task from a specific node, triggering re-execution from that point.
|
|
115
|
+
* @param taskExternalId - The external ID of the durable task to reset.
|
|
116
|
+
* @param nodeId - The node ID to replay from.
|
|
117
|
+
*/
|
|
118
|
+
branchDurableTask(taskExternalId: string, nodeId: number, branchId?: number): Promise<import("axios").AxiosResponse<import("../../../clients/rest/generated/data-contracts").V1BranchDurableTaskResponse, any, {}>>;
|
|
119
|
+
/**
|
|
120
|
+
* Resolve the task external ID for a workflow run. For runs with multiple tasks,
|
|
121
|
+
* returns the first task's external ID.
|
|
122
|
+
* @param workflowRunId - The workflow run ID to look up.
|
|
123
|
+
* @returns The task external ID.
|
|
124
|
+
*/
|
|
125
|
+
getTaskExternalId(workflowRunId: string): Promise<string>;
|
|
113
126
|
runRef<T extends Record<string, any> = any>(id: string): WorkflowRunRef<T>;
|
|
114
127
|
/**
|
|
115
128
|
* Subscribes to a stream of events for a task or workflow run by its ID.
|
|
@@ -147,10 +147,45 @@ class RunsClient {
|
|
|
147
147
|
});
|
|
148
148
|
}
|
|
149
149
|
/**
|
|
150
|
-
*
|
|
151
|
-
* @param
|
|
152
|
-
* @returns A promise that resolves to the run reference.
|
|
150
|
+
* Restore an evicted durable task so it can resume execution.
|
|
151
|
+
* @param taskExternalId - The external ID of the evicted task.
|
|
153
152
|
*/
|
|
153
|
+
restoreTask(taskExternalId) {
|
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
155
|
+
return this.api.v1TaskRestore(taskExternalId);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Fork (reset) a durable task from a specific node, triggering re-execution from that point.
|
|
160
|
+
* @param taskExternalId - The external ID of the durable task to reset.
|
|
161
|
+
* @param nodeId - The node ID to replay from.
|
|
162
|
+
*/
|
|
163
|
+
branchDurableTask(taskExternalId_1, nodeId_1) {
|
|
164
|
+
return __awaiter(this, arguments, void 0, function* (taskExternalId, nodeId, branchId = 0) {
|
|
165
|
+
return this.api.v1DurableTaskBranch(this.tenantId, {
|
|
166
|
+
taskExternalId,
|
|
167
|
+
nodeId,
|
|
168
|
+
branchId,
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Resolve the task external ID for a workflow run. For runs with multiple tasks,
|
|
174
|
+
* returns the first task's external ID.
|
|
175
|
+
* @param workflowRunId - The workflow run ID to look up.
|
|
176
|
+
* @returns The task external ID.
|
|
177
|
+
*/
|
|
178
|
+
getTaskExternalId(workflowRunId) {
|
|
179
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
180
|
+
var _a;
|
|
181
|
+
const run = yield this.get(workflowRunId);
|
|
182
|
+
const tasks = run === null || run === void 0 ? void 0 : run.tasks;
|
|
183
|
+
if (Array.isArray(tasks) && tasks.length > 0 && ((_a = tasks[0]) === null || _a === void 0 ? void 0 : _a.taskExternalId)) {
|
|
184
|
+
return tasks[0].taskExternalId;
|
|
185
|
+
}
|
|
186
|
+
throw new Error(`Could not find task external ID for workflow run ${workflowRunId}`);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
154
189
|
runRef(id) {
|
|
155
190
|
return new workflow_run_ref_1.default(id, this.listener, this);
|
|
156
191
|
}
|
|
@@ -8,23 +8,29 @@
|
|
|
8
8
|
* @module Context
|
|
9
9
|
*/
|
|
10
10
|
import { Priority, RunOpts, TaskWorkflowDeclaration, BaseWorkflowDeclaration as WorkflowV1 } from '../../declaration';
|
|
11
|
-
import { JsonObject } from '@bufbuild/protobuf';
|
|
12
11
|
import { Action } from '../../../clients/dispatcher/action-listener';
|
|
13
12
|
import { Logger, LogLevel } from '../../../util/logger';
|
|
14
13
|
import WorkflowRunRef from '../../../util/workflow-run-ref';
|
|
15
14
|
import { Conditions } from '../../conditions';
|
|
16
15
|
import { CreateWorkflowDurableTaskOpts, CreateWorkflowTaskOpts } from '../../task';
|
|
17
|
-
import { OutputType } from '../../types';
|
|
16
|
+
import { JsonObject, OutputType } from '../../types';
|
|
18
17
|
import { HatchetClient } from '../..';
|
|
19
18
|
import { WorkerLabels } from '../../../clients/dispatcher/dispatcher-client';
|
|
20
19
|
import { NextStep } from '../../../legacy/step';
|
|
20
|
+
import { DurableListenerClient } from '../../../clients/listeners/durable-listener/durable-listener-client';
|
|
21
|
+
import { z } from 'zod';
|
|
21
22
|
import { InternalWorker } from './worker-internal';
|
|
22
23
|
import { Duration } from '../duration';
|
|
24
|
+
import { DurableEvictionManager } from './eviction/eviction-manager';
|
|
23
25
|
type TriggerData = Record<string, Record<string, any>>;
|
|
24
26
|
type ChildRunOpts = RunOpts & {
|
|
25
27
|
key?: string;
|
|
26
28
|
sticky?: boolean;
|
|
27
29
|
};
|
|
30
|
+
export interface SleepResult {
|
|
31
|
+
/** The sleep duration in milliseconds. */
|
|
32
|
+
durationMs: number;
|
|
33
|
+
}
|
|
28
34
|
type LogExtra = {
|
|
29
35
|
extra?: any;
|
|
30
36
|
error?: Error;
|
|
@@ -207,7 +213,30 @@ export declare class Context<T, K = {}> {
|
|
|
207
213
|
* @returns A promise that resolves when the data has been streamed.
|
|
208
214
|
*/
|
|
209
215
|
putStream(data: string | Uint8Array): Promise<void>;
|
|
210
|
-
|
|
216
|
+
protected spawnOptions(workflow: string | WorkflowV1<any, any>, options?: ChildRunOpts): {
|
|
217
|
+
workflowName: string;
|
|
218
|
+
opts: {
|
|
219
|
+
parentId: string;
|
|
220
|
+
parentTaskRunExternalId: string;
|
|
221
|
+
childIndex: number;
|
|
222
|
+
childKey: string | undefined;
|
|
223
|
+
desiredWorkerId: string | undefined;
|
|
224
|
+
_standaloneTaskName: string | undefined;
|
|
225
|
+
additionalMetadata?: {
|
|
226
|
+
[x: string]: string;
|
|
227
|
+
};
|
|
228
|
+
priority?: Priority;
|
|
229
|
+
sticky?: boolean;
|
|
230
|
+
returnExceptions?: boolean;
|
|
231
|
+
desiredWorkerLabels?: Record<string, {
|
|
232
|
+
value: string | number;
|
|
233
|
+
required?: boolean;
|
|
234
|
+
weight?: number;
|
|
235
|
+
comparator?: import("../..").WorkerLabelComparator;
|
|
236
|
+
}>;
|
|
237
|
+
key?: string;
|
|
238
|
+
};
|
|
239
|
+
};
|
|
211
240
|
private spawn;
|
|
212
241
|
private spawnBulk;
|
|
213
242
|
/**
|
|
@@ -323,14 +352,26 @@ export declare class Context<T, K = {}> {
|
|
|
323
352
|
* It extends the Context class and includes additional methods for durable execution like sleepFor and waitFor.
|
|
324
353
|
*/
|
|
325
354
|
export declare class DurableContext<T, K = {}> extends Context<T, K> {
|
|
326
|
-
|
|
355
|
+
private _durableListener;
|
|
356
|
+
private _evictionManager;
|
|
357
|
+
private _engineVersion;
|
|
358
|
+
private _waitKey;
|
|
359
|
+
constructor(action: Action, v1: HatchetClient, worker: InternalWorker, durableListener: DurableListenerClient, evictionManager?: DurableEvictionManager, engineVersion?: string);
|
|
360
|
+
get supportsEviction(): boolean;
|
|
361
|
+
get durableListener(): DurableListenerClient;
|
|
362
|
+
/**
|
|
363
|
+
* The invocation count for the current durable task. Used for deduplication across replays.
|
|
364
|
+
*/
|
|
365
|
+
get invocationCount(): number;
|
|
366
|
+
private get _actionKey();
|
|
367
|
+
private withEvictionWait;
|
|
327
368
|
/**
|
|
328
369
|
* Pauses execution for the specified duration.
|
|
329
370
|
* Duration is "global" meaning it will wait in real time regardless of transient failures like worker restarts.
|
|
330
371
|
* @param duration - The duration to sleep for.
|
|
331
|
-
* @returns A promise that resolves when the sleep duration has elapsed.
|
|
372
|
+
* @returns A promise that resolves with a SleepResult when the sleep duration has elapsed.
|
|
332
373
|
*/
|
|
333
|
-
sleepFor(duration: Duration, readableDataKey?: string): Promise<
|
|
374
|
+
sleepFor(duration: Duration, readableDataKey?: string): Promise<SleepResult>;
|
|
334
375
|
/**
|
|
335
376
|
* Pauses execution until the specified conditions are met.
|
|
336
377
|
* Conditions are "global" meaning they will wait in real time regardless of transient failures like worker restarts.
|
|
@@ -338,5 +379,59 @@ export declare class DurableContext<T, K = {}> extends Context<T, K> {
|
|
|
338
379
|
* @returns A promise that resolves with the event that satisfied the conditions.
|
|
339
380
|
*/
|
|
340
381
|
waitFor(conditions: Conditions | Conditions[]): Promise<Record<string, any>>;
|
|
382
|
+
/**
|
|
383
|
+
* Lightweight wrapper for waiting for a user event. Allows for shorthand usage of
|
|
384
|
+
* `ctx.waitFor` when specifying a user event condition.
|
|
385
|
+
*
|
|
386
|
+
* For more complicated conditions, use `ctx.waitFor` directly.
|
|
387
|
+
*
|
|
388
|
+
* @param key - The event key to wait for.
|
|
389
|
+
* @param expression - An optional CEL expression to filter events.
|
|
390
|
+
* @param payloadSchema - An optional Zod schema to validate and parse the event payload.
|
|
391
|
+
* @returns The event payload, validated against the schema if provided.
|
|
392
|
+
*/
|
|
393
|
+
waitForEvent<T extends z.ZodTypeAny>(key: string, expression?: string, payloadSchema?: T): Promise<z.infer<T>>;
|
|
394
|
+
waitForEvent(key: string, expression?: string): Promise<Record<string, any>>;
|
|
395
|
+
/**
|
|
396
|
+
* Durably sleep until a specific timestamp.
|
|
397
|
+
* Uses the memoized `now()` to compute the remaining duration, then delegates to `sleepFor`.
|
|
398
|
+
*
|
|
399
|
+
* @param wakeAt - The timestamp to sleep until.
|
|
400
|
+
* @returns A SleepResult containing the actual duration slept.
|
|
401
|
+
*/
|
|
402
|
+
sleepUntil(wakeAt: Date): Promise<SleepResult>;
|
|
403
|
+
/**
|
|
404
|
+
* Get the current timestamp, memoized across replays. Returns the same Date on every replay of the same task run.
|
|
405
|
+
* @returns The memoized current timestamp.
|
|
406
|
+
*/
|
|
407
|
+
now(): Promise<Date>;
|
|
408
|
+
private _waitForPreEviction;
|
|
409
|
+
private _buildTriggerOpts;
|
|
410
|
+
/**
|
|
411
|
+
* Spawns a child workflow through the durable event log, waits for the child to complete.
|
|
412
|
+
* @param workflow - The workflow to spawn.
|
|
413
|
+
* @param input - The input data for the child workflow.
|
|
414
|
+
* @param options - Options for spawning the child workflow.
|
|
415
|
+
* @returns The result of the child workflow.
|
|
416
|
+
*/
|
|
417
|
+
spawnChild<Q extends JsonObject, P extends OutputType>(workflow: string | WorkflowV1<Q, P> | TaskWorkflowDeclaration<Q, P>, input?: Q, options?: ChildRunOpts): Promise<P>;
|
|
418
|
+
/**
|
|
419
|
+
* Spawns multiple child workflows through the durable event log, waits for all to complete.
|
|
420
|
+
* @param children - An array of objects containing the workflow, input, and options for each child.
|
|
421
|
+
* @returns A list of results from the child workflows.
|
|
422
|
+
*/
|
|
423
|
+
spawnChildren<Q extends JsonObject, P extends OutputType>(children: Array<{
|
|
424
|
+
workflow: string | WorkflowV1<Q, P> | TaskWorkflowDeclaration<Q, P>;
|
|
425
|
+
input: Q;
|
|
426
|
+
options?: ChildRunOpts;
|
|
427
|
+
}>): Promise<P[]>;
|
|
428
|
+
/**
|
|
429
|
+
* Memoize a function by storing its result in durable storage. Avoids recomputation on replay.
|
|
430
|
+
*
|
|
431
|
+
* @param fn - The async function to compute the value.
|
|
432
|
+
* @param deps - Dependency values that form the memoization key.
|
|
433
|
+
* @returns The memoized value, either from durable storage or freshly computed.
|
|
434
|
+
*/
|
|
435
|
+
private memo;
|
|
341
436
|
}
|
|
342
437
|
export {};
|