@hatchet-dev/typescript-sdk 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -3
- package/dist/{hatchet/clients → clients}/admin/admin-client.d.ts +2 -2
- package/dist/{hatchet/clients → clients}/admin/admin-client.js +2 -2
- package/dist/{hatchet/clients → clients}/dispatcher/action-listener.d.ts +3 -3
- package/dist/{hatchet/clients → clients}/dispatcher/action-listener.js +2 -2
- package/dist/{hatchet/clients → clients}/dispatcher/dispatcher-client.d.ts +3 -3
- package/dist/{hatchet/clients → clients}/dispatcher/dispatcher-client.js +2 -2
- package/dist/{hatchet/clients → clients}/event/event-client.d.ts +3 -3
- package/dist/{hatchet/clients → clients}/event/event-client.js +2 -2
- package/dist/{hatchet/clients → clients}/hatchet-client/hatchet-client.d.ts +6 -6
- package/dist/{hatchet/clients → clients}/hatchet-client/hatchet-client.js +6 -6
- package/dist/{hatchet/clients → clients}/worker/worker.d.ts +6 -6
- package/dist/{hatchet/clients → clients}/worker/worker.js +5 -5
- package/dist/index.d.ts +2 -0
- package/dist/{hatchet/index.js → index.js} +1 -1
- package/dist/protoc/events/index.js +17 -0
- package/dist/sdk.d.ts +2 -0
- package/dist/{hatchet/sdk.js → sdk.js} +1 -1
- package/dist/{hatchet/step.js → step.js} +1 -1
- package/dist/{hatchet/util → util}/config-loader/config-loader.d.ts +1 -1
- package/dist/{hatchet/util → util}/config-loader/config-loader.js +1 -1
- package/package.json +8 -5
- package/.eslintrc.json +0 -36
- package/.prettierrc.json +0 -6
- package/dist/hatchet/clients/admin/admin-client.test.d.ts +0 -1
- package/dist/hatchet/clients/admin/admin-client.test.js +0 -101
- package/dist/hatchet/clients/dispatcher/action-listener.test.d.ts +0 -4
- package/dist/hatchet/clients/dispatcher/action-listener.test.js +0 -277
- package/dist/hatchet/clients/dispatcher/dispatcher-client.test.d.ts +0 -1
- package/dist/hatchet/clients/dispatcher/dispatcher-client.test.js +0 -99
- package/dist/hatchet/clients/event/event-client.test.d.ts +0 -1
- package/dist/hatchet/clients/event/event-client.test.js +0 -60
- package/dist/hatchet/clients/hatchet-client/hatchet-client.test.d.ts +0 -2
- package/dist/hatchet/clients/hatchet-client/hatchet-client.test.js +0 -135
- package/dist/hatchet/clients/worker/worker.test.d.ts +0 -1
- package/dist/hatchet/clients/worker/worker.test.js +0 -256
- package/dist/hatchet/index.d.ts +0 -2
- package/dist/hatchet/sdk.d.ts +0 -2
- package/dist/hatchet/util/config-loader/config-loader.test.d.ts +0 -1
- package/dist/hatchet/util/config-loader/config-loader.test.js +0 -72
- package/dist/hatchet/util/hatchet-promise/hatchet-promise.test.d.ts +0 -1
- package/dist/hatchet/util/hatchet-promise/hatchet-promise.test.js +0 -40
- package/examples/dag-worker.ts +0 -55
- package/examples/example-event.ts +0 -7
- package/examples/simple-worker.ts +0 -39
- package/generate-protoc.sh +0 -10
- package/hatchet/clients/admin/admin-client.test.ts +0 -116
- package/hatchet/clients/admin/admin-client.ts +0 -43
- package/hatchet/clients/dispatcher/action-listener.test.ts +0 -270
- package/hatchet/clients/dispatcher/action-listener.ts +0 -91
- package/hatchet/clients/dispatcher/dispatcher-client.test.ts +0 -111
- package/hatchet/clients/dispatcher/dispatcher-client.ts +0 -47
- package/hatchet/clients/event/event-client.test.ts +0 -72
- package/hatchet/clients/event/event-client.ts +0 -32
- package/hatchet/clients/hatchet-client/client-config.ts +0 -22
- package/hatchet/clients/hatchet-client/fixtures/.hatchet-invalid.yaml +0 -6
- package/hatchet/clients/hatchet-client/fixtures/.hatchet.yaml +0 -8
- package/hatchet/clients/hatchet-client/hatchet-client.test.ts +0 -162
- package/hatchet/clients/hatchet-client/hatchet-client.ts +0 -136
- package/hatchet/clients/hatchet-client/index.ts +0 -2
- package/hatchet/clients/worker/index.ts +0 -1
- package/hatchet/clients/worker/worker.test.ts +0 -347
- package/hatchet/clients/worker/worker.ts +0 -229
- package/hatchet/index.ts +0 -3
- package/hatchet/sdk.ts +0 -3
- package/hatchet/step.ts +0 -48
- package/hatchet/util/config-loader/config-loader.test.ts +0 -79
- package/hatchet/util/config-loader/config-loader.ts +0 -91
- package/hatchet/util/config-loader/fixtures/.hatchet-invalid.yaml +0 -6
- package/hatchet/util/config-loader/fixtures/.hatchet.yaml +0 -8
- package/hatchet/util/config-loader/index.ts +0 -1
- package/hatchet/util/errors/hatchet-error.ts +0 -8
- package/hatchet/util/hatchet-promise/hatchet-promise.test.ts +0 -32
- package/hatchet/util/hatchet-promise/hatchet-promise.ts +0 -13
- package/hatchet/util/logger/index.ts +0 -1
- package/hatchet/util/logger/logger.ts +0 -44
- package/hatchet/util/sleep.ts +0 -6
- package/hatchet/workflow.ts +0 -30
- package/jest.config.ts +0 -205
- package/protoc/dispatcher/dispatcher.ts +0 -1101
- package/protoc/dispatcher/index.ts +0 -1
- package/protoc/events/events.ts +0 -519
- package/protoc/google/protobuf/timestamp.ts +0 -210
- package/protoc/google/protobuf/wrappers.ts +0 -657
- package/protoc/workflows/index.ts +0 -1
- package/protoc/workflows/workflows.ts +0 -2158
- package/tsconfig.json +0 -120
- /package/dist/{hatchet/clients → clients}/hatchet-client/client-config.d.ts +0 -0
- /package/dist/{hatchet/clients → clients}/hatchet-client/client-config.js +0 -0
- /package/dist/{hatchet/clients → clients}/hatchet-client/index.d.ts +0 -0
- /package/dist/{hatchet/clients → clients}/hatchet-client/index.js +0 -0
- /package/dist/{hatchet/clients → clients}/worker/index.d.ts +0 -0
- /package/dist/{hatchet/clients → clients}/worker/index.js +0 -0
- /package/{protoc/events/index.ts → dist/protoc/events/index.d.ts} +0 -0
- /package/dist/{hatchet/step.d.ts → step.d.ts} +0 -0
- /package/dist/{hatchet/util → util}/config-loader/index.d.ts +0 -0
- /package/dist/{hatchet/util → util}/config-loader/index.js +0 -0
- /package/dist/{hatchet/util → util}/errors/hatchet-error.d.ts +0 -0
- /package/dist/{hatchet/util → util}/errors/hatchet-error.js +0 -0
- /package/dist/{hatchet/util → util}/hatchet-promise/hatchet-promise.d.ts +0 -0
- /package/dist/{hatchet/util → util}/hatchet-promise/hatchet-promise.js +0 -0
- /package/dist/{hatchet/util → util}/logger/index.d.ts +0 -0
- /package/dist/{hatchet/util → util}/logger/index.js +0 -0
- /package/dist/{hatchet/util → util}/logger/logger.d.ts +0 -0
- /package/dist/{hatchet/util → util}/logger/logger.js +0 -0
- /package/dist/{hatchet/util → util}/sleep.d.ts +0 -0
- /package/dist/{hatchet/util → util}/sleep.js +0 -0
- /package/dist/{hatchet/workflow.d.ts → workflow.d.ts} +0 -0
- /package/dist/{hatchet/workflow.js → workflow.js} +0 -0
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
import { HatchetClient } from '@clients/hatchet-client';
|
|
2
|
-
import HatchetError from '@util/errors/hatchet-error';
|
|
3
|
-
import { Action, ActionListener } from '@clients/dispatcher/action-listener';
|
|
4
|
-
import { ActionEvent, ActionEventType, ActionType } from '@protoc/dispatcher';
|
|
5
|
-
import HatchetPromise from '@util/hatchet-promise/hatchet-promise';
|
|
6
|
-
import { Workflow } from '@hatchet/workflow';
|
|
7
|
-
import { CreateWorkflowStepOpts } from '@protoc/workflows';
|
|
8
|
-
import { Logger } from '@hatchet/util/logger';
|
|
9
|
-
import sleep from '@hatchet/util/sleep';
|
|
10
|
-
import { Context } from '../../step';
|
|
11
|
-
|
|
12
|
-
export type ActionRegistry = Record<Action['actionId'], Function>;
|
|
13
|
-
|
|
14
|
-
export class Worker {
|
|
15
|
-
serviceName = 'default';
|
|
16
|
-
client: HatchetClient;
|
|
17
|
-
name: string;
|
|
18
|
-
killing: boolean;
|
|
19
|
-
handle_kill: boolean;
|
|
20
|
-
|
|
21
|
-
action_registry: ActionRegistry;
|
|
22
|
-
listener: ActionListener | undefined;
|
|
23
|
-
futures: Record<Action['stepRunId'], HatchetPromise<any>> = {};
|
|
24
|
-
|
|
25
|
-
logger: Logger;
|
|
26
|
-
|
|
27
|
-
constructor(client: HatchetClient, options: { name: string; handleKill?: boolean }) {
|
|
28
|
-
this.client = client;
|
|
29
|
-
this.name = options.name;
|
|
30
|
-
this.action_registry = {};
|
|
31
|
-
|
|
32
|
-
process.on('SIGTERM', () => this.exit_gracefully());
|
|
33
|
-
process.on('SIGINT', () => this.exit_gracefully());
|
|
34
|
-
|
|
35
|
-
this.killing = false;
|
|
36
|
-
this.handle_kill = options.handleKill === undefined ? true : options.handleKill;
|
|
37
|
-
|
|
38
|
-
this.logger = new Logger(`Worker/${this.name}`, this.client.config.log_level);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async register_workflow(workflow: Workflow, options?: { autoVersion?: boolean }) {
|
|
42
|
-
try {
|
|
43
|
-
await this.client.admin.put_workflow(
|
|
44
|
-
{
|
|
45
|
-
name: workflow.id,
|
|
46
|
-
description: workflow.description,
|
|
47
|
-
version: 'v0.55.0', // FIXME workflow.version,
|
|
48
|
-
eventTriggers: workflow.on.event ? [workflow.on.event] : [],
|
|
49
|
-
cronTriggers: workflow.on.cron ? [workflow.on.cron] : [],
|
|
50
|
-
scheduledTriggers: [],
|
|
51
|
-
jobs: [
|
|
52
|
-
{
|
|
53
|
-
name: 'my-job', // FIXME variable names
|
|
54
|
-
timeout: '60s',
|
|
55
|
-
description: 'my-job',
|
|
56
|
-
steps: workflow.steps.map<CreateWorkflowStepOpts>((step) => ({
|
|
57
|
-
readableId: step.name,
|
|
58
|
-
action: `${this.serviceName}:${step.name}`,
|
|
59
|
-
timeout: '60s',
|
|
60
|
-
inputs: '{}',
|
|
61
|
-
parents: step.parents ?? [],
|
|
62
|
-
})),
|
|
63
|
-
},
|
|
64
|
-
],
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
autoVersion: !options?.autoVersion,
|
|
68
|
-
}
|
|
69
|
-
);
|
|
70
|
-
} catch (e: any) {
|
|
71
|
-
throw new HatchetError(`Could not register workflow: ${e.message}`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
this.action_registry = workflow.steps.reduce<ActionRegistry>((acc, step) => {
|
|
75
|
-
acc[`${this.serviceName}:${step.name}`] = step.run;
|
|
76
|
-
return acc;
|
|
77
|
-
}, {});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
handle_start_step_run(action: Action) {
|
|
81
|
-
const { actionId } = action;
|
|
82
|
-
const context = new Context(action.actionPayload);
|
|
83
|
-
|
|
84
|
-
const step = this.action_registry[actionId];
|
|
85
|
-
if (!step) {
|
|
86
|
-
this.logger.error(`Could not find step '${actionId}'`);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const run = async () => {
|
|
91
|
-
return step(context);
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
const success = (result: any) => {
|
|
95
|
-
this.logger.info(`Step run ${action.stepRunId} succeeded`);
|
|
96
|
-
|
|
97
|
-
try {
|
|
98
|
-
// Send the action event to the dispatcher
|
|
99
|
-
const event = this.get_action_event(
|
|
100
|
-
action,
|
|
101
|
-
ActionEventType.STEP_EVENT_TYPE_COMPLETED,
|
|
102
|
-
result
|
|
103
|
-
);
|
|
104
|
-
this.client.dispatcher.send_action_event(event);
|
|
105
|
-
|
|
106
|
-
// delete the run from the futures
|
|
107
|
-
delete this.futures[action.stepRunId];
|
|
108
|
-
} catch (e: any) {
|
|
109
|
-
this.logger.error(`Could not send action event: ${e.message}`);
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
const failure = (error: any) => {
|
|
114
|
-
this.logger.error(`Step run ${action.stepRunId} failed: ${error.message}`);
|
|
115
|
-
|
|
116
|
-
try {
|
|
117
|
-
// Send the action event to the dispatcher
|
|
118
|
-
const event = this.get_action_event(action, ActionEventType.STEP_EVENT_TYPE_FAILED, error);
|
|
119
|
-
this.client.dispatcher.send_action_event(event);
|
|
120
|
-
// delete the run from the futures
|
|
121
|
-
delete this.futures[action.stepRunId];
|
|
122
|
-
} catch (e: any) {
|
|
123
|
-
this.logger.error(`Could not send action event: ${e.message}`);
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
const future = new HatchetPromise(run().then(success).catch(failure));
|
|
128
|
-
this.futures[action.stepRunId] = future;
|
|
129
|
-
|
|
130
|
-
try {
|
|
131
|
-
// Send the action event to the dispatcher
|
|
132
|
-
const event = this.get_action_event(action, ActionEventType.STEP_EVENT_TYPE_STARTED);
|
|
133
|
-
this.client.dispatcher.send_action_event(event);
|
|
134
|
-
} catch (e: any) {
|
|
135
|
-
this.logger.error(`Could not send action event: ${e.message}`);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
get_action_event(action: Action, eventType: ActionEventType, payload: any = ''): ActionEvent {
|
|
140
|
-
return {
|
|
141
|
-
workerId: this.name,
|
|
142
|
-
jobId: action.jobId,
|
|
143
|
-
jobRunId: action.jobRunId,
|
|
144
|
-
stepId: action.stepId,
|
|
145
|
-
stepRunId: action.stepRunId,
|
|
146
|
-
actionId: action.actionId,
|
|
147
|
-
eventTimestamp: new Date(),
|
|
148
|
-
eventType,
|
|
149
|
-
eventPayload: JSON.stringify(payload),
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
handle_cancel_step_run(action: Action) {
|
|
154
|
-
const { stepRunId } = action;
|
|
155
|
-
const future = this.futures[stepRunId];
|
|
156
|
-
if (future) {
|
|
157
|
-
future.cancel();
|
|
158
|
-
delete this.futures[stepRunId];
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
async stop() {
|
|
163
|
-
await this.exit_gracefully();
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
async exit_gracefully() {
|
|
167
|
-
this.killing = true;
|
|
168
|
-
|
|
169
|
-
this.logger.info('Starting to exit...');
|
|
170
|
-
|
|
171
|
-
try {
|
|
172
|
-
this.listener?.unregister();
|
|
173
|
-
} catch (e: any) {
|
|
174
|
-
this.logger.error(`Could not unregister listener: ${e.message}`);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
this.logger.info('Gracefully exiting hatchet worker, running tasks will attempt to finish...');
|
|
178
|
-
|
|
179
|
-
// attempt to wait for futures to finish
|
|
180
|
-
await Promise.all(Object.values(this.futures).map(({ promise }) => promise));
|
|
181
|
-
|
|
182
|
-
if (this.handle_kill) {
|
|
183
|
-
this.logger.info('Exiting hatchet worker...');
|
|
184
|
-
process.exit(0);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
async start() {
|
|
189
|
-
let retries = 0;
|
|
190
|
-
|
|
191
|
-
while (retries < 5) {
|
|
192
|
-
try {
|
|
193
|
-
this.listener = await this.client.dispatcher.get_action_listener({
|
|
194
|
-
workerName: this.name,
|
|
195
|
-
services: ['default'],
|
|
196
|
-
actions: Object.keys(this.action_registry),
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
const generator = this.listener.actions();
|
|
200
|
-
|
|
201
|
-
this.logger.info(`Worker ${this.name} listening for actions`);
|
|
202
|
-
|
|
203
|
-
for await (const action of generator) {
|
|
204
|
-
this.logger.info(`Worker ${this.name} received action ${action.actionId}`);
|
|
205
|
-
|
|
206
|
-
if (action.actionType === ActionType.START_STEP_RUN) {
|
|
207
|
-
this.handle_start_step_run(action);
|
|
208
|
-
} else if (action.actionType === ActionType.CANCEL_STEP_RUN) {
|
|
209
|
-
this.handle_cancel_step_run(action);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
break;
|
|
214
|
-
} catch (e: any) {
|
|
215
|
-
this.logger.error(`Could not start worker: ${e.message}`);
|
|
216
|
-
retries += 1;
|
|
217
|
-
const wait = 500;
|
|
218
|
-
this.logger.error(`Could not start worker, retrying in ${500} seconds`);
|
|
219
|
-
await sleep(wait);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (this.killing) return;
|
|
224
|
-
|
|
225
|
-
if (retries > 5) {
|
|
226
|
-
throw new HatchetError('Could not start worker after 5 retries');
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
package/hatchet/index.ts
DELETED
package/hatchet/sdk.ts
DELETED
package/hatchet/step.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import HatchetError from '@util/errors/hatchet-error';
|
|
2
|
-
import * as z from 'zod';
|
|
3
|
-
|
|
4
|
-
export const CreateStepSchema = z.object({
|
|
5
|
-
name: z.string(),
|
|
6
|
-
parents: z.array(z.string()).optional(),
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
export type NextStep = { [key: string]: string };
|
|
10
|
-
|
|
11
|
-
interface ContextData<T = unknown> {
|
|
12
|
-
input: T;
|
|
13
|
-
parents: Record<string, any>;
|
|
14
|
-
triggered_by_event: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class Context<T = unknown> {
|
|
18
|
-
data: ContextData<T>;
|
|
19
|
-
constructor(payload: string) {
|
|
20
|
-
try {
|
|
21
|
-
this.data = JSON.parse(JSON.parse(payload));
|
|
22
|
-
} catch (e: any) {
|
|
23
|
-
throw new HatchetError(`Could not parse payload: ${e.message}`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
stepOutput(step: string): string {
|
|
28
|
-
if (!this.data.parents) {
|
|
29
|
-
throw new HatchetError('Step output not found');
|
|
30
|
-
}
|
|
31
|
-
if (!this.data.parents[step]) {
|
|
32
|
-
throw new HatchetError(`Step output for '${step}' not found`);
|
|
33
|
-
}
|
|
34
|
-
return this.data.parents[step];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
triggeredByEvent(): boolean {
|
|
38
|
-
return this.data?.triggered_by_event === 'event';
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
workflowInput(): any {
|
|
42
|
-
return this.data?.input || {};
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export interface CreateStep<T> extends z.infer<typeof CreateStepSchema> {
|
|
47
|
-
run: (ctx: Context) => Promise<NextStep> | NextStep | void;
|
|
48
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { ConfigLoader } from './config-loader';
|
|
2
|
-
|
|
3
|
-
fdescribe('ConfigLoader', () => {
|
|
4
|
-
beforeEach(() => {
|
|
5
|
-
process.env.HATCHET_CLIENT_HOST_PORT = 'HOST_PORT';
|
|
6
|
-
process.env.HATCHET_CLIENT_TLS_CERT_FILE = 'TLS_CERT_FILE';
|
|
7
|
-
process.env.HATCHET_CLIENT_TLS_KEY_FILE = 'TLS_KEY_FILE';
|
|
8
|
-
process.env.HATCHET_CLIENT_TLS_ROOT_CA_FILE = 'TLS_ROOT_CA_FILE';
|
|
9
|
-
process.env.HATCHET_CLIENT_TLS_SERVER_NAME = 'TLS_SERVER_NAME';
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('should load from environment variables', () => {
|
|
13
|
-
const config = ConfigLoader.load_client_config();
|
|
14
|
-
expect(config).toEqual({
|
|
15
|
-
host_port: 'HOST_PORT',
|
|
16
|
-
log_level: 'INFO',
|
|
17
|
-
tls_config: {
|
|
18
|
-
tls_strategy: 'tls',
|
|
19
|
-
cert_file: 'TLS_CERT_FILE',
|
|
20
|
-
key_file: 'TLS_KEY_FILE',
|
|
21
|
-
ca_file: 'TLS_ROOT_CA_FILE',
|
|
22
|
-
server_name: 'TLS_SERVER_NAME',
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('should throw an error if the file is not found', () => {
|
|
28
|
-
expect(() =>
|
|
29
|
-
ConfigLoader.load_client_config({
|
|
30
|
-
path: './fixtures/not-found.yaml',
|
|
31
|
-
})
|
|
32
|
-
).toThrow();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
xit('should throw an error if the yaml file fails validation', () => {
|
|
36
|
-
expect(() =>
|
|
37
|
-
// This test is failing because there is no invalid state of the yaml file, need to update with tls and mtls settings
|
|
38
|
-
ConfigLoader.load_client_config({
|
|
39
|
-
path: './fixtures/.hatchet-invalid.yaml',
|
|
40
|
-
})
|
|
41
|
-
).toThrow();
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('should favor yaml config over env vars', () => {
|
|
45
|
-
const config = ConfigLoader.load_client_config({
|
|
46
|
-
path: './fixtures/.hatchet.yaml',
|
|
47
|
-
});
|
|
48
|
-
expect(config).toEqual({
|
|
49
|
-
token: 'TOKEN_YAML',
|
|
50
|
-
host_port: 'HOST_PORT_YAML',
|
|
51
|
-
log_level: 'INFO',
|
|
52
|
-
tls_config: {
|
|
53
|
-
tls_strategy: 'tls',
|
|
54
|
-
cert_file: 'TLS_CERT_FILE_YAML',
|
|
55
|
-
key_file: 'TLS_KEY_FILE_YAML',
|
|
56
|
-
ca_file: 'TLS_ROOT_CA_FILE_YAML',
|
|
57
|
-
server_name: 'TLS_SERVER_NAME_YAML',
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
xit('should attempt to load the root .hatchet.yaml config', () => {
|
|
63
|
-
// i'm not sure the best way to test this, maybe spy on readFileSync called with
|
|
64
|
-
const config = ConfigLoader.load_client_config({
|
|
65
|
-
path: './fixtures/.hatchet.yaml',
|
|
66
|
-
});
|
|
67
|
-
expect(config).toEqual({
|
|
68
|
-
token: 'TOKEN_YAML',
|
|
69
|
-
host_port: 'HOST_PORT_YAML',
|
|
70
|
-
tls_config: {
|
|
71
|
-
tls_strategy: 'tls',
|
|
72
|
-
cert_file: 'TLS_CERT_FILE_YAML',
|
|
73
|
-
key_file: 'TLS_KEY_FILE_YAML',
|
|
74
|
-
ca_file: 'TLS_ROOT_CA_FILE_YAML',
|
|
75
|
-
server_name: 'TLS_SERVER_NAME_YAML',
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
});
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { parse } from 'yaml';
|
|
2
|
-
import { readFileSync } from 'fs';
|
|
3
|
-
import * as p from 'path';
|
|
4
|
-
import { z } from 'zod';
|
|
5
|
-
import { ClientConfig, ClientConfigSchema } from '@clients/hatchet-client';
|
|
6
|
-
import { ChannelCredentials } from 'nice-grpc';
|
|
7
|
-
import { LogLevel } from '../logger/logger';
|
|
8
|
-
|
|
9
|
-
type EnvVars =
|
|
10
|
-
| 'HATCHET_CLIENT_TOKEN'
|
|
11
|
-
| 'HATCHET_CLIENT_TLS_STRATEGY'
|
|
12
|
-
| 'HATCHET_CLIENT_HOST_PORT'
|
|
13
|
-
| 'HATCHET_CLIENT_TLS_CERT_FILE'
|
|
14
|
-
| 'HATCHET_CLIENT_TLS_KEY_FILE'
|
|
15
|
-
| 'HATCHET_CLIENT_TLS_ROOT_CA_FILE'
|
|
16
|
-
| 'HATCHET_CLIENT_TLS_SERVER_NAME'
|
|
17
|
-
| 'HATCHET_CLIENT_LOG_LEVEL';
|
|
18
|
-
|
|
19
|
-
type TLSStrategy = 'tls' | 'mtls';
|
|
20
|
-
|
|
21
|
-
interface LoadClientConfigOptions {
|
|
22
|
-
path?: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const DEFAULT_CONFIG_FILE = '.hatchet.yaml';
|
|
26
|
-
|
|
27
|
-
export class ConfigLoader {
|
|
28
|
-
static load_client_config(config?: LoadClientConfigOptions): Partial<ClientConfig> {
|
|
29
|
-
const yaml = this.load_yaml_config(config?.path);
|
|
30
|
-
const tlsConfig = {
|
|
31
|
-
tls_strategy:
|
|
32
|
-
yaml?.tls_config?.tls_strategy ??
|
|
33
|
-
(this.env('HATCHET_CLIENT_TLS_STRATEGY') as TLSStrategy | undefined) ??
|
|
34
|
-
'tls',
|
|
35
|
-
cert_file: yaml?.tls_config?.cert_file ?? this.env('HATCHET_CLIENT_TLS_CERT_FILE')!,
|
|
36
|
-
key_file: yaml?.tls_config?.key_file ?? this.env('HATCHET_CLIENT_TLS_KEY_FILE')!,
|
|
37
|
-
ca_file: yaml?.tls_config?.ca_file ?? this.env('HATCHET_CLIENT_TLS_ROOT_CA_FILE')!,
|
|
38
|
-
server_name: yaml?.tls_config?.server_name ?? this.env('HATCHET_CLIENT_TLS_SERVER_NAME')!,
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
token: yaml?.token ?? this.env('HATCHET_CLIENT_TOKEN'),
|
|
43
|
-
host_port: yaml?.host_port ?? this.env('HATCHET_CLIENT_HOST_PORT'),
|
|
44
|
-
tls_config: tlsConfig,
|
|
45
|
-
log_level: yaml?.log_level ?? (this.env('HATCHET_CLIENT_LOG_LEVEL') as LogLevel) ?? 'INFO',
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
static get default_yaml_config_path() {
|
|
50
|
-
return p.join(process.cwd(), DEFAULT_CONFIG_FILE);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
static createCredentials(config: ClientConfig['tls_config']): ChannelCredentials {
|
|
54
|
-
if (config.tls_strategy === 'tls') {
|
|
55
|
-
const rootCerts = config.ca_file ? readFileSync(config.ca_file) : undefined;
|
|
56
|
-
return ChannelCredentials.createSsl(rootCerts);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const rootCerts = config.ca_file ? readFileSync(config.ca_file) : null;
|
|
60
|
-
const privateKey = config.key_file ? readFileSync(config.key_file) : null;
|
|
61
|
-
const certChain = config.cert_file ? readFileSync(config.cert_file) : null;
|
|
62
|
-
return ChannelCredentials.createSsl(rootCerts, privateKey, certChain);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
static load_yaml_config(path?: string): ClientConfig | undefined {
|
|
66
|
-
try {
|
|
67
|
-
const configFile = readFileSync(
|
|
68
|
-
p.join(__dirname, path ?? this.default_yaml_config_path),
|
|
69
|
-
'utf8'
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
const config = parse(configFile);
|
|
73
|
-
|
|
74
|
-
ClientConfigSchema.partial().parse(config);
|
|
75
|
-
|
|
76
|
-
return config as ClientConfig;
|
|
77
|
-
} catch (e) {
|
|
78
|
-
if (!path) return undefined;
|
|
79
|
-
|
|
80
|
-
if (e instanceof z.ZodError) {
|
|
81
|
-
throw new Error(`Invalid yaml config: ${e.message}`);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
throw e;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
private static env(name: EnvVars): string | undefined {
|
|
89
|
-
return process.env[name];
|
|
90
|
-
}
|
|
91
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './config-loader';
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import HatchetPromise from './hatchet-promise';
|
|
2
|
-
|
|
3
|
-
describe('HatchetPromise', () => {
|
|
4
|
-
it('should resolve the original promise if not canceled', async () => {
|
|
5
|
-
const hatchetPromise = new HatchetPromise(
|
|
6
|
-
new Promise((resolve) => {
|
|
7
|
-
setTimeout(() => resolve('RESOLVED'), 500);
|
|
8
|
-
})
|
|
9
|
-
);
|
|
10
|
-
const result = await hatchetPromise.promise;
|
|
11
|
-
expect(result).toEqual('RESOLVED');
|
|
12
|
-
});
|
|
13
|
-
it('should resolve the cancel promise if canceled', async () => {
|
|
14
|
-
const hatchetPromise = new HatchetPromise(
|
|
15
|
-
new Promise((resolve) => {
|
|
16
|
-
setTimeout(() => resolve('RESOLVED'), 500);
|
|
17
|
-
})
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
const result = hatchetPromise.promise;
|
|
21
|
-
setTimeout(() => {
|
|
22
|
-
hatchetPromise.cancel();
|
|
23
|
-
}, 100);
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
await result;
|
|
27
|
-
expect(true).toEqual(false); // this should not be reached
|
|
28
|
-
} catch (e) {
|
|
29
|
-
expect(e).toEqual(undefined);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
});
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
class HatchetPromise<T> {
|
|
2
|
-
cancel: Function = (reason: any) => {};
|
|
3
|
-
promise: Promise<T>;
|
|
4
|
-
|
|
5
|
-
constructor(promise: Promise<T>) {
|
|
6
|
-
this.promise = new Promise((resolve, reject) => {
|
|
7
|
-
this.cancel = reject;
|
|
8
|
-
Promise.resolve(promise).then(resolve).catch(reject);
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default HatchetPromise;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './logger';
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
export type LogLevel = 'OFF' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR';
|
|
2
|
-
|
|
3
|
-
// eslint-disable-next-line no-shadow
|
|
4
|
-
enum LogLevelEnum {
|
|
5
|
-
OFF = -1,
|
|
6
|
-
DEBUG = 0,
|
|
7
|
-
INFO = 1,
|
|
8
|
-
WARN = 2,
|
|
9
|
-
ERROR = 3,
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class Logger {
|
|
13
|
-
private logLevel: LogLevel;
|
|
14
|
-
private context: string;
|
|
15
|
-
|
|
16
|
-
constructor(context: string, logLevel: LogLevel = 'INFO') {
|
|
17
|
-
this.logLevel = logLevel;
|
|
18
|
-
this.context = context;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
private log(level: LogLevel, message: string): void {
|
|
22
|
-
if (LogLevelEnum[level] >= LogLevelEnum[this.logLevel]) {
|
|
23
|
-
console.log(`🪓 [${level}/${this.context}] ${message}`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
debug(message: string): void {
|
|
28
|
-
this.log('DEBUG', message);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
info(message: string): void {
|
|
32
|
-
this.log('INFO', message);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
warn(message: string): void {
|
|
36
|
-
this.log('WARN', message);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
error(message: string): void {
|
|
40
|
-
this.log('ERROR', message);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export default Logger;
|
package/hatchet/util/sleep.ts
DELETED
package/hatchet/workflow.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import * as z from 'zod';
|
|
2
|
-
|
|
3
|
-
import { CreateStep, CreateStepSchema } from './step';
|
|
4
|
-
|
|
5
|
-
const CronConfigSchema = z.object({
|
|
6
|
-
cron: z.string(),
|
|
7
|
-
event: z.undefined(),
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
const EventConfigSchema = z.object({
|
|
11
|
-
cron: z.undefined(),
|
|
12
|
-
event: z.string(),
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
const OnConfigSchema = z.union([CronConfigSchema, EventConfigSchema]);
|
|
16
|
-
|
|
17
|
-
const StepsSchema = z.array(CreateStepSchema);
|
|
18
|
-
|
|
19
|
-
export type Steps = z.infer<typeof StepsSchema>;
|
|
20
|
-
|
|
21
|
-
export const CreateWorkflowSchema = z.object({
|
|
22
|
-
id: z.string(),
|
|
23
|
-
description: z.string(),
|
|
24
|
-
on: OnConfigSchema,
|
|
25
|
-
steps: StepsSchema,
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
export interface Workflow extends z.infer<typeof CreateWorkflowSchema> {
|
|
29
|
-
steps: CreateStep<any>[];
|
|
30
|
-
}
|