@hatchet-dev/typescript-sdk 0.0.2 → 0.0.7
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/package.json +27 -19
- package/README.md +0 -4
- package/dist/clients/admin/admin-client.d.ts +0 -14
- package/dist/clients/admin/admin-client.js +0 -50
- package/dist/clients/admin/admin-client.test.d.ts +0 -1
- package/dist/clients/admin/admin-client.test.js +0 -101
- package/dist/clients/dispatcher/action-listener.d.ts +0 -24
- package/dist/clients/dispatcher/action-listener.js +0 -113
- package/dist/clients/dispatcher/action-listener.test.d.ts +0 -4
- package/dist/clients/dispatcher/action-listener.test.js +0 -277
- package/dist/clients/dispatcher/dispatcher-client.d.ts +0 -17
- package/dist/clients/dispatcher/dispatcher-client.js +0 -46
- package/dist/clients/dispatcher/dispatcher-client.test.d.ts +0 -1
- package/dist/clients/dispatcher/dispatcher-client.test.js +0 -99
- package/dist/clients/event/event-client.d.ts +0 -9
- package/dist/clients/event/event-client.js +0 -28
- package/dist/clients/event/event-client.test.d.ts +0 -1
- package/dist/clients/event/event-client.test.js +0 -60
- package/dist/clients/hatchet-client/client-config.d.ts +0 -72
- package/dist/clients/hatchet-client/client-config.js +0 -17
- package/dist/clients/hatchet-client/hatchet-client.d.ts +0 -26
- package/dist/clients/hatchet-client/hatchet-client.js +0 -133
- package/dist/clients/hatchet-client/hatchet-client.test.d.ts +0 -2
- package/dist/clients/hatchet-client/hatchet-client.test.js +0 -135
- package/dist/clients/hatchet-client/index.d.ts +0 -2
- package/dist/clients/hatchet-client/index.js +0 -18
- package/dist/clients/worker/index.d.ts +0 -1
- package/dist/clients/worker/index.js +0 -17
- package/dist/clients/worker/worker.d.ts +0 -31
- package/dist/clients/worker/worker.js +0 -228
- package/dist/clients/worker/worker.test.d.ts +0 -1
- package/dist/clients/worker/worker.test.js +0 -256
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -4
- package/dist/protoc/dispatcher/dispatcher.d.ts +0 -273
- package/dist/protoc/dispatcher/dispatcher.js +0 -918
- package/dist/protoc/dispatcher/index.d.ts +0 -1
- package/dist/protoc/dispatcher/index.js +0 -17
- package/dist/protoc/events/events.d.ts +0 -165
- package/dist/protoc/events/events.js +0 -443
- package/dist/protoc/events/index.d.ts +0 -1
- package/dist/protoc/events/index.js +0 -17
- package/dist/protoc/google/protobuf/timestamp.d.ts +0 -121
- package/dist/protoc/google/protobuf/timestamp.js +0 -110
- package/dist/protoc/google/protobuf/wrappers.d.ts +0 -160
- package/dist/protoc/google/protobuf/wrappers.js +0 -527
- package/dist/protoc/workflows/index.d.ts +0 -1
- package/dist/protoc/workflows/index.js +0 -17
- package/dist/protoc/workflows/workflows.d.ts +0 -438
- package/dist/protoc/workflows/workflows.js +0 -1814
- package/dist/sdk.d.ts +0 -2
- package/dist/sdk.js +0 -4
- package/dist/step.d.ts +0 -30
- package/dist/step.js +0 -63
- package/dist/util/config-loader/config-loader.d.ts +0 -13
- package/dist/util/config-loader/config-loader.js +0 -85
- package/dist/util/config-loader/config-loader.test.d.ts +0 -1
- package/dist/util/config-loader/config-loader.test.js +0 -72
- package/dist/util/config-loader/index.d.ts +0 -1
- package/dist/util/config-loader/index.js +0 -17
- package/dist/util/errors/hatchet-error.d.ts +0 -4
- package/dist/util/errors/hatchet-error.js +0 -9
- package/dist/util/hatchet-promise/hatchet-promise.d.ts +0 -6
- package/dist/util/hatchet-promise/hatchet-promise.js +0 -12
- package/dist/util/hatchet-promise/hatchet-promise.test.d.ts +0 -1
- package/dist/util/hatchet-promise/hatchet-promise.test.js +0 -40
- package/dist/util/logger/index.d.ts +0 -1
- package/dist/util/logger/index.js +0 -17
- package/dist/util/logger/logger.d.ts +0 -12
- package/dist/util/logger/logger.js +0 -37
- package/dist/util/sleep.d.ts +0 -2
- package/dist/util/sleep.js +0 -6
- package/dist/workflow.d.ts +0 -78
- package/dist/workflow.js +0 -44
|
@@ -1,18 +0,0 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./hatchet-client"), exports);
|
|
18
|
-
__exportStar(require("./client-config"), exports);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './worker';
|
|
@@ -1,17 +0,0 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./worker"), exports);
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { HatchetClient } from '../hatchet-client';
|
|
2
|
-
import { Action, ActionListener } from '../dispatcher/action-listener';
|
|
3
|
-
import { ActionEvent, ActionEventType } from '@hatchet/protoc/dispatcher';
|
|
4
|
-
import HatchetPromise from '../../util/hatchet-promise/hatchet-promise';
|
|
5
|
-
import { Workflow } from '@hatchet/workflow';
|
|
6
|
-
import { Logger } from '@hatchet/util/logger';
|
|
7
|
-
export type ActionRegistry = Record<Action['actionId'], Function>;
|
|
8
|
-
export declare class Worker {
|
|
9
|
-
serviceName: string;
|
|
10
|
-
client: HatchetClient;
|
|
11
|
-
name: string;
|
|
12
|
-
killing: boolean;
|
|
13
|
-
handle_kill: boolean;
|
|
14
|
-
action_registry: ActionRegistry;
|
|
15
|
-
listener: ActionListener | undefined;
|
|
16
|
-
futures: Record<Action['stepRunId'], HatchetPromise<any>>;
|
|
17
|
-
logger: Logger;
|
|
18
|
-
constructor(client: HatchetClient, options: {
|
|
19
|
-
name: string;
|
|
20
|
-
handleKill?: boolean;
|
|
21
|
-
});
|
|
22
|
-
register_workflow(workflow: Workflow, options?: {
|
|
23
|
-
autoVersion?: boolean;
|
|
24
|
-
}): Promise<void>;
|
|
25
|
-
handle_start_step_run(action: Action): void;
|
|
26
|
-
get_action_event(action: Action, eventType: ActionEventType, payload?: any): ActionEvent;
|
|
27
|
-
handle_cancel_step_run(action: Action): void;
|
|
28
|
-
stop(): Promise<void>;
|
|
29
|
-
exit_gracefully(): Promise<void>;
|
|
30
|
-
start(): Promise<void>;
|
|
31
|
-
}
|
|
@@ -1,228 +0,0 @@
|
|
|
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 __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
12
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
13
|
-
var m = o[Symbol.asyncIterator], i;
|
|
14
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
15
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
16
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
|
-
};
|
|
18
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
19
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
20
|
-
};
|
|
21
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.Worker = void 0;
|
|
23
|
-
const hatchet_error_1 = __importDefault(require("../../util/errors/hatchet-error"));
|
|
24
|
-
const dispatcher_1 = require("@hatchet/protoc/dispatcher");
|
|
25
|
-
const hatchet_promise_1 = __importDefault(require("../../util/hatchet-promise/hatchet-promise"));
|
|
26
|
-
const logger_1 = require("@hatchet/util/logger");
|
|
27
|
-
const sleep_1 = __importDefault(require("@hatchet/util/sleep"));
|
|
28
|
-
const step_1 = require("../../step");
|
|
29
|
-
class Worker {
|
|
30
|
-
constructor(client, options) {
|
|
31
|
-
this.serviceName = 'default';
|
|
32
|
-
this.futures = {};
|
|
33
|
-
this.client = client;
|
|
34
|
-
this.name = options.name;
|
|
35
|
-
this.action_registry = {};
|
|
36
|
-
process.on('SIGTERM', () => this.exit_gracefully());
|
|
37
|
-
process.on('SIGINT', () => this.exit_gracefully());
|
|
38
|
-
this.killing = false;
|
|
39
|
-
this.handle_kill = options.handleKill === undefined ? true : options.handleKill;
|
|
40
|
-
this.logger = new logger_1.Logger(`Worker/${this.name}`, this.client.config.log_level);
|
|
41
|
-
}
|
|
42
|
-
register_workflow(workflow, options) {
|
|
43
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
try {
|
|
45
|
-
yield this.client.admin.put_workflow({
|
|
46
|
-
name: workflow.id,
|
|
47
|
-
description: workflow.description,
|
|
48
|
-
version: 'v0.55.0', // FIXME workflow.version,
|
|
49
|
-
eventTriggers: workflow.on.event ? [workflow.on.event] : [],
|
|
50
|
-
cronTriggers: workflow.on.cron ? [workflow.on.cron] : [],
|
|
51
|
-
scheduledTriggers: [],
|
|
52
|
-
jobs: [
|
|
53
|
-
{
|
|
54
|
-
name: 'my-job', // FIXME variable names
|
|
55
|
-
timeout: '60s',
|
|
56
|
-
description: 'my-job',
|
|
57
|
-
steps: workflow.steps.map((step) => {
|
|
58
|
-
var _a;
|
|
59
|
-
return ({
|
|
60
|
-
readableId: step.name,
|
|
61
|
-
action: `${this.serviceName}:${step.name}`,
|
|
62
|
-
timeout: '60s',
|
|
63
|
-
inputs: '{}',
|
|
64
|
-
parents: (_a = step.parents) !== null && _a !== void 0 ? _a : [],
|
|
65
|
-
});
|
|
66
|
-
}),
|
|
67
|
-
},
|
|
68
|
-
],
|
|
69
|
-
}, {
|
|
70
|
-
autoVersion: !(options === null || options === void 0 ? void 0 : options.autoVersion),
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
catch (e) {
|
|
74
|
-
throw new hatchet_error_1.default(`Could not register workflow: ${e.message}`);
|
|
75
|
-
}
|
|
76
|
-
this.action_registry = workflow.steps.reduce((acc, step) => {
|
|
77
|
-
acc[`${this.serviceName}:${step.name}`] = step.run;
|
|
78
|
-
return acc;
|
|
79
|
-
}, {});
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
handle_start_step_run(action) {
|
|
83
|
-
const { actionId } = action;
|
|
84
|
-
const context = new step_1.Context(action.actionPayload);
|
|
85
|
-
const step = this.action_registry[actionId];
|
|
86
|
-
if (!step) {
|
|
87
|
-
this.logger.error(`Could not find step '${actionId}'`);
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const run = () => __awaiter(this, void 0, void 0, function* () {
|
|
91
|
-
return step(context);
|
|
92
|
-
});
|
|
93
|
-
const success = (result) => {
|
|
94
|
-
this.logger.info(`Step run ${action.stepRunId} succeeded`);
|
|
95
|
-
try {
|
|
96
|
-
// Send the action event to the dispatcher
|
|
97
|
-
const event = this.get_action_event(action, dispatcher_1.ActionEventType.STEP_EVENT_TYPE_COMPLETED, result);
|
|
98
|
-
this.client.dispatcher.send_action_event(event);
|
|
99
|
-
// delete the run from the futures
|
|
100
|
-
delete this.futures[action.stepRunId];
|
|
101
|
-
}
|
|
102
|
-
catch (e) {
|
|
103
|
-
this.logger.error(`Could not send action event: ${e.message}`);
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
const failure = (error) => {
|
|
107
|
-
this.logger.error(`Step run ${action.stepRunId} failed: ${error.message}`);
|
|
108
|
-
try {
|
|
109
|
-
// Send the action event to the dispatcher
|
|
110
|
-
const event = this.get_action_event(action, dispatcher_1.ActionEventType.STEP_EVENT_TYPE_FAILED, error);
|
|
111
|
-
this.client.dispatcher.send_action_event(event);
|
|
112
|
-
// delete the run from the futures
|
|
113
|
-
delete this.futures[action.stepRunId];
|
|
114
|
-
}
|
|
115
|
-
catch (e) {
|
|
116
|
-
this.logger.error(`Could not send action event: ${e.message}`);
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
const future = new hatchet_promise_1.default(run().then(success).catch(failure));
|
|
120
|
-
this.futures[action.stepRunId] = future;
|
|
121
|
-
try {
|
|
122
|
-
// Send the action event to the dispatcher
|
|
123
|
-
const event = this.get_action_event(action, dispatcher_1.ActionEventType.STEP_EVENT_TYPE_STARTED);
|
|
124
|
-
this.client.dispatcher.send_action_event(event);
|
|
125
|
-
}
|
|
126
|
-
catch (e) {
|
|
127
|
-
this.logger.error(`Could not send action event: ${e.message}`);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
get_action_event(action, eventType, payload = '') {
|
|
131
|
-
return {
|
|
132
|
-
workerId: this.name,
|
|
133
|
-
jobId: action.jobId,
|
|
134
|
-
jobRunId: action.jobRunId,
|
|
135
|
-
stepId: action.stepId,
|
|
136
|
-
stepRunId: action.stepRunId,
|
|
137
|
-
actionId: action.actionId,
|
|
138
|
-
eventTimestamp: new Date(),
|
|
139
|
-
eventType,
|
|
140
|
-
eventPayload: JSON.stringify(payload),
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
handle_cancel_step_run(action) {
|
|
144
|
-
const { stepRunId } = action;
|
|
145
|
-
const future = this.futures[stepRunId];
|
|
146
|
-
if (future) {
|
|
147
|
-
future.cancel();
|
|
148
|
-
delete this.futures[stepRunId];
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
stop() {
|
|
152
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
-
yield this.exit_gracefully();
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
exit_gracefully() {
|
|
157
|
-
var _a;
|
|
158
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
-
this.killing = true;
|
|
160
|
-
this.logger.info('Starting to exit...');
|
|
161
|
-
try {
|
|
162
|
-
(_a = this.listener) === null || _a === void 0 ? void 0 : _a.unregister();
|
|
163
|
-
}
|
|
164
|
-
catch (e) {
|
|
165
|
-
this.logger.error(`Could not unregister listener: ${e.message}`);
|
|
166
|
-
}
|
|
167
|
-
this.logger.info('Gracefully exiting hatchet worker, running tasks will attempt to finish...');
|
|
168
|
-
// attempt to wait for futures to finish
|
|
169
|
-
yield Promise.all(Object.values(this.futures).map(({ promise }) => promise));
|
|
170
|
-
if (this.handle_kill) {
|
|
171
|
-
this.logger.info('Exiting hatchet worker...');
|
|
172
|
-
process.exit(0);
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
start() {
|
|
177
|
-
var _a, e_1, _b, _c;
|
|
178
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
-
let retries = 0;
|
|
180
|
-
while (retries < 5) {
|
|
181
|
-
try {
|
|
182
|
-
this.listener = yield this.client.dispatcher.get_action_listener({
|
|
183
|
-
workerName: this.name,
|
|
184
|
-
services: ['default'],
|
|
185
|
-
actions: Object.keys(this.action_registry),
|
|
186
|
-
});
|
|
187
|
-
const generator = this.listener.actions();
|
|
188
|
-
this.logger.info(`Worker ${this.name} listening for actions`);
|
|
189
|
-
try {
|
|
190
|
-
for (var _d = true, generator_1 = (e_1 = void 0, __asyncValues(generator)), generator_1_1; generator_1_1 = yield generator_1.next(), _a = generator_1_1.done, !_a; _d = true) {
|
|
191
|
-
_c = generator_1_1.value;
|
|
192
|
-
_d = false;
|
|
193
|
-
const action = _c;
|
|
194
|
-
this.logger.info(`Worker ${this.name} received action ${action.actionId}`);
|
|
195
|
-
if (action.actionType === dispatcher_1.ActionType.START_STEP_RUN) {
|
|
196
|
-
this.handle_start_step_run(action);
|
|
197
|
-
}
|
|
198
|
-
else if (action.actionType === dispatcher_1.ActionType.CANCEL_STEP_RUN) {
|
|
199
|
-
this.handle_cancel_step_run(action);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
204
|
-
finally {
|
|
205
|
-
try {
|
|
206
|
-
if (!_d && !_a && (_b = generator_1.return)) yield _b.call(generator_1);
|
|
207
|
-
}
|
|
208
|
-
finally { if (e_1) throw e_1.error; }
|
|
209
|
-
}
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
catch (e) {
|
|
213
|
-
this.logger.error(`Could not start worker: ${e.message}`);
|
|
214
|
-
retries += 1;
|
|
215
|
-
const wait = 500;
|
|
216
|
-
this.logger.error(`Could not start worker, retrying in ${500} seconds`);
|
|
217
|
-
yield (0, sleep_1.default)(wait);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
if (this.killing)
|
|
221
|
-
return;
|
|
222
|
-
if (retries > 5) {
|
|
223
|
-
throw new hatchet_error_1.default('Could not start worker after 5 retries');
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
exports.Worker = Worker;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,256 +0,0 @@
|
|
|
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 hatchet_client_1 = require("../hatchet-client");
|
|
16
|
-
const dispatcher_1 = require("@hatchet/protoc/dispatcher");
|
|
17
|
-
const action_listener_1 = require("../dispatcher/action-listener");
|
|
18
|
-
const nice_grpc_common_1 = require("nice-grpc-common");
|
|
19
|
-
const action_listener_test_1 = require("../dispatcher/action-listener.test");
|
|
20
|
-
const zod_1 = require("zod");
|
|
21
|
-
const sleep_1 = __importDefault(require("../../util/sleep"));
|
|
22
|
-
const nice_grpc_1 = require("nice-grpc");
|
|
23
|
-
const worker_1 = require("./worker");
|
|
24
|
-
const mockStart = {
|
|
25
|
-
tenantId: 'TENANT_ID',
|
|
26
|
-
jobId: 'job1',
|
|
27
|
-
jobName: 'Job One',
|
|
28
|
-
jobRunId: 'run1',
|
|
29
|
-
stepId: 'step1',
|
|
30
|
-
stepRunId: 'runStep1',
|
|
31
|
-
actionId: 'action1',
|
|
32
|
-
actionType: dispatcher_1.ActionType.START_STEP_RUN,
|
|
33
|
-
actionPayload: JSON.stringify('{"input": {"data": 1}}'),
|
|
34
|
-
};
|
|
35
|
-
const mockCancel = Object.assign(Object.assign({}, mockStart), { actionType: dispatcher_1.ActionType.CANCEL_STEP_RUN });
|
|
36
|
-
describe('Worker', () => {
|
|
37
|
-
let hatchet;
|
|
38
|
-
beforeEach(() => {
|
|
39
|
-
hatchet = new hatchet_client_1.HatchetClient({
|
|
40
|
-
token: 'TOKEN',
|
|
41
|
-
log_level: 'OFF',
|
|
42
|
-
host_port: 'HOST_PORT',
|
|
43
|
-
tls_config: {
|
|
44
|
-
cert_file: 'TLS_CERT_FILE',
|
|
45
|
-
key_file: 'TLS_KEY_FILE',
|
|
46
|
-
ca_file: 'TLS_ROOT_CA_FILE',
|
|
47
|
-
server_name: 'TLS_SERVER_NAME',
|
|
48
|
-
},
|
|
49
|
-
}, {
|
|
50
|
-
credentials: nice_grpc_1.ChannelCredentials.createInsecure(),
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
describe('register_workflow', () => {
|
|
54
|
-
it('should update the registry', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
55
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
56
|
-
const putWorkflowSpy = jest.spyOn(worker.client.admin, 'put_workflow').mockResolvedValue();
|
|
57
|
-
const workflow = {
|
|
58
|
-
id: 'workflow1',
|
|
59
|
-
description: 'test',
|
|
60
|
-
on: {
|
|
61
|
-
event: 'user:create',
|
|
62
|
-
},
|
|
63
|
-
steps: [
|
|
64
|
-
{
|
|
65
|
-
name: 'step1',
|
|
66
|
-
run: (ctx) => {
|
|
67
|
-
return { test: 'test' };
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
],
|
|
71
|
-
};
|
|
72
|
-
yield worker.register_workflow(workflow);
|
|
73
|
-
expect(putWorkflowSpy).toHaveBeenCalledTimes(1);
|
|
74
|
-
expect(worker.action_registry).toEqual({
|
|
75
|
-
[`default:step1`]: workflow.steps[0].run,
|
|
76
|
-
});
|
|
77
|
-
}));
|
|
78
|
-
});
|
|
79
|
-
describe('handle_start_step_run', () => {
|
|
80
|
-
it('should start a step run', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
82
|
-
const putWorkflowSpy = jest.spyOn(worker.client.admin, 'put_workflow').mockResolvedValue();
|
|
83
|
-
const getActionEventSpy = jest.spyOn(worker, 'get_action_event');
|
|
84
|
-
const sendActionEventSpy = jest
|
|
85
|
-
.spyOn(worker.client.dispatcher, 'send_action_event')
|
|
86
|
-
.mockResolvedValue({
|
|
87
|
-
tenantId: 'TENANT_ID',
|
|
88
|
-
workerId: 'WORKER_ID',
|
|
89
|
-
});
|
|
90
|
-
const startSpy = jest.fn().mockReturnValue({ data: 4 });
|
|
91
|
-
worker.action_registry = {
|
|
92
|
-
[mockStart.actionId]: startSpy,
|
|
93
|
-
};
|
|
94
|
-
worker.handle_start_step_run(mockStart);
|
|
95
|
-
yield (0, sleep_1.default)(100);
|
|
96
|
-
expect(startSpy).toHaveBeenCalledTimes(1);
|
|
97
|
-
expect(getActionEventSpy).toHaveBeenNthCalledWith(2, expect.anything(), dispatcher_1.ActionEventType.STEP_EVENT_TYPE_COMPLETED, { data: 4 });
|
|
98
|
-
expect(worker.futures[mockStart.stepRunId]).toBeUndefined();
|
|
99
|
-
expect(sendActionEventSpy).toHaveBeenCalledTimes(2);
|
|
100
|
-
}));
|
|
101
|
-
it('should fail gracefully', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
102
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
103
|
-
const getActionEventSpy = jest.spyOn(worker, 'get_action_event');
|
|
104
|
-
const sendActionEventSpy = jest
|
|
105
|
-
.spyOn(worker.client.dispatcher, 'send_action_event')
|
|
106
|
-
.mockResolvedValue({
|
|
107
|
-
tenantId: 'TENANT_ID',
|
|
108
|
-
workerId: 'WORKER_ID',
|
|
109
|
-
});
|
|
110
|
-
const startSpy = jest.fn().mockRejectedValue(new Error('ERROR'));
|
|
111
|
-
worker.action_registry = {
|
|
112
|
-
[mockStart.actionId]: startSpy,
|
|
113
|
-
};
|
|
114
|
-
worker.handle_start_step_run(mockStart);
|
|
115
|
-
yield (0, sleep_1.default)(100);
|
|
116
|
-
expect(startSpy).toHaveBeenCalledTimes(1);
|
|
117
|
-
expect(getActionEventSpy).toHaveBeenNthCalledWith(2, expect.anything(), dispatcher_1.ActionEventType.STEP_EVENT_TYPE_FAILED, expect.anything());
|
|
118
|
-
expect(worker.futures[mockStart.stepRunId]).toBeUndefined();
|
|
119
|
-
expect(sendActionEventSpy).toHaveBeenCalledTimes(2);
|
|
120
|
-
}));
|
|
121
|
-
});
|
|
122
|
-
describe('handle_cancel_step_run', () => {
|
|
123
|
-
it('should cancel a step run', () => {
|
|
124
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
125
|
-
const cancelSpy = jest.fn().mockReturnValue(undefined);
|
|
126
|
-
worker.futures = {
|
|
127
|
-
[mockCancel.stepRunId]: {
|
|
128
|
-
cancel: cancelSpy,
|
|
129
|
-
},
|
|
130
|
-
};
|
|
131
|
-
worker.handle_cancel_step_run(mockCancel);
|
|
132
|
-
expect(cancelSpy).toHaveBeenCalledTimes(1);
|
|
133
|
-
expect(worker.futures[mockCancel.stepRunId]).toBeUndefined();
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
describe('exit_gracefully', () => {
|
|
137
|
-
xit('should call exit_gracefully on SIGTERM', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
138
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
139
|
-
// the spy is not working and the test is killing the test process
|
|
140
|
-
const exitSpy = jest.spyOn(worker, 'exit_gracefully').mockImplementationOnce(() => {
|
|
141
|
-
throw new Error('Simulated error');
|
|
142
|
-
});
|
|
143
|
-
process.emit('SIGTERM', 'SIGTERM');
|
|
144
|
-
expect(exitSpy).toHaveBeenCalledTimes(1);
|
|
145
|
-
}));
|
|
146
|
-
xit('should call exit_gracefully on SIGINT', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
147
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
148
|
-
// This is killing the process (as it should) fix the spy at some point
|
|
149
|
-
const exitSpy = jest.spyOn(worker, 'exit_gracefully').mockResolvedValue();
|
|
150
|
-
process.emit('SIGINT', 'SIGINT');
|
|
151
|
-
expect(exitSpy).toHaveBeenCalledTimes(1);
|
|
152
|
-
}));
|
|
153
|
-
xit('should unregister the listener and exit', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
154
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
155
|
-
jest.spyOn(process, 'exit').mockImplementation((number) => {
|
|
156
|
-
throw new Error(`EXIT ${number}`);
|
|
157
|
-
}); // This is killing the process (as it should) fix the spy at some point
|
|
158
|
-
const mockActionListener = new action_listener_1.ActionListener(hatchet.dispatcher, (0, action_listener_test_1.mockListener)([mockStart, mockStart, new nice_grpc_common_1.ServerError(nice_grpc_common_1.Status.CANCELLED, 'CANCELLED')]), 'WORKER_ID');
|
|
159
|
-
mockActionListener.unregister = jest.fn().mockResolvedValue((0, zod_1.never)());
|
|
160
|
-
worker.listener = mockActionListener;
|
|
161
|
-
expect(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
162
|
-
yield worker.exit_gracefully();
|
|
163
|
-
})).toThrow('EXIT 0');
|
|
164
|
-
expect(mockActionListener.unregister).toHaveBeenCalledTimes(1);
|
|
165
|
-
}));
|
|
166
|
-
it('should exit the process if handle_kill is true', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
167
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
168
|
-
const exitSpy = jest.spyOn(process, 'exit').mockReturnValue(undefined);
|
|
169
|
-
yield worker.exit_gracefully();
|
|
170
|
-
expect(exitSpy).toHaveBeenCalledTimes(1);
|
|
171
|
-
}));
|
|
172
|
-
});
|
|
173
|
-
describe('start', () => {
|
|
174
|
-
xit('should get actions and start runs', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
175
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
176
|
-
const startSpy = jest.spyOn(worker, 'handle_start_step_run').mockReturnValue();
|
|
177
|
-
const cancelSpy = jest.spyOn(worker, 'handle_cancel_step_run').mockReturnValue();
|
|
178
|
-
const mockActionListener = new action_listener_1.ActionListener(hatchet.dispatcher, (0, action_listener_test_1.mockListener)([mockStart, mockStart, new nice_grpc_common_1.ServerError(nice_grpc_common_1.Status.CANCELLED, 'CANCELLED')]), 'WORKER_ID');
|
|
179
|
-
const getActionListenerSpy = jest
|
|
180
|
-
.spyOn(worker.client.dispatcher, 'get_action_listener')
|
|
181
|
-
.mockResolvedValue(mockActionListener);
|
|
182
|
-
yield worker.start();
|
|
183
|
-
expect(getActionListenerSpy).toHaveBeenCalledTimes(1);
|
|
184
|
-
expect(startSpy).toHaveBeenCalledTimes(2);
|
|
185
|
-
expect(cancelSpy).toHaveBeenCalledTimes(0);
|
|
186
|
-
}));
|
|
187
|
-
it('should get actions and cancel runs', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
188
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
189
|
-
const startSpy = jest.spyOn(worker, 'handle_start_step_run').mockReturnValue();
|
|
190
|
-
const cancelSpy = jest.spyOn(worker, 'handle_cancel_step_run').mockReturnValue();
|
|
191
|
-
const mockActionListner = new action_listener_1.ActionListener(hatchet.dispatcher, (0, action_listener_test_1.mockListener)([mockStart, mockCancel, new nice_grpc_common_1.ServerError(nice_grpc_common_1.Status.CANCELLED, 'CANCELLED')]), 'WORKER_ID');
|
|
192
|
-
const getActionListenerSpy = jest
|
|
193
|
-
.spyOn(worker.client.dispatcher, 'get_action_listener')
|
|
194
|
-
.mockResolvedValue(mockActionListner);
|
|
195
|
-
yield worker.start();
|
|
196
|
-
expect(getActionListenerSpy).toHaveBeenCalledTimes(1);
|
|
197
|
-
expect(startSpy).toHaveBeenCalledTimes(1);
|
|
198
|
-
expect(cancelSpy).toHaveBeenCalledTimes(1);
|
|
199
|
-
}));
|
|
200
|
-
it('should retry 5 times to start a worker then throw an error', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
201
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
202
|
-
const startSpy = jest.spyOn(worker, 'handle_start_step_run').mockReturnValue();
|
|
203
|
-
const cancelSpy = jest.spyOn(worker, 'handle_cancel_step_run').mockReturnValue();
|
|
204
|
-
const mockActionListner = new action_listener_1.ActionListener(hatchet.dispatcher, (0, action_listener_test_1.mockListener)([mockStart, mockCancel, new nice_grpc_common_1.ServerError(nice_grpc_common_1.Status.CANCELLED, 'CANCELLED')]), 'WORKER_ID');
|
|
205
|
-
const getActionListenerSpy = jest
|
|
206
|
-
.spyOn(worker.client.dispatcher, 'get_action_listener')
|
|
207
|
-
.mockImplementationOnce(() => {
|
|
208
|
-
throw new Error('Simulated error');
|
|
209
|
-
})
|
|
210
|
-
.mockImplementationOnce(() => {
|
|
211
|
-
throw new Error('Simulated error');
|
|
212
|
-
})
|
|
213
|
-
.mockImplementationOnce(() => {
|
|
214
|
-
throw new Error('Simulated error');
|
|
215
|
-
})
|
|
216
|
-
.mockImplementationOnce(() => {
|
|
217
|
-
throw new Error('Simulated error');
|
|
218
|
-
})
|
|
219
|
-
.mockImplementationOnce(() => {
|
|
220
|
-
throw new Error('Simulated error');
|
|
221
|
-
})
|
|
222
|
-
.mockImplementationOnce(() => {
|
|
223
|
-
throw new Error('Simulated error');
|
|
224
|
-
});
|
|
225
|
-
yield worker.start();
|
|
226
|
-
expect(getActionListenerSpy).toHaveBeenCalledTimes(5);
|
|
227
|
-
expect(startSpy).toHaveBeenCalledTimes(0);
|
|
228
|
-
expect(cancelSpy).toHaveBeenCalledTimes(0);
|
|
229
|
-
}));
|
|
230
|
-
it('should successfully run after retrying < 5 times', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
231
|
-
const worker = new worker_1.Worker(hatchet, { name: 'WORKER_NAME' });
|
|
232
|
-
const startSpy = jest.spyOn(worker, 'handle_start_step_run').mockReturnValue();
|
|
233
|
-
const cancelSpy = jest.spyOn(worker, 'handle_cancel_step_run').mockReturnValue();
|
|
234
|
-
const mockActionLister = new action_listener_1.ActionListener(hatchet.dispatcher, (0, action_listener_test_1.mockListener)([mockStart, mockCancel, new nice_grpc_common_1.ServerError(nice_grpc_common_1.Status.CANCELLED, 'CANCELLED')]), 'WORKER_ID');
|
|
235
|
-
const getActionListenerSpy = jest
|
|
236
|
-
.spyOn(worker.client.dispatcher, 'get_action_listener')
|
|
237
|
-
.mockImplementationOnce(() => {
|
|
238
|
-
throw new Error('Simulated error');
|
|
239
|
-
})
|
|
240
|
-
.mockImplementationOnce(() => {
|
|
241
|
-
throw new Error('Simulated error');
|
|
242
|
-
})
|
|
243
|
-
.mockImplementationOnce(() => {
|
|
244
|
-
throw new Error('Simulated error');
|
|
245
|
-
})
|
|
246
|
-
.mockImplementationOnce(() => {
|
|
247
|
-
throw new Error('Simulated error');
|
|
248
|
-
})
|
|
249
|
-
.mockImplementationOnce(() => __awaiter(void 0, void 0, void 0, function* () { return mockActionLister; }));
|
|
250
|
-
yield worker.start();
|
|
251
|
-
expect(getActionListenerSpy).toHaveBeenCalledTimes(5);
|
|
252
|
-
expect(startSpy).toHaveBeenCalledTimes(1);
|
|
253
|
-
expect(cancelSpy).toHaveBeenCalledTimes(1);
|
|
254
|
-
}));
|
|
255
|
-
});
|
|
256
|
-
});
|
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED