@hatchet-dev/typescript-sdk 0.16.0-alpha.3 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/clients/dispatcher/dispatcher-client.d.ts +2 -1
- package/clients/dispatcher/dispatcher-client.js +10 -1
- package/clients/event/event-client.js +13 -8
- package/clients/hatchet-client/client-config.d.ts +0 -6
- package/clients/hatchet-client/client-config.js +0 -2
- package/clients/hatchet-client/features/cron-client.d.ts +79 -0
- package/clients/hatchet-client/features/cron-client.js +127 -0
- package/clients/hatchet-client/features/schedule-client.d.ts +76 -0
- package/clients/hatchet-client/features/schedule-client.js +120 -0
- package/clients/hatchet-client/hatchet-client.d.ts +4 -2
- package/clients/hatchet-client/hatchet-client.js +6 -25
- package/clients/listener/child-listener-client.js +16 -13
- package/clients/rest/api.d.ts +0 -3
- package/clients/rest/api.js +0 -10
- package/clients/rest/generated/Api.d.ts +152 -2
- package/clients/rest/generated/Api.js +81 -1
- package/clients/rest/generated/data-contracts.d.ts +86 -0
- package/clients/rest/generated/data-contracts.js +27 -1
- package/clients/rest/index.d.ts +2 -2
- package/clients/rest/index.js +21 -10
- package/clients/worker/handler.js +16 -11
- package/clients/worker/worker.d.ts +2 -10
- package/clients/worker/worker.js +21 -37
- package/examples/crons/cron-worker.d.ts +2 -0
- package/examples/crons/cron-worker.js +48 -0
- package/examples/crons/programatic-crons.js +55 -0
- package/examples/on-failure.js +15 -10
- package/examples/scheduled-runs/programatic-schedules.d.ts +1 -0
- package/examples/scheduled-runs/programatic-schedules.js +54 -0
- package/examples/simple-worker.d.ts +2 -1
- package/examples/simple-worker.js +6 -3
- package/package.json +7 -8
- package/protoc/dispatcher/dispatcher.js +1 -1
- package/protoc/events/events.js +1 -1
- package/protoc/google/protobuf/timestamp.js +1 -1
- package/protoc/google/protobuf/wrappers.js +17 -7
- package/protoc/workflows/workflows.d.ts +13 -0
- package/protoc/workflows/workflows.js +160 -7
- package/step.d.ts +0 -117
- package/step.js +17 -9
- package/util/config-loader/config-loader.js +18 -26
- package/util/retrier.js +8 -3
- package/version.d.ts +1 -0
- package/version.js +4 -0
- package/workflow.d.ts +0 -443
- package/workflow.js +17 -7
- package/clients/rest/generated/cloud/Api.d.ts +0 -377
- package/clients/rest/generated/cloud/Api.js +0 -326
- package/clients/rest/generated/cloud/data-contracts.d.ts +0 -468
- package/clients/rest/generated/cloud/data-contracts.js +0 -68
- package/clients/rest/generated/cloud/http-client.d.ts +0 -41
- package/clients/rest/generated/cloud/http-client.js +0 -102
- package/clients/worker/compute/compute-config.d.ts +0 -150
- package/clients/worker/compute/compute-config.js +0 -76
- package/clients/worker/compute/managed-compute.d.ts +0 -15
- package/clients/worker/compute/managed-compute.js +0 -102
- package/examples/managed-worker.js +0 -65
- /package/examples/{managed-worker.d.ts → crons/programatic-crons.d.ts} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Channel, ClientFactory } from 'nice-grpc';
|
|
2
|
-
import { DispatcherClient as PbDispatcherClient, StepActionEvent, GroupKeyActionEvent, OverridesData, DeepPartial } from '../../protoc/dispatcher';
|
|
2
|
+
import { DispatcherClient as PbDispatcherClient, StepActionEvent, GroupKeyActionEvent, OverridesData, DeepPartial, RuntimeInfo } from '../../protoc/dispatcher';
|
|
3
3
|
import { ClientConfig } from '../hatchet-client/client-config';
|
|
4
4
|
import { Logger } from '../../util/logger';
|
|
5
5
|
import { ActionListener } from './action-listener';
|
|
@@ -16,6 +16,7 @@ export declare class DispatcherClient {
|
|
|
16
16
|
client: PbDispatcherClient;
|
|
17
17
|
logger: Logger;
|
|
18
18
|
constructor(config: ClientConfig, channel: Channel, factory: ClientFactory);
|
|
19
|
+
getRuntimeInfo(): RuntimeInfo;
|
|
19
20
|
getActionListener(options: GetActionListenerOptions): Promise<ActionListener>;
|
|
20
21
|
sendStepActionEvent(in_: StepActionEvent): Promise<import("../../protoc/dispatcher").ActionEventResponse>;
|
|
21
22
|
sendGroupKeyActionEvent(in_: GroupKeyActionEvent): Promise<import("../../protoc/dispatcher").ActionEventResponse>;
|
|
@@ -17,6 +17,7 @@ const dispatcher_1 = require("../../protoc/dispatcher");
|
|
|
17
17
|
const hatchet_error_1 = __importDefault(require("../../util/errors/hatchet-error"));
|
|
18
18
|
const logger_1 = require("../../util/logger");
|
|
19
19
|
const retrier_1 = require("../../util/retrier");
|
|
20
|
+
const version_1 = require("../../version");
|
|
20
21
|
const action_listener_1 = require("./action-listener");
|
|
21
22
|
class DispatcherClient {
|
|
22
23
|
constructor(config, channel, factory) {
|
|
@@ -24,10 +25,18 @@ class DispatcherClient {
|
|
|
24
25
|
this.client = factory.create(dispatcher_1.DispatcherDefinition, channel);
|
|
25
26
|
this.logger = new logger_1.Logger(`Dispatcher`, config.log_level);
|
|
26
27
|
}
|
|
28
|
+
getRuntimeInfo() {
|
|
29
|
+
return {
|
|
30
|
+
sdkVersion: version_1.HATCHET_VERSION,
|
|
31
|
+
language: dispatcher_1.SDKS.TYPESCRIPT,
|
|
32
|
+
languageVersion: process.version,
|
|
33
|
+
os: process.platform,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
27
36
|
getActionListener(options) {
|
|
28
37
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
38
|
// Register the worker
|
|
30
|
-
const registration = yield this.client.register(Object.assign(Object.assign({}, options), { labels: options.labels ? mapLabels(options.labels) : undefined }));
|
|
39
|
+
const registration = yield this.client.register(Object.assign(Object.assign({}, options), { labels: options.labels ? mapLabels(options.labels) : undefined, runtimeInfo: this.getRuntimeInfo() }));
|
|
31
40
|
return new action_listener_1.ActionListener(this, registration.workerId);
|
|
32
41
|
});
|
|
33
42
|
}
|
|
@@ -84,14 +84,19 @@ class EventClient {
|
|
|
84
84
|
}
|
|
85
85
|
putLog(stepRunId, log, level) {
|
|
86
86
|
const createdAt = new Date();
|
|
87
|
-
(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
87
|
+
if (log.length > 1000) {
|
|
88
|
+
this.logger.warn(`log is too long, skipping: ${log.length} characters`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
// fire and forget the log
|
|
92
|
+
this.client
|
|
93
|
+
.putLog({
|
|
94
|
+
stepRunId,
|
|
95
|
+
createdAt,
|
|
96
|
+
message: log,
|
|
97
|
+
level: level || LogLevel.INFO,
|
|
98
|
+
})
|
|
99
|
+
.catch((e) => {
|
|
95
100
|
// log a warning, but this is not a fatal error
|
|
96
101
|
this.logger.warn(`Could not put log: ${e.message}`);
|
|
97
102
|
});
|
|
@@ -45,8 +45,6 @@ export declare const ClientConfigSchema: z.ZodObject<{
|
|
|
45
45
|
log_level: z.ZodOptional<z.ZodEnum<["OFF", "DEBUG", "INFO", "WARN", "ERROR"]>>;
|
|
46
46
|
tenant_id: z.ZodString;
|
|
47
47
|
namespace: z.ZodOptional<z.ZodString>;
|
|
48
|
-
runnable_actions: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
49
|
-
cloud_register_id: z.ZodOptional<z.ZodString>;
|
|
50
48
|
}, "strip", z.ZodTypeAny, {
|
|
51
49
|
token: string;
|
|
52
50
|
tls_config: {
|
|
@@ -61,8 +59,6 @@ export declare const ClientConfigSchema: z.ZodObject<{
|
|
|
61
59
|
tenant_id: string;
|
|
62
60
|
log_level?: "OFF" | "DEBUG" | "INFO" | "WARN" | "ERROR" | undefined;
|
|
63
61
|
namespace?: string | undefined;
|
|
64
|
-
runnable_actions?: string[] | undefined;
|
|
65
|
-
cloud_register_id?: string | undefined;
|
|
66
62
|
}, {
|
|
67
63
|
token: string;
|
|
68
64
|
tls_config: {
|
|
@@ -77,8 +73,6 @@ export declare const ClientConfigSchema: z.ZodObject<{
|
|
|
77
73
|
tenant_id: string;
|
|
78
74
|
log_level?: "OFF" | "DEBUG" | "INFO" | "WARN" | "ERROR" | undefined;
|
|
79
75
|
namespace?: string | undefined;
|
|
80
|
-
runnable_actions?: string[] | undefined;
|
|
81
|
-
cloud_register_id?: string | undefined;
|
|
82
76
|
}>;
|
|
83
77
|
export type ClientConfig = z.infer<typeof ClientConfigSchema> & {
|
|
84
78
|
credentials?: ChannelCredentials;
|
|
@@ -17,6 +17,4 @@ exports.ClientConfigSchema = zod_1.z.object({
|
|
|
17
17
|
log_level: zod_1.z.enum(['OFF', 'DEBUG', 'INFO', 'WARN', 'ERROR']).optional(),
|
|
18
18
|
tenant_id: zod_1.z.string(),
|
|
19
19
|
namespace: zod_1.z.string().optional(),
|
|
20
|
-
runnable_actions: zod_1.z.array(zod_1.z.string()).optional(),
|
|
21
|
-
cloud_register_id: zod_1.z.string().optional(),
|
|
22
20
|
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { AdminClient } from '../../admin';
|
|
2
|
+
import { Api } from '../../rest';
|
|
3
|
+
import { CronWorkflows, CronWorkflowsList } from '../../rest/generated/data-contracts';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { Workflow } from '../../../workflow';
|
|
6
|
+
import { ClientConfig } from '../client-config';
|
|
7
|
+
/**
|
|
8
|
+
* Schema for creating a Cron Trigger.
|
|
9
|
+
*/
|
|
10
|
+
export declare const CreateCronTriggerSchema: z.ZodObject<{
|
|
11
|
+
name: z.ZodString;
|
|
12
|
+
expression: z.ZodEffects<z.ZodString, string, string>;
|
|
13
|
+
input: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
14
|
+
additionalMetadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
15
|
+
}, "strip", z.ZodTypeAny, {
|
|
16
|
+
name: string;
|
|
17
|
+
expression: string;
|
|
18
|
+
additionalMetadata?: Record<string, string> | undefined;
|
|
19
|
+
input?: Record<string, any> | undefined;
|
|
20
|
+
}, {
|
|
21
|
+
name: string;
|
|
22
|
+
expression: string;
|
|
23
|
+
additionalMetadata?: Record<string, string> | undefined;
|
|
24
|
+
input?: Record<string, any> | undefined;
|
|
25
|
+
}>;
|
|
26
|
+
/**
|
|
27
|
+
* Type representing the input for creating a Cron.
|
|
28
|
+
*/
|
|
29
|
+
export type CreateCronInput = z.infer<typeof CreateCronTriggerSchema>;
|
|
30
|
+
/**
|
|
31
|
+
* Client for managing Cron Triggers.
|
|
32
|
+
*/
|
|
33
|
+
export declare class CronClient {
|
|
34
|
+
private readonly tenantId;
|
|
35
|
+
private readonly config;
|
|
36
|
+
private readonly api;
|
|
37
|
+
private readonly adminClient;
|
|
38
|
+
private logger;
|
|
39
|
+
/**
|
|
40
|
+
* Initializes a new instance of CronClient.
|
|
41
|
+
* @param tenantId - The tenant identifier.
|
|
42
|
+
* @param config - Client configuration settings.
|
|
43
|
+
* @param api - API instance for REST interactions.
|
|
44
|
+
* @param adminClient - Admin client for administrative operations.
|
|
45
|
+
*/
|
|
46
|
+
constructor(tenantId: string, config: ClientConfig, api: Api, adminClient: AdminClient);
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves the Cron ID from a CronWorkflows object or a string.
|
|
49
|
+
* @param cron - The CronWorkflows object or Cron ID as a string.
|
|
50
|
+
* @returns The Cron ID as a string.
|
|
51
|
+
*/
|
|
52
|
+
private getCronId;
|
|
53
|
+
/**
|
|
54
|
+
* Creates a new Cron workflow.
|
|
55
|
+
* @param workflow - The workflow identifier or Workflow object.
|
|
56
|
+
* @param cron - The input data for creating the Cron Trigger.
|
|
57
|
+
* @returns A promise that resolves to the created CronWorkflows object.
|
|
58
|
+
* @throws Will throw an error if the input is invalid or the API call fails.
|
|
59
|
+
*/
|
|
60
|
+
create(workflow: string | Workflow, cron: CreateCronInput): Promise<CronWorkflows>;
|
|
61
|
+
/**
|
|
62
|
+
* Deletes an existing Cron Trigger.
|
|
63
|
+
* @param cron - The Cron Trigger ID as a string or CronWorkflows object.
|
|
64
|
+
* @returns A promise that resolves when the Cron Trigger is deleted.
|
|
65
|
+
*/
|
|
66
|
+
delete(cron: string | CronWorkflows): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Lists all Cron Triggers based on the provided query parameters.
|
|
69
|
+
* @param query - Query parameters for listing Cron Triggers.
|
|
70
|
+
* @returns A promise that resolves to a CronWorkflowsList object.
|
|
71
|
+
*/
|
|
72
|
+
list(query: Parameters<typeof this.api.cronWorkflowList>[1]): Promise<CronWorkflowsList>;
|
|
73
|
+
/**
|
|
74
|
+
* Retrieves a specific Cron Trigger by its ID.
|
|
75
|
+
* @param cron - The Cron Trigger ID as a string or CronWorkflows object.
|
|
76
|
+
* @returns A promise that resolves to the CronWorkflows object.
|
|
77
|
+
*/
|
|
78
|
+
get(cron: string | CronWorkflows): Promise<CronWorkflows>;
|
|
79
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
exports.CronClient = exports.CreateCronTriggerSchema = void 0;
|
|
16
|
+
const logger_1 = __importDefault(require("../../../util/logger/logger"));
|
|
17
|
+
const zod_1 = require("zod");
|
|
18
|
+
const axios_1 = require("axios");
|
|
19
|
+
/**
|
|
20
|
+
* Schema for creating a Cron Trigger.
|
|
21
|
+
*/
|
|
22
|
+
exports.CreateCronTriggerSchema = zod_1.z.object({
|
|
23
|
+
name: zod_1.z.string(),
|
|
24
|
+
expression: zod_1.z.string().refine((val) => {
|
|
25
|
+
// Basic cron validation regex
|
|
26
|
+
const cronRegex = /^(\*|([0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])|\*\/([0-9]|1[0-9]|2[0-3])) (\*|([0-9]|1[0-9]|2[0-3])|\*\/([0-9]|1[0-9]|2[0-3])) (\*|([1-9]|1[0-9]|2[0-9]|3[0-1])|\*\/([1-9]|1[0-9]|2[0-9]|3[0-1])) (\*|([1-9]|1[0-2])|\*\/([1-9]|1[0-2])) (\*|([0-6])|\*\/([0-6]))$/;
|
|
27
|
+
return cronRegex.test(val);
|
|
28
|
+
}, 'Invalid cron expression'),
|
|
29
|
+
input: zod_1.z.record(zod_1.z.any()).optional(),
|
|
30
|
+
additionalMetadata: zod_1.z.record(zod_1.z.string()).optional(),
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* Client for managing Cron Triggers.
|
|
34
|
+
*/
|
|
35
|
+
class CronClient {
|
|
36
|
+
/**
|
|
37
|
+
* Initializes a new instance of CronClient.
|
|
38
|
+
* @param tenantId - The tenant identifier.
|
|
39
|
+
* @param config - Client configuration settings.
|
|
40
|
+
* @param api - API instance for REST interactions.
|
|
41
|
+
* @param adminClient - Admin client for administrative operations.
|
|
42
|
+
*/
|
|
43
|
+
constructor(tenantId, config, api, adminClient) {
|
|
44
|
+
this.tenantId = tenantId;
|
|
45
|
+
this.config = config;
|
|
46
|
+
this.api = api;
|
|
47
|
+
this.adminClient = adminClient;
|
|
48
|
+
this.logger = new logger_1.default('Cron', this.config.log_level);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves the Cron ID from a CronWorkflows object or a string.
|
|
52
|
+
* @param cron - The CronWorkflows object or Cron ID as a string.
|
|
53
|
+
* @returns The Cron ID as a string.
|
|
54
|
+
*/
|
|
55
|
+
getCronId(cron) {
|
|
56
|
+
return typeof cron === 'string' ? cron : cron.metadata.id;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new Cron workflow.
|
|
60
|
+
* @param workflow - The workflow identifier or Workflow object.
|
|
61
|
+
* @param cron - The input data for creating the Cron Trigger.
|
|
62
|
+
* @returns A promise that resolves to the created CronWorkflows object.
|
|
63
|
+
* @throws Will throw an error if the input is invalid or the API call fails.
|
|
64
|
+
*/
|
|
65
|
+
create(workflow, cron) {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
var _a, _b, _c;
|
|
68
|
+
const workflowId = typeof workflow === 'string' ? workflow : workflow.id;
|
|
69
|
+
console.log('workflowId', workflowId);
|
|
70
|
+
// Validate cron input with zod schema
|
|
71
|
+
try {
|
|
72
|
+
const parsedCron = exports.CreateCronTriggerSchema.parse(cron);
|
|
73
|
+
const response = yield this.api.cronWorkflowTriggerCreate(this.tenantId, workflowId, {
|
|
74
|
+
cronName: parsedCron.name,
|
|
75
|
+
cronExpression: parsedCron.expression,
|
|
76
|
+
input: (_a = parsedCron.input) !== null && _a !== void 0 ? _a : {},
|
|
77
|
+
additionalMetadata: (_b = parsedCron.additionalMetadata) !== null && _b !== void 0 ? _b : {},
|
|
78
|
+
});
|
|
79
|
+
return response.data;
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
if (err instanceof zod_1.z.ZodError) {
|
|
83
|
+
throw new Error(`Invalid cron input: ${err.message}`);
|
|
84
|
+
}
|
|
85
|
+
if (err instanceof axios_1.AxiosError) {
|
|
86
|
+
throw new Error(JSON.stringify((_c = err.response) === null || _c === void 0 ? void 0 : _c.data.errors));
|
|
87
|
+
}
|
|
88
|
+
throw err;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Deletes an existing Cron Trigger.
|
|
94
|
+
* @param cron - The Cron Trigger ID as a string or CronWorkflows object.
|
|
95
|
+
* @returns A promise that resolves when the Cron Trigger is deleted.
|
|
96
|
+
*/
|
|
97
|
+
delete(cron) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
const cronId = this.getCronId(cron);
|
|
100
|
+
yield this.api.workflowCronDelete(this.tenantId, cronId);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Lists all Cron Triggers based on the provided query parameters.
|
|
105
|
+
* @param query - Query parameters for listing Cron Triggers.
|
|
106
|
+
* @returns A promise that resolves to a CronWorkflowsList object.
|
|
107
|
+
*/
|
|
108
|
+
list(query) {
|
|
109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
const response = yield this.api.cronWorkflowList(this.tenantId, query);
|
|
111
|
+
return response.data;
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Retrieves a specific Cron Trigger by its ID.
|
|
116
|
+
* @param cron - The Cron Trigger ID as a string or CronWorkflows object.
|
|
117
|
+
* @returns A promise that resolves to the CronWorkflows object.
|
|
118
|
+
*/
|
|
119
|
+
get(cron) {
|
|
120
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
+
const cronId = this.getCronId(cron);
|
|
122
|
+
const response = yield this.api.workflowCronGet(this.tenantId, cronId);
|
|
123
|
+
return response.data;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.CronClient = CronClient;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { AdminClient } from '../../admin';
|
|
2
|
+
import { Api } from '../../rest';
|
|
3
|
+
import { ScheduledWorkflows, ScheduledWorkflowsList } from '../../rest/generated/data-contracts';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { Workflow } from '../../../workflow';
|
|
6
|
+
import { ClientConfig } from '../client-config';
|
|
7
|
+
/**
|
|
8
|
+
* Schema for creating a Scheduled Run Trigger.
|
|
9
|
+
*/
|
|
10
|
+
export declare const CreateScheduledRunTriggerSchema: z.ZodObject<{
|
|
11
|
+
triggerAt: z.ZodDate;
|
|
12
|
+
input: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
13
|
+
additionalMetadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
14
|
+
}, "strip", z.ZodTypeAny, {
|
|
15
|
+
triggerAt: Date;
|
|
16
|
+
additionalMetadata?: Record<string, string> | undefined;
|
|
17
|
+
input?: Record<string, any> | undefined;
|
|
18
|
+
}, {
|
|
19
|
+
triggerAt: Date;
|
|
20
|
+
additionalMetadata?: Record<string, string> | undefined;
|
|
21
|
+
input?: Record<string, any> | undefined;
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* Type representing the input for creating a Cron.
|
|
25
|
+
*/
|
|
26
|
+
export type CreateScheduledRunInput = z.infer<typeof CreateScheduledRunTriggerSchema>;
|
|
27
|
+
/**
|
|
28
|
+
* Client for managing Scheduled Runs.
|
|
29
|
+
*/
|
|
30
|
+
export declare class ScheduleClient {
|
|
31
|
+
private readonly tenantId;
|
|
32
|
+
private readonly config;
|
|
33
|
+
private readonly api;
|
|
34
|
+
private readonly adminClient;
|
|
35
|
+
private logger;
|
|
36
|
+
/**
|
|
37
|
+
* Initializes a new instance of ScheduleClient.
|
|
38
|
+
* @param tenantId - The tenant identifier.
|
|
39
|
+
* @param config - Client configuration settings.
|
|
40
|
+
* @param api - API instance for REST interactions.
|
|
41
|
+
* @param adminClient - Admin client for administrative operations.
|
|
42
|
+
*/
|
|
43
|
+
constructor(tenantId: string, config: ClientConfig, api: Api, adminClient: AdminClient);
|
|
44
|
+
/**
|
|
45
|
+
* Retrieves the Scheduled Run ID from a ScheduledRun object or a string.
|
|
46
|
+
* @param scheduledRun - The ScheduledRun object or Scheduled Run ID as a string.
|
|
47
|
+
* @returns The Scheduled Run ID as a string.
|
|
48
|
+
*/
|
|
49
|
+
private getScheduledRunId;
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new Scheduled Run.
|
|
52
|
+
* @param workflow - The workflow name or Workflow object.
|
|
53
|
+
* @param scheduledRun - The input data for creating the Scheduled Run.
|
|
54
|
+
* @returns A promise that resolves to the created ScheduledWorkflows object.
|
|
55
|
+
* @throws Will throw an error if the input is invalid or the API call fails.
|
|
56
|
+
*/
|
|
57
|
+
create(workflow: string | Workflow, cron: CreateScheduledRunInput): Promise<ScheduledWorkflows>;
|
|
58
|
+
/**
|
|
59
|
+
* Deletes an existing Scheduled Run.
|
|
60
|
+
* @param scheduledRun - The Scheduled Run ID as a string or ScheduledWorkflows object.
|
|
61
|
+
* @returns A promise that resolves when the Scheduled Run is deleted.
|
|
62
|
+
*/
|
|
63
|
+
delete(scheduledRun: string | ScheduledWorkflows): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Lists all Cron Triggers based on the provided query parameters.
|
|
66
|
+
* @param query - Query parameters for listing Scheduled Runs.
|
|
67
|
+
* @returns A promise that resolves to a ScheduledWorkflowsList object.
|
|
68
|
+
*/
|
|
69
|
+
list(query: Parameters<typeof this.api.workflowScheduledList>[1]): Promise<ScheduledWorkflowsList>;
|
|
70
|
+
/**
|
|
71
|
+
* Retrieves a specific Scheduled Run by its ID.
|
|
72
|
+
* @param scheduledRun - The Scheduled Run ID as a string or ScheduledWorkflows object.
|
|
73
|
+
* @returns A promise that resolves to the ScheduledWorkflows object.
|
|
74
|
+
*/
|
|
75
|
+
get(scheduledRun: string | ScheduledWorkflows): Promise<ScheduledWorkflows>;
|
|
76
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
exports.ScheduleClient = exports.CreateScheduledRunTriggerSchema = void 0;
|
|
16
|
+
const logger_1 = __importDefault(require("../../../util/logger/logger"));
|
|
17
|
+
const zod_1 = require("zod");
|
|
18
|
+
const axios_1 = require("axios");
|
|
19
|
+
/**
|
|
20
|
+
* Schema for creating a Scheduled Run Trigger.
|
|
21
|
+
*/
|
|
22
|
+
exports.CreateScheduledRunTriggerSchema = zod_1.z.object({
|
|
23
|
+
triggerAt: zod_1.z.coerce.date(),
|
|
24
|
+
input: zod_1.z.record(zod_1.z.any()).optional(),
|
|
25
|
+
additionalMetadata: zod_1.z.record(zod_1.z.string()).optional(),
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* Client for managing Scheduled Runs.
|
|
29
|
+
*/
|
|
30
|
+
class ScheduleClient {
|
|
31
|
+
/**
|
|
32
|
+
* Initializes a new instance of ScheduleClient.
|
|
33
|
+
* @param tenantId - The tenant identifier.
|
|
34
|
+
* @param config - Client configuration settings.
|
|
35
|
+
* @param api - API instance for REST interactions.
|
|
36
|
+
* @param adminClient - Admin client for administrative operations.
|
|
37
|
+
*/
|
|
38
|
+
constructor(tenantId, config, api, adminClient) {
|
|
39
|
+
this.tenantId = tenantId;
|
|
40
|
+
this.config = config;
|
|
41
|
+
this.api = api;
|
|
42
|
+
this.adminClient = adminClient;
|
|
43
|
+
this.logger = new logger_1.default('Scheduled Run', this.config.log_level);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Retrieves the Scheduled Run ID from a ScheduledRun object or a string.
|
|
47
|
+
* @param scheduledRun - The ScheduledRun object or Scheduled Run ID as a string.
|
|
48
|
+
* @returns The Scheduled Run ID as a string.
|
|
49
|
+
*/
|
|
50
|
+
getScheduledRunId(scheduledRun) {
|
|
51
|
+
return typeof scheduledRun === 'string' ? scheduledRun : scheduledRun.metadata.id;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates a new Scheduled Run.
|
|
55
|
+
* @param workflow - The workflow name or Workflow object.
|
|
56
|
+
* @param scheduledRun - The input data for creating the Scheduled Run.
|
|
57
|
+
* @returns A promise that resolves to the created ScheduledWorkflows object.
|
|
58
|
+
* @throws Will throw an error if the input is invalid or the API call fails.
|
|
59
|
+
*/
|
|
60
|
+
create(workflow, cron) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
var _a, _b, _c;
|
|
63
|
+
const workflowId = typeof workflow === 'string' ? workflow : workflow.id;
|
|
64
|
+
// Validate cron input with zod schema
|
|
65
|
+
try {
|
|
66
|
+
const parsedCron = exports.CreateScheduledRunTriggerSchema.parse(cron);
|
|
67
|
+
const response = yield this.api.scheduledWorkflowRunCreate(this.tenantId, workflowId, {
|
|
68
|
+
input: (_a = parsedCron.input) !== null && _a !== void 0 ? _a : {},
|
|
69
|
+
additionalMetadata: (_b = parsedCron.additionalMetadata) !== null && _b !== void 0 ? _b : {},
|
|
70
|
+
triggerAt: parsedCron.triggerAt.toISOString(),
|
|
71
|
+
});
|
|
72
|
+
return response.data;
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
if (err instanceof zod_1.z.ZodError) {
|
|
76
|
+
throw new Error(`Invalid cron input: ${err.message}`);
|
|
77
|
+
}
|
|
78
|
+
if (err instanceof axios_1.AxiosError) {
|
|
79
|
+
throw new Error(JSON.stringify((_c = err.response) === null || _c === void 0 ? void 0 : _c.data.errors));
|
|
80
|
+
}
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Deletes an existing Scheduled Run.
|
|
87
|
+
* @param scheduledRun - The Scheduled Run ID as a string or ScheduledWorkflows object.
|
|
88
|
+
* @returns A promise that resolves when the Scheduled Run is deleted.
|
|
89
|
+
*/
|
|
90
|
+
delete(scheduledRun) {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
const scheduledRunId = this.getScheduledRunId(scheduledRun);
|
|
93
|
+
yield this.api.workflowScheduledDelete(this.tenantId, scheduledRunId);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Lists all Cron Triggers based on the provided query parameters.
|
|
98
|
+
* @param query - Query parameters for listing Scheduled Runs.
|
|
99
|
+
* @returns A promise that resolves to a ScheduledWorkflowsList object.
|
|
100
|
+
*/
|
|
101
|
+
list(query) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
const response = yield this.api.workflowScheduledList(this.tenantId, query);
|
|
104
|
+
return response.data;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Retrieves a specific Scheduled Run by its ID.
|
|
109
|
+
* @param scheduledRun - The Scheduled Run ID as a string or ScheduledWorkflows object.
|
|
110
|
+
* @returns A promise that resolves to the ScheduledWorkflows object.
|
|
111
|
+
*/
|
|
112
|
+
get(scheduledRun) {
|
|
113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
const scheduledRunId = this.getScheduledRunId(scheduledRun);
|
|
115
|
+
const response = yield this.api.workflowScheduledGet(this.tenantId, scheduledRunId);
|
|
116
|
+
return response.data;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.ScheduleClient = ScheduleClient;
|
|
@@ -9,7 +9,8 @@ import { AxiosRequestConfig } from 'axios';
|
|
|
9
9
|
import { ClientConfig } from './client-config';
|
|
10
10
|
import { ListenerClient } from '../listener/listener-client';
|
|
11
11
|
import { Api } from '../rest/generated/Api';
|
|
12
|
-
import {
|
|
12
|
+
import { CronClient } from './features/cron-client';
|
|
13
|
+
import { ScheduleClient } from './features/schedule-client';
|
|
13
14
|
export interface HatchetClientOptions {
|
|
14
15
|
config_path?: string;
|
|
15
16
|
credentials?: ChannelCredentials;
|
|
@@ -23,10 +24,11 @@ export declare class HatchetClient {
|
|
|
23
24
|
dispatcher: DispatcherClient;
|
|
24
25
|
admin: AdminClient;
|
|
25
26
|
api: Api;
|
|
26
|
-
cloudApi: CloudApi;
|
|
27
27
|
listener: ListenerClient;
|
|
28
28
|
tenantId: string;
|
|
29
29
|
logger: Logger;
|
|
30
|
+
cron: CronClient;
|
|
31
|
+
schedule: ScheduleClient;
|
|
30
32
|
constructor(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosOpts?: AxiosRequestConfig);
|
|
31
33
|
static init(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosConfig?: AxiosRequestConfig): HatchetClient;
|
|
32
34
|
run(workflow: string | Workflow): Promise<Worker>;
|
|
@@ -1,27 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -71,7 +48,9 @@ const worker_1 = require("../worker");
|
|
|
71
48
|
const logger_1 = __importDefault(require("../../util/logger/logger"));
|
|
72
49
|
const client_config_1 = require("./client-config");
|
|
73
50
|
const listener_client_1 = require("../listener/listener-client");
|
|
74
|
-
const rest_1 =
|
|
51
|
+
const rest_1 = __importDefault(require("../rest"));
|
|
52
|
+
const cron_client_1 = require("./features/cron-client");
|
|
53
|
+
const schedule_client_1 = require("./features/schedule-client");
|
|
75
54
|
const channelFactory = (config, credentials) => (0, nice_grpc_1.createChannel)(config.host_port, credentials, {
|
|
76
55
|
'grpc.ssl_target_name_override': config.tls_config.server_name,
|
|
77
56
|
'grpc.keepalive_timeout_ms': 60 * 1000,
|
|
@@ -132,13 +111,15 @@ class HatchetClient {
|
|
|
132
111
|
const clientFactory = (0, nice_grpc_1.createClientFactory)().use((0, exports.addTokenMiddleware)(this.config.token));
|
|
133
112
|
this.tenantId = this.config.tenant_id;
|
|
134
113
|
this.api = (0, rest_1.default)(this.config.api_url, this.config.token, axiosOpts);
|
|
135
|
-
this.cloudApi = (0, rest_1.cloudApi)(this.config.api_url, this.config.token, axiosOpts);
|
|
136
114
|
this.event = new event_client_1.EventClient(this.config, (0, exports.channelFactory)(this.config, this.credentials), clientFactory);
|
|
137
115
|
this.dispatcher = new dispatcher_client_1.DispatcherClient(this.config, (0, exports.channelFactory)(this.config, this.credentials), clientFactory);
|
|
138
116
|
this.listener = new listener_client_1.ListenerClient(this.config, (0, exports.channelFactory)(this.config, this.credentials), clientFactory, this.api);
|
|
139
117
|
this.admin = new admin_client_1.AdminClient(this.config, (0, exports.channelFactory)(this.config, this.credentials), clientFactory, this.api, this.tenantId, this.listener);
|
|
140
118
|
this.logger = new logger_1.default('HatchetClient', this.config.log_level);
|
|
141
119
|
this.logger.info(`Initialized HatchetClient`);
|
|
120
|
+
// Feature Clients
|
|
121
|
+
this.cron = new cron_client_1.CronClient(this.tenantId, this.config, this.api, this.admin);
|
|
122
|
+
this.schedule = new schedule_client_1.ScheduleClient(this.tenantId, this.config, this.api, this.admin);
|
|
142
123
|
}
|
|
143
124
|
static init(config, options, axiosConfig) {
|
|
144
125
|
return new HatchetClient(config, options, axiosConfig);
|
|
@@ -38,8 +38,6 @@ const events_1 = require("events");
|
|
|
38
38
|
const dispatcher_1 = require("../../protoc/dispatcher");
|
|
39
39
|
const abort_controller_x_1 = require("abort-controller-x");
|
|
40
40
|
const sleep_1 = __importDefault(require("../../util/sleep"));
|
|
41
|
-
const DEFAULT_EVENT_LISTENER_RETRY_INTERVAL = 5; // seconds
|
|
42
|
-
const DEFAULT_EVENT_LISTENER_RETRY_COUNT = 20;
|
|
43
41
|
class Streamable {
|
|
44
42
|
constructor(listener, id) {
|
|
45
43
|
this.responseEmitter = new events_1.EventEmitter();
|
|
@@ -71,14 +69,17 @@ class GrpcPooledListener {
|
|
|
71
69
|
init() {
|
|
72
70
|
return __awaiter(this, arguments, void 0, function* (retries = 0) {
|
|
73
71
|
var _a, e_1, _b, _c;
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
let retryCount = retries;
|
|
73
|
+
const MAX_RETRY_INTERVAL = 5000; // 5 seconds in milliseconds
|
|
74
|
+
const BASE_RETRY_INTERVAL = 100; // 0.1 seconds in milliseconds
|
|
76
75
|
if (retries > 0) {
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
const backoffTime = Math.min(BASE_RETRY_INTERVAL * 2 ** (retries - 1), MAX_RETRY_INTERVAL);
|
|
77
|
+
this.client.logger.info(`Retrying in ... ${backoffTime / 1000} seconds`);
|
|
78
|
+
yield (0, sleep_1.default)(backoffTime);
|
|
79
79
|
}
|
|
80
80
|
try {
|
|
81
81
|
this.client.logger.debug('Initializing child-listener');
|
|
82
|
+
this.signal = new AbortController();
|
|
82
83
|
this.listener = this.client.client.subscribeToWorkflowRuns(this.request(), {
|
|
83
84
|
signal: this.signal.signal,
|
|
84
85
|
});
|
|
@@ -89,17 +90,12 @@ class GrpcPooledListener {
|
|
|
89
90
|
_c = _f.value;
|
|
90
91
|
_d = false;
|
|
91
92
|
const event = _c;
|
|
93
|
+
retryCount = 0;
|
|
92
94
|
const emitter = this.subscribers[event.workflowRunId];
|
|
93
95
|
if (emitter) {
|
|
94
96
|
emitter.responseEmitter.emit('response', event);
|
|
95
97
|
if (event.eventType === dispatcher_1.WorkflowRunEventType.WORKFLOW_RUN_EVENT_TYPE_FINISHED) {
|
|
96
98
|
delete this.subscribers[event.workflowRunId];
|
|
97
|
-
if (Object.keys(this.subscribers).length === 0) {
|
|
98
|
-
// FIXME it would be better to cleanup on parent complete
|
|
99
|
-
this.client.logger.debug('All subscriptions finished, cleaning up listener');
|
|
100
|
-
this.signal.abort();
|
|
101
|
-
this.onFinish();
|
|
102
|
-
}
|
|
103
99
|
}
|
|
104
100
|
}
|
|
105
101
|
}
|
|
@@ -111,6 +107,7 @@ class GrpcPooledListener {
|
|
|
111
107
|
}
|
|
112
108
|
finally { if (e_1) throw e_1.error; }
|
|
113
109
|
}
|
|
110
|
+
this.client.logger.debug('Child listener finished');
|
|
114
111
|
}
|
|
115
112
|
catch (e) {
|
|
116
113
|
if ((0, abort_controller_x_1.isAbortError)(e)) {
|
|
@@ -118,7 +115,13 @@ class GrpcPooledListener {
|
|
|
118
115
|
return;
|
|
119
116
|
}
|
|
120
117
|
this.client.logger.error(`Error in child-listener: ${e.message}`);
|
|
121
|
-
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
// it is possible the server hangs up early,
|
|
121
|
+
// restart the listener if we still have subscribers
|
|
122
|
+
this.client.logger.debug(`Child listener loop exited with ${Object.keys(this.subscribers).length} subscribers`);
|
|
123
|
+
this.client.logger.debug(`Restarting child listener retry ${retryCount + 1}`);
|
|
124
|
+
this.init(retryCount + 1);
|
|
122
125
|
}
|
|
123
126
|
});
|
|
124
127
|
}
|