@hatchet-dev/typescript-sdk 0.15.0 → 0.16.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/hatchet-client/client-config.d.ts +6 -0
- package/clients/hatchet-client/client-config.js +2 -0
- package/clients/hatchet-client/hatchet-client.d.ts +2 -0
- package/clients/hatchet-client/hatchet-client.js +25 -1
- package/clients/rest/api.d.ts +3 -0
- package/clients/rest/api.js +10 -0
- package/clients/rest/generated/cloud/Api.d.ts +364 -0
- package/clients/rest/generated/cloud/Api.js +316 -0
- package/clients/rest/generated/cloud/data-contracts.d.ts +460 -0
- package/clients/rest/generated/cloud/data-contracts.js +68 -0
- package/clients/rest/generated/cloud/http-client.d.ts +41 -0
- package/clients/rest/generated/cloud/http-client.js +102 -0
- package/clients/rest/index.d.ts +2 -2
- package/clients/rest/index.js +4 -5
- package/clients/worker/compute/compute-config.d.ts +96 -0
- package/clients/worker/compute/compute-config.js +52 -0
- package/clients/worker/compute/managed-compute.d.ts +15 -0
- package/clients/worker/compute/managed-compute.js +100 -0
- package/clients/worker/worker.d.ts +10 -2
- package/clients/worker/worker.js +31 -10
- package/examples/managed-worker.d.ts +1 -0
- package/examples/managed-worker.js +65 -0
- package/package.json +4 -1
- package/step.d.ts +75 -0
- package/step.js +2 -0
- package/util/config-loader/config-loader.js +19 -1
- package/workflow.d.ts +285 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { ManagedWorkerRegion } from '../../rest/generated/cloud/data-contracts';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export declare const SharedCPUComputeSchema: z.ZodObject<z.objectUtil.extendShape<{
|
|
4
|
+
pool: z.ZodOptional<z.ZodDefault<z.ZodString>>;
|
|
5
|
+
numReplicas: z.ZodDefault<z.ZodNumber>;
|
|
6
|
+
regions: z.ZodOptional<z.ZodArray<z.ZodNativeEnum<typeof ManagedWorkerRegion>, "many">>;
|
|
7
|
+
cpus: z.ZodNumber;
|
|
8
|
+
}, {
|
|
9
|
+
cpuKind: z.ZodLiteral<"shared">;
|
|
10
|
+
memoryMb: z.ZodEffects<z.ZodNumber, number, number>;
|
|
11
|
+
}>, "strip", z.ZodTypeAny, {
|
|
12
|
+
numReplicas: number;
|
|
13
|
+
cpus: number;
|
|
14
|
+
cpuKind: "shared";
|
|
15
|
+
memoryMb: number;
|
|
16
|
+
pool?: string | undefined;
|
|
17
|
+
regions?: ManagedWorkerRegion[] | undefined;
|
|
18
|
+
}, {
|
|
19
|
+
cpus: number;
|
|
20
|
+
cpuKind: "shared";
|
|
21
|
+
memoryMb: number;
|
|
22
|
+
pool?: string | undefined;
|
|
23
|
+
numReplicas?: number | undefined;
|
|
24
|
+
regions?: ManagedWorkerRegion[] | undefined;
|
|
25
|
+
}>;
|
|
26
|
+
export type SharedCPUCompute = z.infer<typeof SharedCPUComputeSchema>;
|
|
27
|
+
export declare const DedicatedCPUComputeSchema: z.ZodObject<z.objectUtil.extendShape<{
|
|
28
|
+
pool: z.ZodOptional<z.ZodDefault<z.ZodString>>;
|
|
29
|
+
numReplicas: z.ZodDefault<z.ZodNumber>;
|
|
30
|
+
regions: z.ZodOptional<z.ZodArray<z.ZodNativeEnum<typeof ManagedWorkerRegion>, "many">>;
|
|
31
|
+
cpus: z.ZodNumber;
|
|
32
|
+
}, {
|
|
33
|
+
cpuKind: z.ZodLiteral<"dedicated">;
|
|
34
|
+
memoryMb: z.ZodEffects<z.ZodNumber, number, number>;
|
|
35
|
+
}>, "strip", z.ZodTypeAny, {
|
|
36
|
+
numReplicas: number;
|
|
37
|
+
cpus: number;
|
|
38
|
+
cpuKind: "dedicated";
|
|
39
|
+
memoryMb: number;
|
|
40
|
+
pool?: string | undefined;
|
|
41
|
+
regions?: ManagedWorkerRegion[] | undefined;
|
|
42
|
+
}, {
|
|
43
|
+
cpus: number;
|
|
44
|
+
cpuKind: "dedicated";
|
|
45
|
+
memoryMb: number;
|
|
46
|
+
pool?: string | undefined;
|
|
47
|
+
numReplicas?: number | undefined;
|
|
48
|
+
regions?: ManagedWorkerRegion[] | undefined;
|
|
49
|
+
}>;
|
|
50
|
+
export type DedicatedCPUCompute = z.infer<typeof DedicatedCPUComputeSchema>;
|
|
51
|
+
export declare const ComputeSchema: z.ZodUnion<[z.ZodObject<z.objectUtil.extendShape<{
|
|
52
|
+
pool: z.ZodOptional<z.ZodDefault<z.ZodString>>;
|
|
53
|
+
numReplicas: z.ZodDefault<z.ZodNumber>;
|
|
54
|
+
regions: z.ZodOptional<z.ZodArray<z.ZodNativeEnum<typeof ManagedWorkerRegion>, "many">>;
|
|
55
|
+
cpus: z.ZodNumber;
|
|
56
|
+
}, {
|
|
57
|
+
cpuKind: z.ZodLiteral<"shared">;
|
|
58
|
+
memoryMb: z.ZodEffects<z.ZodNumber, number, number>;
|
|
59
|
+
}>, "strip", z.ZodTypeAny, {
|
|
60
|
+
numReplicas: number;
|
|
61
|
+
cpus: number;
|
|
62
|
+
cpuKind: "shared";
|
|
63
|
+
memoryMb: number;
|
|
64
|
+
pool?: string | undefined;
|
|
65
|
+
regions?: ManagedWorkerRegion[] | undefined;
|
|
66
|
+
}, {
|
|
67
|
+
cpus: number;
|
|
68
|
+
cpuKind: "shared";
|
|
69
|
+
memoryMb: number;
|
|
70
|
+
pool?: string | undefined;
|
|
71
|
+
numReplicas?: number | undefined;
|
|
72
|
+
regions?: ManagedWorkerRegion[] | undefined;
|
|
73
|
+
}>, z.ZodObject<z.objectUtil.extendShape<{
|
|
74
|
+
pool: z.ZodOptional<z.ZodDefault<z.ZodString>>;
|
|
75
|
+
numReplicas: z.ZodDefault<z.ZodNumber>;
|
|
76
|
+
regions: z.ZodOptional<z.ZodArray<z.ZodNativeEnum<typeof ManagedWorkerRegion>, "many">>;
|
|
77
|
+
cpus: z.ZodNumber;
|
|
78
|
+
}, {
|
|
79
|
+
cpuKind: z.ZodLiteral<"dedicated">;
|
|
80
|
+
memoryMb: z.ZodEffects<z.ZodNumber, number, number>;
|
|
81
|
+
}>, "strip", z.ZodTypeAny, {
|
|
82
|
+
numReplicas: number;
|
|
83
|
+
cpus: number;
|
|
84
|
+
cpuKind: "dedicated";
|
|
85
|
+
memoryMb: number;
|
|
86
|
+
pool?: string | undefined;
|
|
87
|
+
regions?: ManagedWorkerRegion[] | undefined;
|
|
88
|
+
}, {
|
|
89
|
+
cpus: number;
|
|
90
|
+
cpuKind: "dedicated";
|
|
91
|
+
memoryMb: number;
|
|
92
|
+
pool?: string | undefined;
|
|
93
|
+
numReplicas?: number | undefined;
|
|
94
|
+
regions?: ManagedWorkerRegion[] | undefined;
|
|
95
|
+
}>]>;
|
|
96
|
+
export declare const computeHash: (compute: z.infer<typeof ComputeSchema>) => string;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeHash = exports.ComputeSchema = exports.DedicatedCPUComputeSchema = exports.SharedCPUComputeSchema = void 0;
|
|
4
|
+
const data_contracts_1 = require("../../rest/generated/cloud/data-contracts");
|
|
5
|
+
const crypto_1 = require("crypto");
|
|
6
|
+
const zod_1 = require("zod");
|
|
7
|
+
const BaseComputeSchema = zod_1.z.object({
|
|
8
|
+
pool: zod_1.z.string().default('default').optional().describe('The name of the compute pool to use'),
|
|
9
|
+
numReplicas: zod_1.z
|
|
10
|
+
.number()
|
|
11
|
+
.int()
|
|
12
|
+
.min(0, { message: 'Must be at least 0' })
|
|
13
|
+
.max(1000, { message: 'Must be at most 1000' })
|
|
14
|
+
.default(1)
|
|
15
|
+
.describe('The number of replicas'),
|
|
16
|
+
regions: zod_1.z
|
|
17
|
+
.array(zod_1.z.nativeEnum(data_contracts_1.ManagedWorkerRegion))
|
|
18
|
+
.optional()
|
|
19
|
+
.describe('The regions to deploy the worker to'),
|
|
20
|
+
cpus: zod_1.z
|
|
21
|
+
.number()
|
|
22
|
+
.int()
|
|
23
|
+
.min(1, { message: 'Must be at least 1' })
|
|
24
|
+
.max(64, { message: 'Must be at most 64' })
|
|
25
|
+
.describe('The number of CPUs to use for the worker'),
|
|
26
|
+
});
|
|
27
|
+
exports.SharedCPUComputeSchema = BaseComputeSchema.extend({
|
|
28
|
+
cpuKind: zod_1.z.literal('shared'),
|
|
29
|
+
memoryMb: zod_1.z
|
|
30
|
+
.number()
|
|
31
|
+
.int()
|
|
32
|
+
.min(256, { message: 'Must be at least 256 MB' })
|
|
33
|
+
.max(65536, { message: 'Must be at most 65536 MB' })
|
|
34
|
+
.refine((val) => val % 256 === 0, { message: 'Must be divisible by 256 MB' })
|
|
35
|
+
.describe('The amount of memory in MB to use for the worker'),
|
|
36
|
+
});
|
|
37
|
+
exports.DedicatedCPUComputeSchema = BaseComputeSchema.extend({
|
|
38
|
+
cpuKind: zod_1.z.literal('dedicated'),
|
|
39
|
+
memoryMb: zod_1.z
|
|
40
|
+
.number()
|
|
41
|
+
.int()
|
|
42
|
+
.min(2048, { message: 'Must be at least 1024 MB' })
|
|
43
|
+
.max(65536, { message: 'Must be at most 65536 MB' })
|
|
44
|
+
.refine((val) => val % 256 === 0, { message: 'Must be divisible by 256 MB' })
|
|
45
|
+
.describe('The amount of memory in MB to use for the worker'),
|
|
46
|
+
});
|
|
47
|
+
exports.ComputeSchema = zod_1.z.union([exports.SharedCPUComputeSchema, exports.DedicatedCPUComputeSchema]);
|
|
48
|
+
const computeHash = (compute) => {
|
|
49
|
+
const str = JSON.stringify(compute);
|
|
50
|
+
return (0, crypto_1.createHash)('sha256').update(str).digest('hex');
|
|
51
|
+
};
|
|
52
|
+
exports.computeHash = computeHash;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { HatchetClient } from '../../hatchet-client';
|
|
2
|
+
import { CreateManagedWorkerRuntimeConfigRequest } from '../../rest/generated/cloud/data-contracts';
|
|
3
|
+
import { Logger } from '../../../util/logger';
|
|
4
|
+
import { ActionRegistry } from '../worker';
|
|
5
|
+
export declare class ManagedCompute {
|
|
6
|
+
actions: ActionRegistry;
|
|
7
|
+
client: HatchetClient;
|
|
8
|
+
maxRuns: number;
|
|
9
|
+
configs: CreateManagedWorkerRuntimeConfigRequest[];
|
|
10
|
+
cloudRegisterId: string | undefined;
|
|
11
|
+
logger: Logger;
|
|
12
|
+
constructor(actions: ActionRegistry, client: HatchetClient, maxRuns?: number);
|
|
13
|
+
getComputeConfigs(actions: ActionRegistry): CreateManagedWorkerRuntimeConfigRequest[];
|
|
14
|
+
cloudRegister(): Promise<void>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
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.ManagedCompute = void 0;
|
|
13
|
+
const logger_1 = require("../../../util/logger");
|
|
14
|
+
const compute_config_1 = require("./compute-config");
|
|
15
|
+
class ManagedCompute {
|
|
16
|
+
constructor(actions, client, maxRuns = 1) {
|
|
17
|
+
this.actions = actions;
|
|
18
|
+
this.client = client;
|
|
19
|
+
this.maxRuns = maxRuns;
|
|
20
|
+
this.configs = this.getComputeConfigs(this.actions);
|
|
21
|
+
this.cloudRegisterId = this.client.config.cloud_register_id;
|
|
22
|
+
this.logger = new logger_1.Logger(`Compute`, this.client.config.log_level);
|
|
23
|
+
if (this.configs.length === 0) {
|
|
24
|
+
this.logger.debug('No compute configs found, skipping cloud registration and running all actions locally.');
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (!this.cloudRegisterId) {
|
|
28
|
+
this.logger.warn('Managed cloud compute plan:');
|
|
29
|
+
this.configs.forEach((compute) => {
|
|
30
|
+
var _a;
|
|
31
|
+
this.logger.warn(' ----------------------------');
|
|
32
|
+
this.logger.warn(` actions: ${(_a = compute.actions) === null || _a === void 0 ? void 0 : _a.join(', ')}`);
|
|
33
|
+
this.logger.warn(` num replicas: ${compute.numReplicas}`);
|
|
34
|
+
this.logger.warn(` cpu kind: ${compute.cpuKind}`);
|
|
35
|
+
this.logger.warn(` cpus: ${compute.cpus}`);
|
|
36
|
+
this.logger.warn(` memory mb: ${compute.memoryMb}`);
|
|
37
|
+
// this.logger.warn(` regions: ${compute.regions?.join(', ')}`);
|
|
38
|
+
});
|
|
39
|
+
this.logger.warn('NOTICE: local mode detected, skipping cloud registration and running all actions locally.');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
getComputeConfigs(actions) {
|
|
43
|
+
/**
|
|
44
|
+
* Builds a map of compute hashes to compute configs and lists of actions that correspond to each compute hash.
|
|
45
|
+
*/
|
|
46
|
+
const computeMap = {};
|
|
47
|
+
try {
|
|
48
|
+
Object.entries(actions).forEach(([action, { compute }]) => {
|
|
49
|
+
if (!compute) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const key = (0, compute_config_1.computeHash)(compute);
|
|
53
|
+
if (!computeMap[key]) {
|
|
54
|
+
computeMap[key] = {
|
|
55
|
+
actions: [],
|
|
56
|
+
numReplicas: compute.numReplicas,
|
|
57
|
+
cpuKind: compute.cpuKind,
|
|
58
|
+
cpus: compute.cpus,
|
|
59
|
+
memoryMb: compute.memoryMb,
|
|
60
|
+
regions: compute.regions,
|
|
61
|
+
slots: this.maxRuns,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
computeMap[key].actions.push(action);
|
|
65
|
+
});
|
|
66
|
+
return Object.values(computeMap);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
this.logger.error(`Error getting compute configs: ${e}`);
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
cloudRegister() {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
/**
|
|
76
|
+
* Registers the compute plan with the cloud if the environment variable is set.
|
|
77
|
+
* Exits the process upon completion.
|
|
78
|
+
*/
|
|
79
|
+
if (this.cloudRegisterId) {
|
|
80
|
+
this.logger.info(`Registering cloud compute plan with ID: ${this.cloudRegisterId}`);
|
|
81
|
+
try {
|
|
82
|
+
if (this.configs.length === 0) {
|
|
83
|
+
this.logger.warn('No actions to register, skipping cloud registration.');
|
|
84
|
+
process.exit(0);
|
|
85
|
+
}
|
|
86
|
+
const req = {
|
|
87
|
+
runtimeConfigs: this.configs,
|
|
88
|
+
};
|
|
89
|
+
yield this.client.cloudApi.infraAsCodeCreate(this.cloudRegisterId, req);
|
|
90
|
+
process.exit(0);
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
this.logger.error(`ERROR: ${e}`);
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
exports.ManagedCompute = ManagedCompute;
|
|
@@ -6,9 +6,16 @@ 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 { z } from 'zod';
|
|
9
10
|
import { Context, StepRunFunction } from '../../step';
|
|
10
11
|
import { WorkerLabels } from '../dispatcher/dispatcher-client';
|
|
11
|
-
|
|
12
|
+
import { ComputeSchema } from './compute/compute-config';
|
|
13
|
+
type ActionFunction = StepRunFunction<any, any> | ((ctx: any) => string | Promise<string>);
|
|
14
|
+
type ActionRegistryRecord = {
|
|
15
|
+
func: ActionFunction;
|
|
16
|
+
compute?: z.infer<typeof ComputeSchema>;
|
|
17
|
+
};
|
|
18
|
+
export type ActionRegistry = Record<Action['actionId'], ActionRegistryRecord>;
|
|
12
19
|
export interface WorkerOpts {
|
|
13
20
|
name: string;
|
|
14
21
|
handleKill?: boolean;
|
|
@@ -44,7 +51,7 @@ export declare class Worker {
|
|
|
44
51
|
*/
|
|
45
52
|
register_workflow(initWorkflow: Workflow): Promise<void>;
|
|
46
53
|
registerWorkflow(initWorkflow: Workflow): Promise<void>;
|
|
47
|
-
registerAction<T, K>(actionId: string, action: StepRunFunction<T, K>): void;
|
|
54
|
+
registerAction<T, K>(actionId: string, action: StepRunFunction<T, K>, compute?: z.infer<typeof ComputeSchema>): void;
|
|
48
55
|
handleStartStepRun(action: Action): Promise<void>;
|
|
49
56
|
handleStartGroupKeyRun(action: Action): Promise<void>;
|
|
50
57
|
getStepActionEvent(action: Action, eventType: StepActionEventType, payload?: any): StepActionEvent;
|
|
@@ -56,3 +63,4 @@ export declare class Worker {
|
|
|
56
63
|
handleAction(action: Action): Promise<void>;
|
|
57
64
|
upsertLabels(labels: WorkerLabels): Promise<WorkerLabels>;
|
|
58
65
|
}
|
|
66
|
+
export {};
|
package/clients/worker/worker.js
CHANGED
|
@@ -27,6 +27,7 @@ const workflows_1 = require("../../protoc/workflows");
|
|
|
27
27
|
const logger_1 = require("../../util/logger");
|
|
28
28
|
const handler_1 = require("./handler");
|
|
29
29
|
const step_1 = require("../../step");
|
|
30
|
+
const managed_compute_1 = require("./compute/managed-compute");
|
|
30
31
|
class Worker {
|
|
31
32
|
constructor(client, options) {
|
|
32
33
|
this.workflow_registry = [];
|
|
@@ -48,18 +49,33 @@ class Worker {
|
|
|
48
49
|
registerActions(workflow) {
|
|
49
50
|
var _a;
|
|
50
51
|
const newActions = workflow.steps.reduce((acc, step) => {
|
|
51
|
-
|
|
52
|
+
// Only register actions that are in the runnable_actions list
|
|
53
|
+
if (!this.client.config.runnable_actions ||
|
|
54
|
+
this.client.config.runnable_actions.includes(`${workflow.id}:${step.name}`)) {
|
|
55
|
+
acc[`${workflow.id}:${step.name}`] = {
|
|
56
|
+
func: step.run,
|
|
57
|
+
compute: step.compute,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
52
60
|
return acc;
|
|
53
61
|
}, {});
|
|
54
62
|
const onFailureAction = workflow.onFailure
|
|
55
63
|
? {
|
|
56
|
-
[`${workflow.id}-on-failure:${workflow.onFailure.name}`]:
|
|
64
|
+
[`${workflow.id}-on-failure:${workflow.onFailure.name}`]: {
|
|
65
|
+
func: workflow.onFailure.run,
|
|
66
|
+
compute: workflow.onFailure.compute,
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
: {};
|
|
70
|
+
const concurrencyAction = ((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.name) && workflow.concurrency.key
|
|
71
|
+
? {
|
|
72
|
+
[`${workflow.id}:${workflow.concurrency.name}`]: {
|
|
73
|
+
func: workflow.concurrency.key,
|
|
74
|
+
compute: undefined,
|
|
75
|
+
},
|
|
57
76
|
}
|
|
58
77
|
: {};
|
|
59
|
-
this.action_registry = Object.assign(Object.assign(Object.assign({}, this.action_registry), newActions), onFailureAction);
|
|
60
|
-
this.action_registry =
|
|
61
|
-
((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.name) && workflow.concurrency.key
|
|
62
|
-
? Object.assign(Object.assign({}, this.action_registry), { [`${workflow.id}:${workflow.concurrency.name}`]: workflow.concurrency.key }) : Object.assign({}, this.action_registry);
|
|
78
|
+
this.action_registry = Object.assign(Object.assign(Object.assign(Object.assign({}, this.action_registry), newActions), onFailureAction), concurrencyAction);
|
|
63
79
|
}
|
|
64
80
|
getHandler(workflows) {
|
|
65
81
|
for (const workflow of workflows) {
|
|
@@ -162,8 +178,11 @@ class Worker {
|
|
|
162
178
|
this.registerActions(workflow);
|
|
163
179
|
});
|
|
164
180
|
}
|
|
165
|
-
registerAction(actionId, action) {
|
|
166
|
-
this.action_registry[actionId] =
|
|
181
|
+
registerAction(actionId, action, compute) {
|
|
182
|
+
this.action_registry[actionId] = {
|
|
183
|
+
func: action,
|
|
184
|
+
compute,
|
|
185
|
+
};
|
|
167
186
|
}
|
|
168
187
|
handleStartStepRun(action) {
|
|
169
188
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -177,7 +196,7 @@ class Worker {
|
|
|
177
196
|
return;
|
|
178
197
|
}
|
|
179
198
|
const run = () => __awaiter(this, void 0, void 0, function* () {
|
|
180
|
-
return step(context);
|
|
199
|
+
return step.func(context);
|
|
181
200
|
});
|
|
182
201
|
const success = (result) => __awaiter(this, void 0, void 0, function* () {
|
|
183
202
|
this.logger.info(`Step run ${action.stepRunId} succeeded`);
|
|
@@ -262,7 +281,7 @@ class Worker {
|
|
|
262
281
|
return;
|
|
263
282
|
}
|
|
264
283
|
const run = () => __awaiter(this, void 0, void 0, function* () {
|
|
265
|
-
return step(context);
|
|
284
|
+
return step.func(context);
|
|
266
285
|
});
|
|
267
286
|
const success = (result) => {
|
|
268
287
|
this.logger.info(`Step run ${action.stepRunId} succeeded`);
|
|
@@ -391,6 +410,8 @@ class Worker {
|
|
|
391
410
|
var _a, e_1, _b, _c;
|
|
392
411
|
// ensure all workflows are registered
|
|
393
412
|
yield Promise.all(this.registeredWorkflowPromises);
|
|
413
|
+
const managedCompute = new managed_compute_1.ManagedCompute(this.action_registry, this.client, this.maxRuns);
|
|
414
|
+
yield managedCompute.cloudRegister();
|
|
394
415
|
try {
|
|
395
416
|
this.listener = yield this.client.dispatcher.getActionListener({
|
|
396
417
|
workerName: this.name,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const data_contracts_1 = require("../clients/rest/generated/cloud/data-contracts");
|
|
16
|
+
const sdk_1 = __importDefault(require("../sdk"));
|
|
17
|
+
const hatchet = sdk_1.default.init();
|
|
18
|
+
const oneCpuWorkerConfig = {
|
|
19
|
+
cpuKind: 'shared',
|
|
20
|
+
memoryMb: 1024,
|
|
21
|
+
numReplicas: 1,
|
|
22
|
+
cpus: 1,
|
|
23
|
+
regions: [data_contracts_1.ManagedWorkerRegion.Ewr],
|
|
24
|
+
};
|
|
25
|
+
const twoCpuWorkerConfig = {
|
|
26
|
+
cpuKind: 'shared',
|
|
27
|
+
memoryMb: 1024,
|
|
28
|
+
numReplicas: 1,
|
|
29
|
+
cpus: 2,
|
|
30
|
+
regions: [data_contracts_1.ManagedWorkerRegion.Ewr],
|
|
31
|
+
};
|
|
32
|
+
const workflow = {
|
|
33
|
+
id: 'simple-workflow',
|
|
34
|
+
description: 'test',
|
|
35
|
+
on: {
|
|
36
|
+
event: 'user:create',
|
|
37
|
+
},
|
|
38
|
+
steps: [
|
|
39
|
+
{
|
|
40
|
+
name: 'step1',
|
|
41
|
+
compute: oneCpuWorkerConfig,
|
|
42
|
+
run: (ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
43
|
+
console.log('executed step1!');
|
|
44
|
+
return { step1: 'step1 results!' };
|
|
45
|
+
}),
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: 'step2',
|
|
49
|
+
parents: ['step1'],
|
|
50
|
+
compute: twoCpuWorkerConfig,
|
|
51
|
+
run: (ctx) => {
|
|
52
|
+
console.log('executed step2 after step1 returned ', ctx.stepOutput('step1'));
|
|
53
|
+
return { step2: 'step2 results!' };
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
};
|
|
58
|
+
function main() {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
const worker = yield hatchet.worker('managed-worker');
|
|
61
|
+
yield worker.registerWorkflow(workflow);
|
|
62
|
+
worker.start();
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
main();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hatchet-dev/typescript-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0-alpha.1",
|
|
4
4
|
"description": "Background task orchestration & visibility for developers",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"test:unit:watch": "jest --testMatch='**/*.test.ts' --watch",
|
|
24
24
|
"generate": "pnpm run '/generate-.*/'",
|
|
25
25
|
"generate-api": "npx --yes swagger-cli bundle ./hatchet/api-contracts/openapi/openapi.yaml --outfile openapi.yaml --type yaml && npx swagger-typescript-api -p openapi.yaml -o src/clients/rest/generated -n hatchet.ts --modular --axios",
|
|
26
|
+
"generate-cloud-api": "npx --yes swagger-cli bundle ./hatchet-cloud/api-contracts/openapi/openapi.yaml --outfile openapi.yaml --type yaml && npx swagger-typescript-api -p openapi.yaml -o src/clients/rest/generated/cloud -n hatchet.ts --modular --axios",
|
|
26
27
|
"generate-protoc": "./generate-protoc.sh",
|
|
27
28
|
"lint:check": "npm run eslint:check && npm run prettier:check",
|
|
28
29
|
"lint:fix": "npm run eslint:fix && npm run prettier:fix",
|
|
@@ -38,6 +39,7 @@
|
|
|
38
39
|
"example:rate": "npm run exec -- ./src/examples/rate-limit/events.ts",
|
|
39
40
|
"worker:fanout": "npm run exec -- ./src/examples/fanout-worker.ts",
|
|
40
41
|
"worker:simple": "npm run exec -- ./src/examples/simple-worker.ts",
|
|
42
|
+
"worker:managed": "npm run exec -- ./src/examples/managed-worker.ts",
|
|
41
43
|
"worker:affinity": "npm run exec -- ./src/examples/affinity-workers.ts",
|
|
42
44
|
"worker:sticky": "npm run exec -- ./src/examples/sticky-worker.ts",
|
|
43
45
|
"worker:on-failure": "npm run exec -- ./src/examples/on-failure.ts",
|
|
@@ -58,6 +60,7 @@
|
|
|
58
60
|
"api": "npm run exec -- ./src/examples/api.ts",
|
|
59
61
|
"prepublish": "cp package.json dist/package.json;",
|
|
60
62
|
"publish:ci": "rm -rf ./dist && npm run tsc:build && npm run prepublish && cd dist && npm publish --access public --no-git-checks",
|
|
63
|
+
"publish:ci:alpha": "rm -rf ./dist && npm run tsc:build && npm run prepublish && cd dist && npm publish --access public --no-git-checks --tag alpha",
|
|
61
64
|
"generate-docs": "typedoc"
|
|
62
65
|
},
|
|
63
66
|
"keywords": [],
|
package/step.d.ts
CHANGED
|
@@ -89,11 +89,71 @@ export declare const CreateStepSchema: z.ZodObject<{
|
|
|
89
89
|
comparator?: WorkerLabelComparator | undefined;
|
|
90
90
|
weight?: number | undefined;
|
|
91
91
|
}>]>>>>>;
|
|
92
|
+
compute: z.ZodOptional<z.ZodUnion<[z.ZodObject<z.objectUtil.extendShape<{
|
|
93
|
+
pool: z.ZodOptional<z.ZodDefault<z.ZodString>>;
|
|
94
|
+
numReplicas: z.ZodDefault<z.ZodNumber>;
|
|
95
|
+
regions: z.ZodOptional<z.ZodArray<z.ZodNativeEnum<typeof import("./clients/rest/generated/cloud/data-contracts").ManagedWorkerRegion>, "many">>;
|
|
96
|
+
cpus: z.ZodNumber;
|
|
97
|
+
}, {
|
|
98
|
+
cpuKind: z.ZodLiteral<"shared">;
|
|
99
|
+
memoryMb: z.ZodEffects<z.ZodNumber, number, number>;
|
|
100
|
+
}>, "strip", z.ZodTypeAny, {
|
|
101
|
+
numReplicas: number;
|
|
102
|
+
cpus: number;
|
|
103
|
+
cpuKind: "shared";
|
|
104
|
+
memoryMb: number;
|
|
105
|
+
pool?: string | undefined;
|
|
106
|
+
regions?: import("./clients/rest/generated/cloud/data-contracts").ManagedWorkerRegion[] | undefined;
|
|
107
|
+
}, {
|
|
108
|
+
cpus: number;
|
|
109
|
+
cpuKind: "shared";
|
|
110
|
+
memoryMb: number;
|
|
111
|
+
pool?: string | undefined;
|
|
112
|
+
numReplicas?: number | undefined;
|
|
113
|
+
regions?: import("./clients/rest/generated/cloud/data-contracts").ManagedWorkerRegion[] | undefined;
|
|
114
|
+
}>, z.ZodObject<z.objectUtil.extendShape<{
|
|
115
|
+
pool: z.ZodOptional<z.ZodDefault<z.ZodString>>;
|
|
116
|
+
numReplicas: z.ZodDefault<z.ZodNumber>;
|
|
117
|
+
regions: z.ZodOptional<z.ZodArray<z.ZodNativeEnum<typeof import("./clients/rest/generated/cloud/data-contracts").ManagedWorkerRegion>, "many">>;
|
|
118
|
+
cpus: z.ZodNumber;
|
|
119
|
+
}, {
|
|
120
|
+
cpuKind: z.ZodLiteral<"dedicated">;
|
|
121
|
+
memoryMb: z.ZodEffects<z.ZodNumber, number, number>;
|
|
122
|
+
}>, "strip", z.ZodTypeAny, {
|
|
123
|
+
numReplicas: number;
|
|
124
|
+
cpus: number;
|
|
125
|
+
cpuKind: "dedicated";
|
|
126
|
+
memoryMb: number;
|
|
127
|
+
pool?: string | undefined;
|
|
128
|
+
regions?: import("./clients/rest/generated/cloud/data-contracts").ManagedWorkerRegion[] | undefined;
|
|
129
|
+
}, {
|
|
130
|
+
cpus: number;
|
|
131
|
+
cpuKind: "dedicated";
|
|
132
|
+
memoryMb: number;
|
|
133
|
+
pool?: string | undefined;
|
|
134
|
+
numReplicas?: number | undefined;
|
|
135
|
+
regions?: import("./clients/rest/generated/cloud/data-contracts").ManagedWorkerRegion[] | undefined;
|
|
136
|
+
}>]>>;
|
|
92
137
|
}, "strip", z.ZodTypeAny, {
|
|
93
138
|
name: string;
|
|
94
139
|
timeout?: string | undefined;
|
|
95
140
|
parents?: string[] | undefined;
|
|
96
141
|
retries?: number | undefined;
|
|
142
|
+
compute?: {
|
|
143
|
+
numReplicas: number;
|
|
144
|
+
cpus: number;
|
|
145
|
+
cpuKind: "shared";
|
|
146
|
+
memoryMb: number;
|
|
147
|
+
pool?: string | undefined;
|
|
148
|
+
regions?: import("./clients/rest/generated/cloud/data-contracts").ManagedWorkerRegion[] | undefined;
|
|
149
|
+
} | {
|
|
150
|
+
numReplicas: number;
|
|
151
|
+
cpus: number;
|
|
152
|
+
cpuKind: "dedicated";
|
|
153
|
+
memoryMb: number;
|
|
154
|
+
pool?: string | undefined;
|
|
155
|
+
regions?: import("./clients/rest/generated/cloud/data-contracts").ManagedWorkerRegion[] | undefined;
|
|
156
|
+
} | undefined;
|
|
97
157
|
rate_limits?: {
|
|
98
158
|
units: string | number;
|
|
99
159
|
key?: string | undefined;
|
|
@@ -113,6 +173,21 @@ export declare const CreateStepSchema: z.ZodObject<{
|
|
|
113
173
|
timeout?: string | undefined;
|
|
114
174
|
parents?: string[] | undefined;
|
|
115
175
|
retries?: number | undefined;
|
|
176
|
+
compute?: {
|
|
177
|
+
cpus: number;
|
|
178
|
+
cpuKind: "shared";
|
|
179
|
+
memoryMb: number;
|
|
180
|
+
pool?: string | undefined;
|
|
181
|
+
numReplicas?: number | undefined;
|
|
182
|
+
regions?: import("./clients/rest/generated/cloud/data-contracts").ManagedWorkerRegion[] | undefined;
|
|
183
|
+
} | {
|
|
184
|
+
cpus: number;
|
|
185
|
+
cpuKind: "dedicated";
|
|
186
|
+
memoryMb: number;
|
|
187
|
+
pool?: string | undefined;
|
|
188
|
+
numReplicas?: number | undefined;
|
|
189
|
+
regions?: import("./clients/rest/generated/cloud/data-contracts").ManagedWorkerRegion[] | undefined;
|
|
190
|
+
} | undefined;
|
|
116
191
|
rate_limits?: {
|
|
117
192
|
units: string | number;
|
|
118
193
|
key?: string | undefined;
|
package/step.js
CHANGED
|
@@ -44,6 +44,7 @@ const workflow_1 = require("./workflow");
|
|
|
44
44
|
const logger_1 = require("./util/logger");
|
|
45
45
|
const parse_1 = require("./util/parse");
|
|
46
46
|
const workflows_1 = require("./protoc/workflows");
|
|
47
|
+
const compute_config_1 = require("./clients/worker/compute/compute-config");
|
|
47
48
|
exports.CreateRateLimitSchema = z.object({
|
|
48
49
|
key: z.string().optional(),
|
|
49
50
|
staticKey: z.string().optional(),
|
|
@@ -74,6 +75,7 @@ exports.CreateStepSchema = z.object({
|
|
|
74
75
|
retries: z.number().optional(),
|
|
75
76
|
rate_limits: z.array(exports.CreateRateLimitSchema).optional(),
|
|
76
77
|
worker_labels: z.record(z.lazy(() => exports.DesiredWorkerLabelSchema)).optional(),
|
|
78
|
+
compute: compute_config_1.ComputeSchema.optional(),
|
|
77
79
|
});
|
|
78
80
|
class ContextWorker {
|
|
79
81
|
constructor(worker) {
|
|
@@ -66,6 +66,22 @@ class ConfigLoader {
|
|
|
66
66
|
apiUrl = (_z = (_y = override === null || override === void 0 ? void 0 : override.api_url) !== null && _y !== void 0 ? _y : yaml === null || yaml === void 0 ? void 0 : yaml.api_url) !== null && _z !== void 0 ? _z : this.env('HATCHET_CLIENT_API_URL');
|
|
67
67
|
}
|
|
68
68
|
const namespace = (_1 = (_0 = override === null || override === void 0 ? void 0 : override.namespace) !== null && _0 !== void 0 ? _0 : yaml === null || yaml === void 0 ? void 0 : yaml.namespace) !== null && _1 !== void 0 ? _1 : this.env('HATCHET_CLIENT_NAMESPACE');
|
|
69
|
+
let rawRunnableActions;
|
|
70
|
+
if (override === null || override === void 0 ? void 0 : override.runnable_actions) {
|
|
71
|
+
rawRunnableActions = override.runnable_actions;
|
|
72
|
+
}
|
|
73
|
+
else if (yaml === null || yaml === void 0 ? void 0 : yaml.runnable_actions) {
|
|
74
|
+
rawRunnableActions = yaml.runnable_actions;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
const envActions = this.env('HATCHET_CLOUD_ACTIONS');
|
|
78
|
+
if (envActions) {
|
|
79
|
+
rawRunnableActions = envActions.split(',');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const registerId = this.env('HATCHET_CLOUD_REGISTER_ID');
|
|
83
|
+
const namespacePrefix = namespace ? `${namespace}_`.toLowerCase() : '';
|
|
84
|
+
const runnableActions = rawRunnableActions === null || rawRunnableActions === void 0 ? void 0 : rawRunnableActions.map((action) => namespacePrefix + action.trim());
|
|
69
85
|
return {
|
|
70
86
|
token: (_3 = (_2 = override === null || override === void 0 ? void 0 : override.token) !== null && _2 !== void 0 ? _2 : yaml === null || yaml === void 0 ? void 0 : yaml.token) !== null && _3 !== void 0 ? _3 : this.env('HATCHET_CLIENT_TOKEN'),
|
|
71
87
|
host_port: grpcBroadcastAddress,
|
|
@@ -73,7 +89,9 @@ class ConfigLoader {
|
|
|
73
89
|
tls_config: tlsConfig,
|
|
74
90
|
log_level: (_6 = (_5 = (_4 = override === null || override === void 0 ? void 0 : override.log_level) !== null && _4 !== void 0 ? _4 : yaml === null || yaml === void 0 ? void 0 : yaml.log_level) !== null && _5 !== void 0 ? _5 : this.env('HATCHET_CLIENT_LOG_LEVEL')) !== null && _6 !== void 0 ? _6 : 'INFO',
|
|
75
91
|
tenant_id: tenantId,
|
|
76
|
-
namespace:
|
|
92
|
+
namespace: namespacePrefix,
|
|
93
|
+
runnable_actions: runnableActions,
|
|
94
|
+
cloud_register_id: registerId,
|
|
77
95
|
};
|
|
78
96
|
}
|
|
79
97
|
static get default_yaml_config_path() {
|