@hatchet-dev/typescript-sdk 1.3.1 → 1.4.0-alpha.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/clients/dispatcher/heartbeat/heartbeat-worker.js +3 -2
- package/clients/hatchet-client/hatchet-client.d.ts +3 -5
- package/clients/hatchet-client/hatchet-client.js +11 -72
- package/clients/worker/worker.d.ts +6 -9
- package/clients/worker/worker.js +2 -164
- package/examples/fanout-worker.js +1 -2
- package/examples/manual-trigger.js +1 -1
- package/package.json +3 -2
- package/protoc/workflows/workflows.d.ts +1 -1
- package/protoc/workflows/workflows.js +5 -5
- package/step.d.ts +13 -11
- package/step.js +44 -40
- package/util/grpc-helpers.d.ts +10 -0
- package/util/grpc-helpers.js +79 -0
- package/v1/client/admin.d.ts +64 -0
- package/v1/client/admin.js +155 -0
- package/v1/client/client.d.ts +14 -9
- package/v1/client/client.interface.d.ts +4 -2
- package/v1/client/client.js +28 -16
- package/v1/client/worker/context.d.ts +246 -0
- package/v1/client/worker/context.js +512 -0
- package/v1/client/worker/worker-internal.d.ts +62 -0
- package/v1/client/worker/worker-internal.js +703 -0
- package/v1/client/{worker.d.ts → worker/worker.d.ts} +13 -13
- package/v1/client/{worker.js → worker/worker.js} +8 -4
- package/v1/declaration.d.ts +3 -3
- package/v1/declaration.js +21 -14
- package/v1/examples/cancellations/run.js +4 -4
- package/v1/examples/cancellations/workflow.js +2 -2
- package/v1/examples/concurrency-rr/workflow.d.ts +1 -0
- package/v1/examples/concurrency-rr/workflow.js +27 -1
- package/v1/examples/high-memory/child-worker.js +29 -0
- package/v1/examples/high-memory/parent-worker.d.ts +1 -0
- package/v1/examples/high-memory/parent-worker.js +29 -0
- package/v1/examples/high-memory/run.d.ts +1 -0
- package/v1/examples/high-memory/run.js +27 -0
- package/v1/examples/high-memory/workflow-with-child.d.ts +12 -0
- package/v1/examples/high-memory/workflow-with-child.js +48 -0
- package/v1/examples/with_timeouts/workflow.js +4 -4
- package/v1/index.d.ts +1 -1
- package/v1/index.js +1 -1
- package/v1/task.d.ts +2 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/examples/api.js +0 -61
- /package/{examples/api.d.ts → v1/examples/high-memory/child-worker.d.ts} +0 -0
|
@@ -13,6 +13,7 @@ const worker_threads_1 = require("worker_threads");
|
|
|
13
13
|
const hatchet_client_1 = require("../../hatchet-client");
|
|
14
14
|
const config_loader_1 = require("../../../util/config-loader");
|
|
15
15
|
const nice_grpc_1 = require("nice-grpc");
|
|
16
|
+
const grpc_helpers_1 = require("../../../util/grpc-helpers");
|
|
16
17
|
const dispatcher_client_1 = require("../dispatcher-client");
|
|
17
18
|
const heartbeat_controller_1 = require("./heartbeat-controller");
|
|
18
19
|
const HEARTBEAT_INTERVAL = 4000;
|
|
@@ -26,8 +27,8 @@ class HeartbeatWorker {
|
|
|
26
27
|
this.logger = new hatchet_client_1.HatchetLogger(`HeartbeatThread`, config.log_level);
|
|
27
28
|
this.logger.debug('Heartbeat thread starting...');
|
|
28
29
|
const credentials = config_loader_1.ConfigLoader.createCredentials(config.tls_config);
|
|
29
|
-
const clientFactory = (0, nice_grpc_1.createClientFactory)().use((0,
|
|
30
|
-
const dispatcher = new dispatcher_client_1.DispatcherClient(Object.assign(Object.assign({}, config), { logger: (ctx, level) => new hatchet_client_1.HatchetLogger(ctx, level) }), (0,
|
|
30
|
+
const clientFactory = (0, nice_grpc_1.createClientFactory)().use((0, grpc_helpers_1.addTokenMiddleware)(config.token));
|
|
31
|
+
const dispatcher = new dispatcher_client_1.DispatcherClient(Object.assign(Object.assign({}, config), { logger: (ctx, level) => new hatchet_client_1.HatchetLogger(ctx, level) }), (0, grpc_helpers_1.channelFactory)(config, credentials), clientFactory);
|
|
31
32
|
this.client = dispatcher.client;
|
|
32
33
|
postMessage({
|
|
33
34
|
type: 'debug',
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EventClient } from '../event/event-client';
|
|
2
2
|
import { DispatcherClient } from '../dispatcher/dispatcher-client';
|
|
3
3
|
import { AdminClient } from '../admin/admin-client';
|
|
4
|
-
import {
|
|
4
|
+
import { ChannelCredentials } from 'nice-grpc';
|
|
5
5
|
import { Workflow as V0Workflow } from '../../workflow';
|
|
6
6
|
import { V0Worker, WorkerOpts } from '../worker';
|
|
7
7
|
import { AxiosRequestConfig } from 'axios';
|
|
@@ -17,9 +17,7 @@ export interface HatchetClientOptions {
|
|
|
17
17
|
config_path?: string;
|
|
18
18
|
credentials?: ChannelCredentials;
|
|
19
19
|
}
|
|
20
|
-
export declare
|
|
21
|
-
export declare const addTokenMiddleware: (token: string) => <Request, Response>(call: ClientMiddlewareCall<Request, Response>, options: CallOptions) => AsyncGenerator<Awaited<Response>, Awaited<Response> | undefined, undefined>;
|
|
22
|
-
export declare class InternalHatchetClient {
|
|
20
|
+
export declare class LegacyHatchetClient {
|
|
23
21
|
config: ClientConfig;
|
|
24
22
|
credentials: ChannelCredentials;
|
|
25
23
|
event: EventClient;
|
|
@@ -34,7 +32,7 @@ export declare class InternalHatchetClient {
|
|
|
34
32
|
cron: CronClient;
|
|
35
33
|
schedule: ScheduleClient;
|
|
36
34
|
constructor(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosOpts?: AxiosRequestConfig, runs?: RunsClient);
|
|
37
|
-
static init(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosConfig?: AxiosRequestConfig):
|
|
35
|
+
static init(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosConfig?: AxiosRequestConfig): LegacyHatchetClient;
|
|
38
36
|
run(workflow: string | V0Workflow): Promise<V0Worker>;
|
|
39
37
|
worker(workflow: string | V0Workflow, opts?: Omit<WorkerOpts, 'name'> | number): Promise<V0Worker>;
|
|
40
38
|
webhooks(workflows: Array<V0Workflow>): import("../worker/handler").WebhookHandler;
|
|
@@ -8,36 +8,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
12
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
13
|
-
var m = o[Symbol.asyncIterator], i;
|
|
14
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
15
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
16
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
|
-
};
|
|
18
|
-
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
19
|
-
var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
|
|
20
|
-
var i, p;
|
|
21
|
-
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
|
|
22
|
-
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }
|
|
23
|
-
};
|
|
24
|
-
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
25
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
26
|
-
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
27
|
-
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
28
|
-
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
|
|
29
|
-
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
|
|
30
|
-
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
31
|
-
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
32
|
-
function fulfill(value) { resume("next", value); }
|
|
33
|
-
function reject(value) { resume("throw", value); }
|
|
34
|
-
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
35
|
-
};
|
|
36
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
13
|
};
|
|
39
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
-
exports.
|
|
15
|
+
exports.LegacyHatchetClient = void 0;
|
|
41
16
|
const zod_1 = require("zod");
|
|
42
17
|
const config_loader_1 = require("../../util/config-loader");
|
|
43
18
|
const event_client_1 = require("../event/event-client");
|
|
@@ -46,50 +21,14 @@ const admin_client_1 = require("../admin/admin-client");
|
|
|
46
21
|
const nice_grpc_1 = require("nice-grpc");
|
|
47
22
|
const worker_1 = require("../worker");
|
|
48
23
|
const hatchet_logger_1 = require("./hatchet-logger");
|
|
24
|
+
const grpc_helpers_1 = require("../../util/grpc-helpers");
|
|
49
25
|
const client_config_1 = require("./client-config");
|
|
50
26
|
const child_listener_client_1 = require("../listeners/run-listener/child-listener-client");
|
|
51
27
|
const rest_1 = __importDefault(require("../rest"));
|
|
52
28
|
const cron_client_1 = require("./features/cron-client");
|
|
53
29
|
const schedule_client_1 = require("./features/schedule-client");
|
|
54
30
|
const durable_listener_client_1 = require("../listeners/durable-listener/durable-listener-client");
|
|
55
|
-
|
|
56
|
-
'grpc.ssl_target_name_override': config.tls_config.server_name,
|
|
57
|
-
'grpc.keepalive_timeout_ms': 60 * 1000,
|
|
58
|
-
'grpc.client_idle_timeout_ms': 60 * 1000,
|
|
59
|
-
// Send keepalive pings every 10 seconds, default is 2 hours.
|
|
60
|
-
'grpc.keepalive_time_ms': 10 * 1000,
|
|
61
|
-
// Allow keepalive pings when there are no gRPC calls.
|
|
62
|
-
'grpc.keepalive_permit_without_calls': 1,
|
|
63
|
-
});
|
|
64
|
-
exports.channelFactory = channelFactory;
|
|
65
|
-
const addTokenMiddleware = (token) => function _(call, options) {
|
|
66
|
-
return __asyncGenerator(this, arguments, function* _1() {
|
|
67
|
-
var _a, e_1, _b, _c;
|
|
68
|
-
const optionsWithAuth = Object.assign(Object.assign({}, options), { metadata: new nice_grpc_1.Metadata({ authorization: `bearer ${token}` }) });
|
|
69
|
-
if (!call.responseStream) {
|
|
70
|
-
const response = yield __await(yield* __asyncDelegator(__asyncValues(call.next(call.request, optionsWithAuth))));
|
|
71
|
-
return yield __await(response);
|
|
72
|
-
}
|
|
73
|
-
try {
|
|
74
|
-
for (var _d = true, _e = __asyncValues(call.next(call.request, optionsWithAuth)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) {
|
|
75
|
-
_c = _f.value;
|
|
76
|
-
_d = false;
|
|
77
|
-
const response = _c;
|
|
78
|
-
yield yield __await(response);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
82
|
-
finally {
|
|
83
|
-
try {
|
|
84
|
-
if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
|
|
85
|
-
}
|
|
86
|
-
finally { if (e_1) throw e_1.error; }
|
|
87
|
-
}
|
|
88
|
-
return yield __await(undefined);
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
exports.addTokenMiddleware = addTokenMiddleware;
|
|
92
|
-
class InternalHatchetClient {
|
|
31
|
+
class LegacyHatchetClient {
|
|
93
32
|
constructor(config, options, axiosOpts, runs) {
|
|
94
33
|
// Initializes a new Client instance.
|
|
95
34
|
// Loads config in the following order: config param > yaml file > env vars
|
|
@@ -114,14 +53,14 @@ class InternalHatchetClient {
|
|
|
114
53
|
}
|
|
115
54
|
this.credentials =
|
|
116
55
|
(_a = options === null || options === void 0 ? void 0 : options.credentials) !== null && _a !== void 0 ? _a : config_loader_1.ConfigLoader.createCredentials(this.config.tls_config);
|
|
117
|
-
const clientFactory = (0, nice_grpc_1.createClientFactory)().use((0,
|
|
56
|
+
const clientFactory = (0, nice_grpc_1.createClientFactory)().use((0, grpc_helpers_1.addTokenMiddleware)(this.config.token));
|
|
118
57
|
this.tenantId = this.config.tenant_id;
|
|
119
58
|
this.api = (0, rest_1.default)(this.config.api_url, this.config.token, axiosOpts);
|
|
120
|
-
this.event = new event_client_1.EventClient(this.config, (0,
|
|
121
|
-
this.dispatcher = new dispatcher_client_1.DispatcherClient(this.config, (0,
|
|
122
|
-
this.listener = new child_listener_client_1.RunListenerClient(this.config, (0,
|
|
123
|
-
this.admin = new admin_client_1.AdminClient(this.config, (0,
|
|
124
|
-
this.durableListener = new durable_listener_client_1.DurableListenerClient(this.config, (0,
|
|
59
|
+
this.event = new event_client_1.EventClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory);
|
|
60
|
+
this.dispatcher = new dispatcher_client_1.DispatcherClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory);
|
|
61
|
+
this.listener = new child_listener_client_1.RunListenerClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this.api);
|
|
62
|
+
this.admin = new admin_client_1.AdminClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this.api, this.tenantId, this.listener, this.runs);
|
|
63
|
+
this.durableListener = new durable_listener_client_1.DurableListenerClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this.api);
|
|
125
64
|
this.logger = this.config.logger('HatchetClient', this.config.log_level);
|
|
126
65
|
this.logger.debug(`Initialized HatchetClient`);
|
|
127
66
|
// Feature Clients
|
|
@@ -129,7 +68,7 @@ class InternalHatchetClient {
|
|
|
129
68
|
this.schedule = new schedule_client_1.ScheduleClient(this.tenantId, this.config, this.api, this.admin);
|
|
130
69
|
}
|
|
131
70
|
static init(config, options, axiosConfig) {
|
|
132
|
-
return new
|
|
71
|
+
return new LegacyHatchetClient(config, options, axiosConfig);
|
|
133
72
|
}
|
|
134
73
|
// @deprecated
|
|
135
74
|
run(workflow) {
|
|
@@ -169,4 +108,4 @@ class InternalHatchetClient {
|
|
|
169
108
|
return worker.getHandler(workflows);
|
|
170
109
|
}
|
|
171
110
|
}
|
|
172
|
-
exports.
|
|
111
|
+
exports.LegacyHatchetClient = LegacyHatchetClient;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LegacyHatchetClient } from '../hatchet-client';
|
|
2
2
|
import { Action, ActionListener } from '../dispatcher/action-listener';
|
|
3
3
|
import { StepActionEvent, StepActionEventType, GroupKeyActionEvent, GroupKeyActionEventType } from '../../protoc/dispatcher';
|
|
4
4
|
import HatchetPromise from '../../util/hatchet-promise/hatchet-promise';
|
|
@@ -6,8 +6,8 @@ import { Workflow } from '../../workflow';
|
|
|
6
6
|
import { Logger } from '../../util/logger';
|
|
7
7
|
import { WebhookHandler } from './handler';
|
|
8
8
|
import { WebhookWorkerCreateRequest } from '../rest/generated/data-contracts';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
9
|
+
import { WorkflowDefinition } from '../../v1/declaration';
|
|
10
|
+
import { V0Context, StepRunFunction } from '../../step';
|
|
11
11
|
import { WorkerLabels } from '../dispatcher/dispatcher-client';
|
|
12
12
|
export type ActionRegistry = Record<Action['actionId'], Function>;
|
|
13
13
|
export interface WorkerOpts {
|
|
@@ -17,7 +17,7 @@ export interface WorkerOpts {
|
|
|
17
17
|
labels?: WorkerLabels;
|
|
18
18
|
}
|
|
19
19
|
export declare class V0Worker {
|
|
20
|
-
client:
|
|
20
|
+
client: LegacyHatchetClient;
|
|
21
21
|
name: string;
|
|
22
22
|
workerId: string | undefined;
|
|
23
23
|
killing: boolean;
|
|
@@ -26,12 +26,12 @@ export declare class V0Worker {
|
|
|
26
26
|
workflow_registry: Array<WorkflowDefinition | Workflow>;
|
|
27
27
|
listener: ActionListener | undefined;
|
|
28
28
|
futures: Record<Action['stepRunId'], HatchetPromise<any>>;
|
|
29
|
-
contexts: Record<Action['stepRunId'],
|
|
29
|
+
contexts: Record<Action['stepRunId'], V0Context<any, any>>;
|
|
30
30
|
maxRuns?: number;
|
|
31
31
|
logger: Logger;
|
|
32
32
|
registeredWorkflowPromises: Array<Promise<any>>;
|
|
33
33
|
labels: WorkerLabels;
|
|
34
|
-
constructor(client:
|
|
34
|
+
constructor(client: LegacyHatchetClient, options: {
|
|
35
35
|
name: string;
|
|
36
36
|
handleKill?: boolean;
|
|
37
37
|
maxRuns?: number;
|
|
@@ -44,9 +44,6 @@ export declare class V0Worker {
|
|
|
44
44
|
* @deprecated use registerWorkflow instead
|
|
45
45
|
*/
|
|
46
46
|
register_workflow(initWorkflow: Workflow): Promise<void>;
|
|
47
|
-
registerDurableActionsV1(workflow: WorkflowDefinition): void;
|
|
48
|
-
private registerActionsV1;
|
|
49
|
-
registerWorkflowV1(initWorkflow: BaseWorkflowDeclaration<any, any>): Promise<void>;
|
|
50
47
|
registerWorkflow(initWorkflow: Workflow): Promise<void>;
|
|
51
48
|
registerAction<T, K>(actionId: string, action: StepRunFunction<T, K>): void;
|
|
52
49
|
handleStartStepRun(action: Action): Promise<void>;
|
package/clients/worker/worker.js
CHANGED
|
@@ -26,7 +26,6 @@ const hatchet_promise_1 = __importDefault(require("../../util/hatchet-promise/ha
|
|
|
26
26
|
const workflows_1 = require("../../protoc/workflows");
|
|
27
27
|
const handler_1 = require("./handler");
|
|
28
28
|
const task_1 = require("../../v1/task");
|
|
29
|
-
const transformer_1 = require("../../v1/conditions/transformer");
|
|
30
29
|
const step_1 = require("../../step");
|
|
31
30
|
class V0Worker {
|
|
32
31
|
constructor(client, options) {
|
|
@@ -83,167 +82,6 @@ class V0Worker {
|
|
|
83
82
|
return this.registerWorkflow(initWorkflow);
|
|
84
83
|
});
|
|
85
84
|
}
|
|
86
|
-
registerDurableActionsV1(workflow) {
|
|
87
|
-
const newActions = workflow._durableTasks.reduce((acc, task) => {
|
|
88
|
-
acc[`${workflow.name}:${task.name.toLowerCase()}`] = (ctx) => task.fn(ctx.workflowInput(), ctx);
|
|
89
|
-
return acc;
|
|
90
|
-
}, {});
|
|
91
|
-
this.action_registry = Object.assign(Object.assign({}, this.action_registry), newActions);
|
|
92
|
-
}
|
|
93
|
-
registerActionsV1(workflow) {
|
|
94
|
-
const newActions = workflow._tasks.reduce((acc, task) => {
|
|
95
|
-
acc[`${workflow.name}:${task.name.toLowerCase()}`] = (ctx) => task.fn(ctx.workflowInput(), ctx);
|
|
96
|
-
return acc;
|
|
97
|
-
}, {});
|
|
98
|
-
const onFailureFn = workflow.onFailure
|
|
99
|
-
? typeof workflow.onFailure === 'function'
|
|
100
|
-
? workflow.onFailure
|
|
101
|
-
: workflow.onFailure.fn
|
|
102
|
-
: undefined;
|
|
103
|
-
const onFailureAction = onFailureFn
|
|
104
|
-
? {
|
|
105
|
-
[onFailureTaskName(workflow)]: (ctx) => onFailureFn(ctx.workflowInput(), ctx),
|
|
106
|
-
}
|
|
107
|
-
: {};
|
|
108
|
-
this.action_registry = Object.assign(Object.assign(Object.assign({}, this.action_registry), newActions), onFailureAction);
|
|
109
|
-
}
|
|
110
|
-
registerWorkflowV1(initWorkflow) {
|
|
111
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
112
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
113
|
-
// patch the namespace
|
|
114
|
-
const workflow = Object.assign(Object.assign({}, initWorkflow.definition), { name: (this.client.config.namespace + initWorkflow.definition.name).toLowerCase() });
|
|
115
|
-
try {
|
|
116
|
-
const { concurrency } = workflow;
|
|
117
|
-
let onFailureTask;
|
|
118
|
-
if (workflow.onFailure && typeof workflow.onFailure === 'function') {
|
|
119
|
-
onFailureTask = {
|
|
120
|
-
readableId: 'on-failure-task',
|
|
121
|
-
action: onFailureTaskName(workflow),
|
|
122
|
-
timeout: '60s',
|
|
123
|
-
inputs: '{}',
|
|
124
|
-
parents: [],
|
|
125
|
-
retries: 0,
|
|
126
|
-
rateLimits: [],
|
|
127
|
-
workerLabels: {},
|
|
128
|
-
concurrency: [],
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
if (workflow.onFailure && typeof workflow.onFailure === 'object') {
|
|
132
|
-
const onFailure = workflow.onFailure;
|
|
133
|
-
onFailureTask = {
|
|
134
|
-
readableId: 'on-failure-task',
|
|
135
|
-
action: onFailureTaskName(workflow),
|
|
136
|
-
timeout: onFailure.executionTimeout || ((_a = workflow.taskDefaults) === null || _a === void 0 ? void 0 : _a.executionTimeout) || '60s',
|
|
137
|
-
scheduleTimeout: onFailure.scheduleTimeout || ((_b = workflow.taskDefaults) === null || _b === void 0 ? void 0 : _b.scheduleTimeout),
|
|
138
|
-
inputs: '{}',
|
|
139
|
-
parents: [],
|
|
140
|
-
retries: onFailure.retries || ((_c = workflow.taskDefaults) === null || _c === void 0 ? void 0 : _c.retries) || 0,
|
|
141
|
-
rateLimits: (0, step_1.mapRateLimit)(onFailure.rateLimits || ((_d = workflow.taskDefaults) === null || _d === void 0 ? void 0 : _d.rateLimits)),
|
|
142
|
-
workerLabels: toPbWorkerLabel(onFailure.desiredWorkerLabels || ((_e = workflow.taskDefaults) === null || _e === void 0 ? void 0 : _e.workerLabels)),
|
|
143
|
-
concurrency: [],
|
|
144
|
-
backoffFactor: ((_f = onFailure.backoff) === null || _f === void 0 ? void 0 : _f.factor) || ((_h = (_g = workflow.taskDefaults) === null || _g === void 0 ? void 0 : _g.backoff) === null || _h === void 0 ? void 0 : _h.factor),
|
|
145
|
-
backoffMaxSeconds: ((_j = onFailure.backoff) === null || _j === void 0 ? void 0 : _j.maxSeconds) || ((_l = (_k = workflow.taskDefaults) === null || _k === void 0 ? void 0 : _k.backoff) === null || _l === void 0 ? void 0 : _l.maxSeconds),
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
let onSuccessTask;
|
|
149
|
-
if (workflow.onSuccess && typeof workflow.onSuccess === 'function') {
|
|
150
|
-
const parents = getLeaves(workflow._tasks);
|
|
151
|
-
onSuccessTask = {
|
|
152
|
-
name: 'on-success-task',
|
|
153
|
-
fn: workflow.onSuccess,
|
|
154
|
-
timeout: '60s',
|
|
155
|
-
parents,
|
|
156
|
-
retries: 0,
|
|
157
|
-
rateLimits: [],
|
|
158
|
-
desiredWorkerLabels: {},
|
|
159
|
-
concurrency: [],
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
if (workflow.onSuccess && typeof workflow.onSuccess === 'object') {
|
|
163
|
-
const onSuccess = workflow.onSuccess;
|
|
164
|
-
const parents = getLeaves(workflow._tasks);
|
|
165
|
-
onSuccessTask = {
|
|
166
|
-
name: 'on-success-task',
|
|
167
|
-
fn: onSuccess.fn,
|
|
168
|
-
timeout: onSuccess.executionTimeout || ((_m = workflow.taskDefaults) === null || _m === void 0 ? void 0 : _m.executionTimeout) || '60s',
|
|
169
|
-
scheduleTimeout: onSuccess.scheduleTimeout || ((_o = workflow.taskDefaults) === null || _o === void 0 ? void 0 : _o.scheduleTimeout),
|
|
170
|
-
parents,
|
|
171
|
-
retries: onSuccess.retries || ((_p = workflow.taskDefaults) === null || _p === void 0 ? void 0 : _p.retries) || 0,
|
|
172
|
-
rateLimits: onSuccess.rateLimits || ((_q = workflow.taskDefaults) === null || _q === void 0 ? void 0 : _q.rateLimits),
|
|
173
|
-
desiredWorkerLabels: onSuccess.desiredWorkerLabels || ((_r = workflow.taskDefaults) === null || _r === void 0 ? void 0 : _r.workerLabels),
|
|
174
|
-
concurrency: onSuccess.concurrency || ((_s = workflow.taskDefaults) === null || _s === void 0 ? void 0 : _s.concurrency),
|
|
175
|
-
backoff: onSuccess.backoff || ((_t = workflow.taskDefaults) === null || _t === void 0 ? void 0 : _t.backoff),
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
if (onSuccessTask) {
|
|
179
|
-
workflow._tasks.push(onSuccessTask);
|
|
180
|
-
}
|
|
181
|
-
// cron and event triggers
|
|
182
|
-
if (workflow.on) {
|
|
183
|
-
this.logger.warn(`\`on\` for event and cron triggers is deprecated and will be removed soon, use \`onEvents\` and \`onCrons\` instead for ${workflow.name}`);
|
|
184
|
-
}
|
|
185
|
-
const eventTriggers = [
|
|
186
|
-
...(workflow.onEvents || []),
|
|
187
|
-
...(((_u = workflow.on) === null || _u === void 0 ? void 0 : _u.event) ? [workflow.on.event] : []),
|
|
188
|
-
];
|
|
189
|
-
const cronTriggers = [
|
|
190
|
-
...(workflow.onCrons || []),
|
|
191
|
-
...(((_v = workflow.on) === null || _v === void 0 ? void 0 : _v.cron) ? [workflow.on.cron] : []),
|
|
192
|
-
];
|
|
193
|
-
const concurrencyArr = Array.isArray(concurrency) ? concurrency : [];
|
|
194
|
-
const concurrencySolo = !Array.isArray(concurrency) ? concurrency : undefined;
|
|
195
|
-
const registeredWorkflow = this.client.admin.putWorkflowV1({
|
|
196
|
-
name: workflow.name,
|
|
197
|
-
description: workflow.description || '',
|
|
198
|
-
version: workflow.version || '',
|
|
199
|
-
eventTriggers,
|
|
200
|
-
cronTriggers,
|
|
201
|
-
sticky: workflow.sticky,
|
|
202
|
-
concurrencyArr,
|
|
203
|
-
onFailureTask,
|
|
204
|
-
defaultPriority: workflow.defaultPriority,
|
|
205
|
-
tasks: [...workflow._tasks, ...workflow._durableTasks].map((task) => {
|
|
206
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
|
|
207
|
-
return ({
|
|
208
|
-
readableId: task.name,
|
|
209
|
-
action: `${workflow.name}:${task.name}`,
|
|
210
|
-
timeout: task.executionTimeout ||
|
|
211
|
-
task.timeout ||
|
|
212
|
-
((_a = workflow.taskDefaults) === null || _a === void 0 ? void 0 : _a.executionTimeout) ||
|
|
213
|
-
'60s',
|
|
214
|
-
scheduleTimeout: task.scheduleTimeout || ((_b = workflow.taskDefaults) === null || _b === void 0 ? void 0 : _b.scheduleTimeout),
|
|
215
|
-
inputs: '{}',
|
|
216
|
-
parents: (_d = (_c = task.parents) === null || _c === void 0 ? void 0 : _c.map((p) => p.name)) !== null && _d !== void 0 ? _d : [],
|
|
217
|
-
userData: '{}',
|
|
218
|
-
retries: task.retries || ((_e = workflow.taskDefaults) === null || _e === void 0 ? void 0 : _e.retries) || 0,
|
|
219
|
-
rateLimits: (0, step_1.mapRateLimit)(task.rateLimits || ((_f = workflow.taskDefaults) === null || _f === void 0 ? void 0 : _f.rateLimits)),
|
|
220
|
-
workerLabels: toPbWorkerLabel(task.desiredWorkerLabels || ((_g = workflow.taskDefaults) === null || _g === void 0 ? void 0 : _g.workerLabels)),
|
|
221
|
-
backoffFactor: ((_h = task.backoff) === null || _h === void 0 ? void 0 : _h.factor) || ((_k = (_j = workflow.taskDefaults) === null || _j === void 0 ? void 0 : _j.backoff) === null || _k === void 0 ? void 0 : _k.factor),
|
|
222
|
-
backoffMaxSeconds: ((_l = task.backoff) === null || _l === void 0 ? void 0 : _l.maxSeconds) || ((_o = (_m = workflow.taskDefaults) === null || _m === void 0 ? void 0 : _m.backoff) === null || _o === void 0 ? void 0 : _o.maxSeconds),
|
|
223
|
-
conditions: (0, transformer_1.taskConditionsToPb)(task),
|
|
224
|
-
concurrency: task.concurrency
|
|
225
|
-
? Array.isArray(task.concurrency)
|
|
226
|
-
? task.concurrency
|
|
227
|
-
: [task.concurrency]
|
|
228
|
-
: ((_p = workflow.taskDefaults) === null || _p === void 0 ? void 0 : _p.concurrency)
|
|
229
|
-
? Array.isArray(workflow.taskDefaults.concurrency)
|
|
230
|
-
? workflow.taskDefaults.concurrency
|
|
231
|
-
: [workflow.taskDefaults.concurrency]
|
|
232
|
-
: [],
|
|
233
|
-
});
|
|
234
|
-
}),
|
|
235
|
-
concurrency: concurrencySolo,
|
|
236
|
-
});
|
|
237
|
-
this.registeredWorkflowPromises.push(registeredWorkflow);
|
|
238
|
-
yield registeredWorkflow;
|
|
239
|
-
this.workflow_registry.push(workflow);
|
|
240
|
-
}
|
|
241
|
-
catch (e) {
|
|
242
|
-
throw new hatchet_error_1.default(`Could not register workflow: ${e.message}`);
|
|
243
|
-
}
|
|
244
|
-
this.registerActionsV1(workflow);
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
85
|
registerWorkflow(initWorkflow) {
|
|
248
86
|
return __awaiter(this, void 0, void 0, function* () {
|
|
249
87
|
var _a, _b, _c;
|
|
@@ -335,7 +173,7 @@ class V0Worker {
|
|
|
335
173
|
const { actionId } = action;
|
|
336
174
|
try {
|
|
337
175
|
// Note: we always use a DurableContext since its a superset of the Context class
|
|
338
|
-
const context = new step_1.
|
|
176
|
+
const context = new step_1.V0DurableContext(action, this.client, this);
|
|
339
177
|
this.contexts[action.stepRunId] = context;
|
|
340
178
|
const step = this.action_registry[actionId];
|
|
341
179
|
if (!step) {
|
|
@@ -427,7 +265,7 @@ class V0Worker {
|
|
|
427
265
|
return __awaiter(this, void 0, void 0, function* () {
|
|
428
266
|
const { actionId } = action;
|
|
429
267
|
try {
|
|
430
|
-
const context = new step_1.
|
|
268
|
+
const context = new step_1.V0Context(action, this.client, this);
|
|
431
269
|
const key = action.getGroupKeyRunId;
|
|
432
270
|
if (!key) {
|
|
433
271
|
this.logger.error(`No group key run id provided for action ${actionId}`);
|
|
@@ -27,10 +27,9 @@ const parentWorkflow = {
|
|
|
27
27
|
run: (ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
28
|
const promises = Array.from({ length: 3 }, (_, i) => ctx
|
|
29
29
|
.spawnWorkflow('child-workflow', { input: `child-input-${i}` }, { additionalMetadata: { childKey: 'childValue' } })
|
|
30
|
-
.result()
|
|
31
30
|
.then((result) => {
|
|
32
31
|
ctx.log('spawned workflow result:');
|
|
33
|
-
return result;
|
|
32
|
+
return result.output;
|
|
34
33
|
}));
|
|
35
34
|
const results = yield Promise.all(promises);
|
|
36
35
|
console.log('spawned workflow results:', results);
|
|
@@ -24,7 +24,7 @@ const hatchet = sdk_1.default.init();
|
|
|
24
24
|
function main() {
|
|
25
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
26
26
|
var _a, e_1, _b, _c;
|
|
27
|
-
const workflowRun = hatchet.admin.runWorkflow('simple-workflow', {});
|
|
27
|
+
const workflowRun = yield hatchet.admin.runWorkflow('simple-workflow', {});
|
|
28
28
|
const stream = yield workflowRun.stream();
|
|
29
29
|
try {
|
|
30
30
|
for (var _d = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _a = stream_1_1.done, !_a; _d = true) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hatchet-dev/typescript-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0-alpha.1",
|
|
4
4
|
"description": "Background task orchestration & visibility for developers",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -88,5 +88,6 @@
|
|
|
88
88
|
"qs": "^6.14.0",
|
|
89
89
|
"yaml": "^2.7.1",
|
|
90
90
|
"zod": "^3.24.2"
|
|
91
|
-
}
|
|
91
|
+
},
|
|
92
|
+
"packageManager": "pnpm@9.15.4+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0"
|
|
92
93
|
}
|
|
@@ -196,7 +196,7 @@ export interface ScheduleWorkflowRequest {
|
|
|
196
196
|
/** (optional) the additional metadata for the workflow */
|
|
197
197
|
additionalMetadata?: string | undefined;
|
|
198
198
|
/** (optional) the priority of the workflow */
|
|
199
|
-
priority
|
|
199
|
+
priority?: number | undefined;
|
|
200
200
|
}
|
|
201
201
|
/** ScheduledWorkflow represents a scheduled workflow. */
|
|
202
202
|
export interface ScheduledWorkflow {
|
|
@@ -1409,7 +1409,7 @@ function createBaseScheduleWorkflowRequest() {
|
|
|
1409
1409
|
childIndex: undefined,
|
|
1410
1410
|
childKey: undefined,
|
|
1411
1411
|
additionalMetadata: undefined,
|
|
1412
|
-
priority:
|
|
1412
|
+
priority: undefined,
|
|
1413
1413
|
};
|
|
1414
1414
|
}
|
|
1415
1415
|
exports.ScheduleWorkflowRequest = {
|
|
@@ -1438,7 +1438,7 @@ exports.ScheduleWorkflowRequest = {
|
|
|
1438
1438
|
if (message.additionalMetadata !== undefined) {
|
|
1439
1439
|
writer.uint32(66).string(message.additionalMetadata);
|
|
1440
1440
|
}
|
|
1441
|
-
if (message.priority !==
|
|
1441
|
+
if (message.priority !== undefined) {
|
|
1442
1442
|
writer.uint32(72).int32(message.priority);
|
|
1443
1443
|
}
|
|
1444
1444
|
return writer;
|
|
@@ -1537,7 +1537,7 @@ exports.ScheduleWorkflowRequest = {
|
|
|
1537
1537
|
additionalMetadata: isSet(object.additionalMetadata)
|
|
1538
1538
|
? globalThis.String(object.additionalMetadata)
|
|
1539
1539
|
: undefined,
|
|
1540
|
-
priority: isSet(object.priority) ? globalThis.Number(object.priority) :
|
|
1540
|
+
priority: isSet(object.priority) ? globalThis.Number(object.priority) : undefined,
|
|
1541
1541
|
};
|
|
1542
1542
|
},
|
|
1543
1543
|
toJSON(message) {
|
|
@@ -1567,7 +1567,7 @@ exports.ScheduleWorkflowRequest = {
|
|
|
1567
1567
|
if (message.additionalMetadata !== undefined) {
|
|
1568
1568
|
obj.additionalMetadata = message.additionalMetadata;
|
|
1569
1569
|
}
|
|
1570
|
-
if (message.priority !==
|
|
1570
|
+
if (message.priority !== undefined) {
|
|
1571
1571
|
obj.priority = Math.round(message.priority);
|
|
1572
1572
|
}
|
|
1573
1573
|
return obj;
|
|
@@ -1586,7 +1586,7 @@ exports.ScheduleWorkflowRequest = {
|
|
|
1586
1586
|
message.childIndex = (_f = object.childIndex) !== null && _f !== void 0 ? _f : undefined;
|
|
1587
1587
|
message.childKey = (_g = object.childKey) !== null && _g !== void 0 ? _g : undefined;
|
|
1588
1588
|
message.additionalMetadata = (_h = object.additionalMetadata) !== null && _h !== void 0 ? _h : undefined;
|
|
1589
|
-
message.priority = (_j = object.priority) !== null && _j !== void 0 ? _j :
|
|
1589
|
+
message.priority = (_j = object.priority) !== null && _j !== void 0 ? _j : undefined;
|
|
1590
1590
|
return message;
|
|
1591
1591
|
},
|
|
1592
1592
|
};
|
package/step.d.ts
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import * as z from 'zod';
|
|
2
|
+
import { JsonObject } from '@bufbuild/protobuf';
|
|
2
3
|
import { Workflow } from './workflow';
|
|
3
4
|
import { Action } from './clients/dispatcher/action-listener';
|
|
4
5
|
import { LogLevel } from './clients/event/event-client';
|
|
5
6
|
import { Logger } from './util/logger';
|
|
6
|
-
import { InternalHatchetClient } from './clients/hatchet-client';
|
|
7
7
|
import WorkflowRunRef from './util/workflow-run-ref';
|
|
8
|
-
import { V0Worker } from './clients/worker';
|
|
9
8
|
import { WorkerLabels } from './clients/dispatcher/dispatcher-client';
|
|
10
9
|
import { CreateStepRateLimit, RateLimitDuration, WorkerLabelComparator } from './protoc/workflows';
|
|
11
10
|
import { CreateWorkflowTaskOpts, Priority } from './v1';
|
|
12
11
|
import { RunOpts, TaskWorkflowDeclaration, BaseWorkflowDeclaration as WorkflowV1 } from './v1/declaration';
|
|
13
12
|
import { Conditions } from './v1/conditions';
|
|
14
13
|
import { Duration } from './v1/client/duration';
|
|
15
|
-
import {
|
|
14
|
+
import { JsonValue, OutputType } from './v1/types';
|
|
15
|
+
import { V1Worker } from './v1/client/worker/worker-internal';
|
|
16
|
+
import { V0Worker } from './clients/worker';
|
|
17
|
+
import { LegacyHatchetClient } from './clients/hatchet-client';
|
|
16
18
|
export declare const CreateRateLimitSchema: z.ZodObject<{
|
|
17
19
|
key: z.ZodOptional<z.ZodString>;
|
|
18
20
|
staticKey: z.ZodOptional<z.ZodString>;
|
|
@@ -169,7 +171,7 @@ interface ContextData<T, K> {
|
|
|
169
171
|
}
|
|
170
172
|
export declare class ContextWorker {
|
|
171
173
|
private worker;
|
|
172
|
-
constructor(worker: V0Worker);
|
|
174
|
+
constructor(worker: V0Worker | V1Worker);
|
|
173
175
|
/**
|
|
174
176
|
* Gets the ID of the worker.
|
|
175
177
|
* @returns The ID of the worker.
|
|
@@ -193,17 +195,17 @@ export declare class ContextWorker {
|
|
|
193
195
|
*/
|
|
194
196
|
upsertLabels(labels: WorkerLabels): Promise<WorkerLabels>;
|
|
195
197
|
}
|
|
196
|
-
export declare class
|
|
198
|
+
export declare class V0Context<T, K = {}> {
|
|
197
199
|
data: ContextData<T, K>;
|
|
198
200
|
input: T;
|
|
199
201
|
controller: AbortController;
|
|
200
202
|
action: Action;
|
|
201
|
-
|
|
203
|
+
v0: LegacyHatchetClient;
|
|
202
204
|
worker: ContextWorker;
|
|
203
205
|
overridesData: Record<string, any>;
|
|
204
206
|
logger: Logger;
|
|
205
207
|
spawnIndex: number;
|
|
206
|
-
constructor(action: Action, client:
|
|
208
|
+
constructor(action: Action, client: LegacyHatchetClient, worker: V0Worker | V1Worker);
|
|
207
209
|
get abortController(): AbortController;
|
|
208
210
|
get cancelled(): boolean;
|
|
209
211
|
/**
|
|
@@ -359,7 +361,7 @@ export declare class Context<T, K = {}> {
|
|
|
359
361
|
* @param options - An options object containing key, sticky, priority, and additionalMetadata.
|
|
360
362
|
* @returns A reference to the spawned workflow run.
|
|
361
363
|
*/
|
|
362
|
-
runNoWaitChild<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P>, input: Q, options?: ChildRunOpts): WorkflowRunRef<P
|
|
364
|
+
runNoWaitChild<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P>, input: Q, options?: ChildRunOpts): Promise<WorkflowRunRef<P>>;
|
|
363
365
|
/**
|
|
364
366
|
* Spawns a new workflow.
|
|
365
367
|
*
|
|
@@ -369,7 +371,7 @@ export declare class Context<T, K = {}> {
|
|
|
369
371
|
* @returns A reference to the spawned workflow run.
|
|
370
372
|
* @deprecated Use runChild or runNoWaitChild instead.
|
|
371
373
|
*/
|
|
372
|
-
spawnWorkflow<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P> | TaskWorkflowDeclaration<Q, P>, input: Q, options?: ChildRunOpts): WorkflowRunRef<P
|
|
374
|
+
spawnWorkflow<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P> | TaskWorkflowDeclaration<Q, P>, input: Q, options?: ChildRunOpts): Promise<WorkflowRunRef<P>>;
|
|
373
375
|
/**
|
|
374
376
|
* Retrieves additional metadata associated with the current workflow run.
|
|
375
377
|
* @returns A record of metadata key-value pairs.
|
|
@@ -392,7 +394,7 @@ export declare class Context<T, K = {}> {
|
|
|
392
394
|
parentWorkflowRunId(): string | undefined;
|
|
393
395
|
priority(): Priority | undefined;
|
|
394
396
|
}
|
|
395
|
-
export declare class
|
|
397
|
+
export declare class V0DurableContext<T, K = {}> extends V0Context<T, K> {
|
|
396
398
|
waitKey: number;
|
|
397
399
|
/**
|
|
398
400
|
* Pauses execution for the specified duration.
|
|
@@ -409,7 +411,7 @@ export declare class DurableContext<T, K = {}> extends Context<T, K> {
|
|
|
409
411
|
*/
|
|
410
412
|
waitFor(conditions: Conditions | Conditions[]): Promise<Record<string, any>>;
|
|
411
413
|
}
|
|
412
|
-
export type StepRunFunction<T, K> = (ctx:
|
|
414
|
+
export type StepRunFunction<T, K> = (ctx: V0Context<T, K>) => Promise<NextStep | void> | NextStep | void;
|
|
413
415
|
/**
|
|
414
416
|
* A step is a unit of work that can be run by a worker.
|
|
415
417
|
* It is defined by a name, a function that returns the next step, and optional configuration.
|