@hatchet-dev/typescript-sdk 0.20.3 → 1.0.0-alpha0
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/hatchet-client/features/cron-client.js +0 -1
- package/clients/hatchet-client/hatchet-client.d.ts +8 -7
- package/clients/hatchet-client/hatchet-client.js +9 -8
- package/clients/hatchet-client/hatchet-logger.js +1 -17
- package/clients/worker/handler.d.ts +2 -2
- package/clients/worker/worker.d.ts +4 -4
- package/clients/worker/worker.js +14 -28
- package/examples/api.js +1 -1
- package/examples/example-event-with-results.js +1 -1
- package/examples/stream-by-additional-meta.js +1 -1
- package/index.d.ts +3 -1
- package/index.js +5 -3
- package/package.json +1 -1
- package/sdk.d.ts +1 -1
- package/sdk.js +2 -2
- package/step.d.ts +75 -9
- package/step.js +65 -2
- package/util/hatchet-promise/hatchet-promise.js +1 -0
- package/v1/client/client.d.ts +81 -0
- package/v1/client/client.interface.d.ts +4 -0
- package/v1/client/client.interface.js +2 -0
- package/v1/client/client.js +134 -0
- package/v1/client/worker.d.ts +82 -0
- package/v1/client/worker.js +125 -0
- package/v1/examples/child_workflows/run.d.ts +1 -0
- package/v1/examples/child_workflows/run.js +30 -0
- package/v1/examples/child_workflows/worker.d.ts +1 -0
- package/v1/examples/child_workflows/worker.js +24 -0
- package/v1/examples/child_workflows/workflow.d.ts +19 -0
- package/v1/examples/child_workflows/workflow.js +43 -0
- package/v1/examples/client.d.ts +2 -0
- package/v1/examples/client.js +5 -0
- package/v1/examples/concurrency-rr/load.d.ts +1 -0
- package/v1/examples/concurrency-rr/load.js +54 -0
- package/v1/examples/concurrency-rr/run.d.ts +1 -0
- package/v1/examples/concurrency-rr/run.js +39 -0
- package/v1/examples/concurrency-rr/worker.d.ts +1 -0
- package/v1/examples/concurrency-rr/worker.js +24 -0
- package/v1/examples/concurrency-rr/workflow.d.ts +11 -0
- package/v1/examples/concurrency-rr/workflow.js +35 -0
- package/v1/examples/dag/run.d.ts +1 -0
- package/v1/examples/dag/run.js +24 -0
- package/v1/examples/dag/worker.d.ts +1 -0
- package/v1/examples/dag/worker.js +24 -0
- package/v1/examples/dag/workflow.d.ts +11 -0
- package/v1/examples/dag/workflow.js +35 -0
- package/v1/examples/deep/run.d.ts +1 -0
- package/v1/examples/deep/run.js +28 -0
- package/v1/examples/deep/worker.d.ts +1 -0
- package/v1/examples/deep/worker.js +25 -0
- package/v1/examples/deep/workflow.d.ts +18 -0
- package/v1/examples/deep/workflow.js +110 -0
- package/v1/examples/legacy/run.d.ts +1 -0
- package/v1/examples/legacy/run.js +25 -0
- package/v1/examples/legacy/worker.d.ts +1 -0
- package/v1/examples/legacy/worker.js +24 -0
- package/v1/examples/legacy/workflow.d.ts +2 -0
- package/v1/examples/legacy/workflow.js +36 -0
- package/v1/examples/on_event/event.d.ts +1 -0
- package/v1/examples/on_event/event.js +25 -0
- package/v1/examples/on_event/worker.d.ts +1 -0
- package/v1/examples/on_event/worker.js +24 -0
- package/v1/examples/on_event/workflow.d.ts +17 -0
- package/v1/examples/on_event/workflow.js +33 -0
- package/v1/examples/simple/cron.d.ts +1 -0
- package/v1/examples/simple/cron.js +26 -0
- package/v1/examples/simple/delay.d.ts +1 -0
- package/v1/examples/simple/delay.js +27 -0
- package/v1/examples/simple/run.d.ts +1 -0
- package/v1/examples/simple/run.js +24 -0
- package/v1/examples/simple/schedule.d.ts +1 -0
- package/v1/examples/simple/schedule.js +27 -0
- package/v1/examples/simple/worker.d.ts +1 -0
- package/v1/examples/simple/worker.js +24 -0
- package/v1/examples/simple/workflow.d.ts +5 -0
- package/v1/examples/simple/workflow.js +15 -0
- package/v1/index.d.ts +0 -0
- package/v1/index.js +1 -0
- package/v1/task.d.ts +51 -0
- package/v1/task.js +2 -0
- package/v1/workflow.d.ts +152 -0
- package/v1/workflow.js +145 -0
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/workflow.d.ts +3 -0
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
class HatchetPromise {
|
|
4
4
|
constructor(promise) {
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
5
6
|
this.cancel = (reason) => { };
|
|
6
7
|
this.promise = new Promise((resolve, reject) => {
|
|
7
8
|
this.cancel = reject;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ClientConfig, InternalHatchetClient, HatchetClientOptions } from '../../clients/hatchet-client';
|
|
2
|
+
import { AxiosRequestConfig } from 'axios';
|
|
3
|
+
import WorkflowRunRef from '../../util/workflow-run-ref';
|
|
4
|
+
import { Workflow as V0Workflow } from '../../workflow';
|
|
5
|
+
import { CreateWorkflowOpts, RunOpts, Workflow } from '../workflow';
|
|
6
|
+
import { IHatchetClient } from './client.interface';
|
|
7
|
+
import { CreateWorkerOpts, Worker } from './worker';
|
|
8
|
+
/**
|
|
9
|
+
* HatchetV1 implements the main client interface for interacting with the Hatchet workflow engine.
|
|
10
|
+
* It provides methods for creating and executing workflows, as well as managing workers.
|
|
11
|
+
*/
|
|
12
|
+
export declare class HatchetClient implements IHatchetClient {
|
|
13
|
+
/** The underlying v0 client instance */
|
|
14
|
+
v0: InternalHatchetClient;
|
|
15
|
+
/** The tenant ID for the Hatchet client */
|
|
16
|
+
get tenantId(): string;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new Hatchet client instance.
|
|
19
|
+
* @param config - Optional configuration for the client
|
|
20
|
+
* @param options - Optional client options
|
|
21
|
+
* @param axiosConfig - Optional Axios configuration for HTTP requests
|
|
22
|
+
*/
|
|
23
|
+
constructor(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosConfig?: AxiosRequestConfig);
|
|
24
|
+
/**
|
|
25
|
+
* Static factory method to create a new Hatchet client instance.
|
|
26
|
+
* @param config - Optional configuration for the client
|
|
27
|
+
* @param options - Optional client options
|
|
28
|
+
* @param axiosConfig - Optional Axios configuration for HTTP requests
|
|
29
|
+
* @returns A new Hatchet client instance
|
|
30
|
+
*/
|
|
31
|
+
static init(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosConfig?: AxiosRequestConfig): HatchetClient;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new workflow definition.
|
|
34
|
+
* @template T - The input type for the workflow
|
|
35
|
+
* @template K - The return type of the workflow
|
|
36
|
+
* @param options - Configuration options for creating the workflow
|
|
37
|
+
* @returns A new Workflow instance
|
|
38
|
+
* @note It is possible to create an orphaned workflow if no client is available using @hatchet/client CreateWorkflow
|
|
39
|
+
*/
|
|
40
|
+
workflow<T extends Record<string, any> = any, K = any>(options: CreateWorkflowOpts): Workflow<T, K>;
|
|
41
|
+
/**
|
|
42
|
+
* Triggers a workflow run without waiting for completion.
|
|
43
|
+
* @template T - The input type for the workflow
|
|
44
|
+
* @template K - The return type of the workflow
|
|
45
|
+
* @param workflow - The workflow to run, either as a Workflow instance or workflow name
|
|
46
|
+
* @param input - The input data for the workflow
|
|
47
|
+
* @param options - Configuration options for the workflow run
|
|
48
|
+
* @returns A WorkflowRunRef containing the run ID and methods to interact with the run
|
|
49
|
+
*/
|
|
50
|
+
enqueue<T extends Record<string, any> = any, K = any>(workflow: Workflow<T, K> | string | V0Workflow, input: T, options: RunOpts): WorkflowRunRef<K>;
|
|
51
|
+
/**
|
|
52
|
+
* Triggers a workflow run and waits for the result.
|
|
53
|
+
* @template T - The input type for the workflow
|
|
54
|
+
* @template K - The return type of the workflow
|
|
55
|
+
* @param workflow - The workflow to run, either as a Workflow instance or workflow name
|
|
56
|
+
* @param input - The input data for the workflow
|
|
57
|
+
* @param options - Configuration options for the workflow run
|
|
58
|
+
* @returns A promise that resolves with the workflow result
|
|
59
|
+
*/
|
|
60
|
+
run<T extends Record<string, any> = any, K = any>(workflow: Workflow<T, K> | string | V0Workflow, input: T, options?: RunOpts): Promise<K>;
|
|
61
|
+
get cron(): import("../../clients/hatchet-client/features/cron-client").CronClient;
|
|
62
|
+
get schedule(): import("../../clients/hatchet-client/features/schedule-client").ScheduleClient;
|
|
63
|
+
get event(): import("../../clients/event/event-client").EventClient;
|
|
64
|
+
get api(): import("../..").Api<unknown>;
|
|
65
|
+
/**
|
|
66
|
+
* @deprecated use workflow.run or client.run instead
|
|
67
|
+
*/
|
|
68
|
+
get admin(): import("../..").AdminClient;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a new worker instance for processing workflow tasks.
|
|
71
|
+
* @param options - Configuration options for creating the worker
|
|
72
|
+
* @returns A promise that resolves with a new HatchetWorker instance
|
|
73
|
+
*/
|
|
74
|
+
worker(name: string, options?: CreateWorkerOpts | number): Promise<Worker>;
|
|
75
|
+
/**
|
|
76
|
+
* Register a webhook with the worker
|
|
77
|
+
* @param workflows - The workflows to register on the webhooks
|
|
78
|
+
* @returns A promise that resolves when the webhook is registered
|
|
79
|
+
*/
|
|
80
|
+
webhooks(workflows: Workflow<any, any>[] | V0Workflow[]): import("../../clients/worker/handler").WebhookHandler;
|
|
81
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.HatchetClient = void 0;
|
|
13
|
+
const hatchet_client_1 = require("../../clients/hatchet-client");
|
|
14
|
+
const workflow_1 = require("../workflow");
|
|
15
|
+
const worker_1 = require("./worker");
|
|
16
|
+
/**
|
|
17
|
+
* HatchetV1 implements the main client interface for interacting with the Hatchet workflow engine.
|
|
18
|
+
* It provides methods for creating and executing workflows, as well as managing workers.
|
|
19
|
+
*/
|
|
20
|
+
class HatchetClient {
|
|
21
|
+
/** The tenant ID for the Hatchet client */
|
|
22
|
+
get tenantId() {
|
|
23
|
+
return this.v0.tenantId;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new Hatchet client instance.
|
|
27
|
+
* @param config - Optional configuration for the client
|
|
28
|
+
* @param options - Optional client options
|
|
29
|
+
* @param axiosConfig - Optional Axios configuration for HTTP requests
|
|
30
|
+
*/
|
|
31
|
+
constructor(config, options, axiosConfig) {
|
|
32
|
+
this.v0 = new hatchet_client_1.InternalHatchetClient(config, options, axiosConfig);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Static factory method to create a new Hatchet client instance.
|
|
36
|
+
* @param config - Optional configuration for the client
|
|
37
|
+
* @param options - Optional client options
|
|
38
|
+
* @param axiosConfig - Optional Axios configuration for HTTP requests
|
|
39
|
+
* @returns A new Hatchet client instance
|
|
40
|
+
*/
|
|
41
|
+
static init(config, options, axiosConfig) {
|
|
42
|
+
return new HatchetClient(config, options, axiosConfig);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new workflow definition.
|
|
46
|
+
* @template T - The input type for the workflow
|
|
47
|
+
* @template K - The return type of the workflow
|
|
48
|
+
* @param options - Configuration options for creating the workflow
|
|
49
|
+
* @returns A new Workflow instance
|
|
50
|
+
* @note It is possible to create an orphaned workflow if no client is available using @hatchet/client CreateWorkflow
|
|
51
|
+
*/
|
|
52
|
+
workflow(options) {
|
|
53
|
+
return (0, workflow_1.CreateWorkflow)(options, this);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Triggers a workflow run without waiting for completion.
|
|
57
|
+
* @template T - The input type for the workflow
|
|
58
|
+
* @template K - The return type of the workflow
|
|
59
|
+
* @param workflow - The workflow to run, either as a Workflow instance or workflow name
|
|
60
|
+
* @param input - The input data for the workflow
|
|
61
|
+
* @param options - Configuration options for the workflow run
|
|
62
|
+
* @returns A WorkflowRunRef containing the run ID and methods to interact with the run
|
|
63
|
+
*/
|
|
64
|
+
enqueue(workflow, input, options) {
|
|
65
|
+
let name;
|
|
66
|
+
if (typeof workflow === 'string') {
|
|
67
|
+
name = workflow;
|
|
68
|
+
}
|
|
69
|
+
else if ('id' in workflow) {
|
|
70
|
+
name = workflow.id;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
throw new Error('unable to identify workflow');
|
|
74
|
+
}
|
|
75
|
+
return this.v0.admin.runWorkflow(name, input, options);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Triggers a workflow run and waits for the result.
|
|
79
|
+
* @template T - The input type for the workflow
|
|
80
|
+
* @template K - The return type of the workflow
|
|
81
|
+
* @param workflow - The workflow to run, either as a Workflow instance or workflow name
|
|
82
|
+
* @param input - The input data for the workflow
|
|
83
|
+
* @param options - Configuration options for the workflow run
|
|
84
|
+
* @returns A promise that resolves with the workflow result
|
|
85
|
+
*/
|
|
86
|
+
run(workflow_2, input_1) {
|
|
87
|
+
return __awaiter(this, arguments, void 0, function* (workflow, input, options = {}) {
|
|
88
|
+
const run = this.enqueue(workflow, input, options);
|
|
89
|
+
return run.result();
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
get cron() {
|
|
93
|
+
return this.v0.cron;
|
|
94
|
+
}
|
|
95
|
+
get schedule() {
|
|
96
|
+
return this.v0.schedule;
|
|
97
|
+
}
|
|
98
|
+
get event() {
|
|
99
|
+
return this.v0.event;
|
|
100
|
+
}
|
|
101
|
+
get api() {
|
|
102
|
+
return this.v0.api;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* @deprecated use workflow.run or client.run instead
|
|
106
|
+
*/
|
|
107
|
+
get admin() {
|
|
108
|
+
return this.v0.admin;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Creates a new worker instance for processing workflow tasks.
|
|
112
|
+
* @param options - Configuration options for creating the worker
|
|
113
|
+
* @returns A promise that resolves with a new HatchetWorker instance
|
|
114
|
+
*/
|
|
115
|
+
worker(name, options) {
|
|
116
|
+
let opts = {};
|
|
117
|
+
if (typeof options === 'number') {
|
|
118
|
+
opts = { slots: options };
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
opts = options || {};
|
|
122
|
+
}
|
|
123
|
+
return worker_1.Worker.create(this.v0, name, opts);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Register a webhook with the worker
|
|
127
|
+
* @param workflows - The workflows to register on the webhooks
|
|
128
|
+
* @returns A promise that resolves when the webhook is registered
|
|
129
|
+
*/
|
|
130
|
+
webhooks(workflows) {
|
|
131
|
+
return this.v0.webhooks(workflows);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exports.HatchetClient = HatchetClient;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { WorkerLabels } from '../../clients/dispatcher/dispatcher-client';
|
|
2
|
+
import { InternalHatchetClient } from '../../clients/hatchet-client';
|
|
3
|
+
import { V0Worker } from '../../clients/worker';
|
|
4
|
+
import { Workflow as V0Workflow } from '../../workflow';
|
|
5
|
+
import { WebhookWorkerCreateRequest } from '../../clients/rest/generated/data-contracts';
|
|
6
|
+
import { Workflow } from '../workflow';
|
|
7
|
+
/**
|
|
8
|
+
* Options for creating a new hatchet worker
|
|
9
|
+
* @interface CreateWorkerOpts
|
|
10
|
+
*/
|
|
11
|
+
export interface CreateWorkerOpts {
|
|
12
|
+
/** Maximum number of concurrent runs on this worker */
|
|
13
|
+
slots?: number;
|
|
14
|
+
/** Array of workflows to register */
|
|
15
|
+
workflows?: Workflow<any, any>[] | V0Workflow[];
|
|
16
|
+
/** Worker labels for affinity-based assignment */
|
|
17
|
+
labels?: WorkerLabels;
|
|
18
|
+
/** Whether to handle kill signals */
|
|
19
|
+
handleKill?: boolean;
|
|
20
|
+
/** @deprecated Use slots instead */
|
|
21
|
+
maxRuns?: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* HatchetWorker class for workflow execution runtime
|
|
25
|
+
*/
|
|
26
|
+
export declare class Worker {
|
|
27
|
+
/** Internal reference to the underlying V0 worker implementation */
|
|
28
|
+
v0: V0Worker;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a new HatchetWorker instance
|
|
31
|
+
* @param v0 - The V0 worker implementation
|
|
32
|
+
*/
|
|
33
|
+
constructor(v0: V0Worker);
|
|
34
|
+
/**
|
|
35
|
+
* Creates and initializes a new HatchetWorker
|
|
36
|
+
* @param v0 - The HatchetClient instance
|
|
37
|
+
* @param options - Worker creation options
|
|
38
|
+
* @returns A new HatchetWorker instance
|
|
39
|
+
*/
|
|
40
|
+
static create(v0: InternalHatchetClient, name: string, options: CreateWorkerOpts): Promise<Worker>;
|
|
41
|
+
/**
|
|
42
|
+
* Registers workflows with the worker
|
|
43
|
+
* @param workflows - Array of workflows to register
|
|
44
|
+
* @returns Array of registered workflow promises
|
|
45
|
+
*/
|
|
46
|
+
registerWorkflows(workflows?: Array<Workflow<any, any> | V0Workflow>): Promise<void>[] | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Registers a single workflow with the worker
|
|
49
|
+
* @param workflow - The workflow to register
|
|
50
|
+
* @returns A promise that resolves when the workflow is registered
|
|
51
|
+
* @deprecated use registerWorkflows instead
|
|
52
|
+
*/
|
|
53
|
+
registerWorkflow(workflow: Workflow<any, any> | V0Workflow): Promise<void>[] | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Starts the worker
|
|
56
|
+
* @returns Promise that resolves when the worker is stopped or killed
|
|
57
|
+
*/
|
|
58
|
+
start(): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Stops the worker
|
|
61
|
+
* @returns Promise that resolves when the worker stops
|
|
62
|
+
*/
|
|
63
|
+
stop(): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Updates or inserts worker labels
|
|
66
|
+
* @param labels - Worker labels to update
|
|
67
|
+
* @returns Promise that resolves when labels are updated
|
|
68
|
+
*/
|
|
69
|
+
upsertLabels(labels: WorkerLabels): Promise<WorkerLabels>;
|
|
70
|
+
/**
|
|
71
|
+
* Get the labels for the worker
|
|
72
|
+
* @returns The labels for the worker
|
|
73
|
+
*/
|
|
74
|
+
getLabels(): WorkerLabels;
|
|
75
|
+
/**
|
|
76
|
+
* Register a webhook with the worker
|
|
77
|
+
* @param webhook - The webhook to register
|
|
78
|
+
* @returns A promise that resolves when the webhook is registered
|
|
79
|
+
*/
|
|
80
|
+
registerWebhook(webhook: WebhookWorkerCreateRequest): Promise<import("axios").AxiosResponse<import("../../clients/rest/generated/data-contracts").WebhookWorkerCreated, any>>;
|
|
81
|
+
}
|
|
82
|
+
export declare function toV0Workflow(wf: Workflow<any, any> | V0Workflow): V0Workflow;
|
|
@@ -0,0 +1,125 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Worker = void 0;
|
|
13
|
+
exports.toV0Workflow = toV0Workflow;
|
|
14
|
+
const workflow_1 = require("../workflow");
|
|
15
|
+
/**
|
|
16
|
+
* HatchetWorker class for workflow execution runtime
|
|
17
|
+
*/
|
|
18
|
+
class Worker {
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new HatchetWorker instance
|
|
21
|
+
* @param v0 - The V0 worker implementation
|
|
22
|
+
*/
|
|
23
|
+
constructor(v0) {
|
|
24
|
+
this.v0 = v0;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Creates and initializes a new HatchetWorker
|
|
28
|
+
* @param v0 - The HatchetClient instance
|
|
29
|
+
* @param options - Worker creation options
|
|
30
|
+
* @returns A new HatchetWorker instance
|
|
31
|
+
*/
|
|
32
|
+
static create(v0, name, options) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
const v0worker = yield v0.worker(name, Object.assign(Object.assign({}, options), { maxRuns: options.slots || options.maxRuns }));
|
|
35
|
+
const worker = new Worker(v0worker);
|
|
36
|
+
yield worker.registerWorkflows(options.workflows);
|
|
37
|
+
return worker;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Registers workflows with the worker
|
|
42
|
+
* @param workflows - Array of workflows to register
|
|
43
|
+
* @returns Array of registered workflow promises
|
|
44
|
+
*/
|
|
45
|
+
registerWorkflows(workflows) {
|
|
46
|
+
return workflows === null || workflows === void 0 ? void 0 : workflows.map((wf) => {
|
|
47
|
+
return this.v0.registerWorkflow(toV0Workflow(wf));
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Registers a single workflow with the worker
|
|
52
|
+
* @param workflow - The workflow to register
|
|
53
|
+
* @returns A promise that resolves when the workflow is registered
|
|
54
|
+
* @deprecated use registerWorkflows instead
|
|
55
|
+
*/
|
|
56
|
+
registerWorkflow(workflow) {
|
|
57
|
+
return this.registerWorkflows([workflow]);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Starts the worker
|
|
61
|
+
* @returns Promise that resolves when the worker is stopped or killed
|
|
62
|
+
*/
|
|
63
|
+
start() {
|
|
64
|
+
return this.v0.start();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Stops the worker
|
|
68
|
+
* @returns Promise that resolves when the worker stops
|
|
69
|
+
*/
|
|
70
|
+
stop() {
|
|
71
|
+
return this.v0.stop();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Updates or inserts worker labels
|
|
75
|
+
* @param labels - Worker labels to update
|
|
76
|
+
* @returns Promise that resolves when labels are updated
|
|
77
|
+
*/
|
|
78
|
+
upsertLabels(labels) {
|
|
79
|
+
return this.v0.upsertLabels(labels);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get the labels for the worker
|
|
83
|
+
* @returns The labels for the worker
|
|
84
|
+
*/
|
|
85
|
+
getLabels() {
|
|
86
|
+
return this.v0.labels;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Register a webhook with the worker
|
|
90
|
+
* @param webhook - The webhook to register
|
|
91
|
+
* @returns A promise that resolves when the webhook is registered
|
|
92
|
+
*/
|
|
93
|
+
registerWebhook(webhook) {
|
|
94
|
+
return this.v0.registerWebhook(webhook);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.Worker = Worker;
|
|
98
|
+
function toV0Workflow(wf) {
|
|
99
|
+
if (wf instanceof workflow_1.Workflow) {
|
|
100
|
+
const { definition } = wf;
|
|
101
|
+
return {
|
|
102
|
+
id: definition.name,
|
|
103
|
+
description: definition.description || '',
|
|
104
|
+
version: definition.version || '',
|
|
105
|
+
sticky: definition.sticky,
|
|
106
|
+
scheduleTimeout: definition.scheduleTimeout,
|
|
107
|
+
on: definition.on,
|
|
108
|
+
concurrency: definition.concurrency,
|
|
109
|
+
steps: definition.tasks.map((task) => {
|
|
110
|
+
var _a;
|
|
111
|
+
return ({
|
|
112
|
+
name: task.name,
|
|
113
|
+
parents: (_a = task.parents) === null || _a === void 0 ? void 0 : _a.map((p) => p.name),
|
|
114
|
+
run: (ctx) => task.fn(ctx.workflowInput(), ctx),
|
|
115
|
+
timeout: task.timeout,
|
|
116
|
+
retries: task.retries,
|
|
117
|
+
rate_limits: task.rateLimits,
|
|
118
|
+
worker_labels: task.workerLabels,
|
|
119
|
+
backoff: task.backoff,
|
|
120
|
+
});
|
|
121
|
+
}),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return wf;
|
|
125
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const workflow_1 = require("./workflow");
|
|
13
|
+
function main() {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const res = yield workflow_1.parent.run({
|
|
16
|
+
N: 10,
|
|
17
|
+
});
|
|
18
|
+
// eslint-disable-next-line no-console
|
|
19
|
+
console.log(res.sum.Result);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
if (require.main === module) {
|
|
23
|
+
main()
|
|
24
|
+
.then(() => process.exit(0))
|
|
25
|
+
.catch((error) => {
|
|
26
|
+
// eslint-disable-next-line no-console
|
|
27
|
+
console.error('Error:', error);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const client_1 = require("../client");
|
|
13
|
+
const workflow_1 = require("./workflow");
|
|
14
|
+
function main() {
|
|
15
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
+
const worker = yield client_1.hatchet.worker('child-workflow-worker', {
|
|
17
|
+
workflows: [workflow_1.parent, workflow_1.child],
|
|
18
|
+
});
|
|
19
|
+
yield worker.start();
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
if (require.main === module) {
|
|
23
|
+
main();
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type ChildInput = {
|
|
2
|
+
N: number;
|
|
3
|
+
};
|
|
4
|
+
type ChildOutput = {
|
|
5
|
+
value: {
|
|
6
|
+
Value: number;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export declare const child: import("../../workflow").Workflow<ChildInput, ChildOutput>;
|
|
10
|
+
type ParentInput = {
|
|
11
|
+
N: number;
|
|
12
|
+
};
|
|
13
|
+
type ParentOutput = {
|
|
14
|
+
sum: {
|
|
15
|
+
Result: number;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export declare const parent: import("../../workflow").Workflow<ParentInput, ParentOutput>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.parent = exports.child = void 0;
|
|
13
|
+
const client_1 = require("../client");
|
|
14
|
+
exports.child = client_1.hatchet.workflow({
|
|
15
|
+
name: 'child',
|
|
16
|
+
});
|
|
17
|
+
exports.child.task({
|
|
18
|
+
name: 'value',
|
|
19
|
+
fn: (input) => {
|
|
20
|
+
return {
|
|
21
|
+
Value: input.N,
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
exports.parent = client_1.hatchet.workflow({
|
|
26
|
+
name: 'parent',
|
|
27
|
+
});
|
|
28
|
+
exports.parent.task({
|
|
29
|
+
name: 'sum',
|
|
30
|
+
fn: (input, ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
+
const n = input.N;
|
|
32
|
+
const promises = [];
|
|
33
|
+
// eslint-disable-next-line no-plusplus
|
|
34
|
+
for (let i = 0; i < n; i++) {
|
|
35
|
+
promises.push(ctx.runChild(exports.child, { N: i }));
|
|
36
|
+
}
|
|
37
|
+
const childRes = yield Promise.all(promises);
|
|
38
|
+
const sum = childRes.reduce((acc, curr) => acc + curr.value.Value, 0);
|
|
39
|
+
return {
|
|
40
|
+
Result: sum,
|
|
41
|
+
};
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
/* eslint-disable no-plusplus */
|
|
13
|
+
const client_1 = require("../client");
|
|
14
|
+
const workflow_1 = require("./workflow");
|
|
15
|
+
function generateRandomString(length) {
|
|
16
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
17
|
+
let result = '';
|
|
18
|
+
for (let i = 0; i < length; i++) {
|
|
19
|
+
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
function main() {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const groupCount = 2;
|
|
26
|
+
const runsPerGroup = 20000;
|
|
27
|
+
const BATCH_SIZE = 400;
|
|
28
|
+
const workflowRuns = [];
|
|
29
|
+
for (let i = 0; i < groupCount; i++) {
|
|
30
|
+
for (let j = 0; j < runsPerGroup; j++) {
|
|
31
|
+
workflowRuns.push({
|
|
32
|
+
workflowName: workflow_1.simpleConcurrency.definition.name,
|
|
33
|
+
input: {
|
|
34
|
+
Message: generateRandomString(10),
|
|
35
|
+
GroupKey: `group-${i}`,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// Shuffle the workflow runs array
|
|
41
|
+
for (let i = workflowRuns.length - 1; i > 0; i--) {
|
|
42
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
43
|
+
[workflowRuns[i], workflowRuns[j]] = [workflowRuns[j], workflowRuns[i]];
|
|
44
|
+
}
|
|
45
|
+
// Process workflows in batches
|
|
46
|
+
for (let i = 0; i < workflowRuns.length; i += BATCH_SIZE) {
|
|
47
|
+
const batch = workflowRuns.slice(i, i + BATCH_SIZE);
|
|
48
|
+
yield client_1.hatchet.admin.runWorkflows(batch);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
if (require.main === module) {
|
|
53
|
+
main().then(() => process.exit(0));
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|