@hatchet-dev/typescript-sdk 0.9.0 → 0.10.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.
@@ -25,7 +25,7 @@ const dispatcher_1 = require("../../protoc/dispatcher");
25
25
  const hatchet_promise_1 = __importDefault(require("../../util/hatchet-promise/hatchet-promise"));
26
26
  const workflows_1 = require("../../protoc/workflows");
27
27
  const logger_1 = require("../../util/logger");
28
- // import sleep from '../../util/sleep.js';
28
+ const handler_1 = require("./handler");
29
29
  const step_1 = require("../../step");
30
30
  class Worker {
31
31
  constructor(client, options) {
@@ -42,6 +42,33 @@ class Worker {
42
42
  this.handle_kill = options.handleKill === undefined ? true : options.handleKill;
43
43
  this.logger = new logger_1.Logger(`Worker/${this.name}`, this.client.config.log_level);
44
44
  }
45
+ registerActions(workflow) {
46
+ var _a;
47
+ const newActions = workflow.steps.reduce((acc, step) => {
48
+ acc[`${workflow.id}:${step.name}`] = step.run;
49
+ return acc;
50
+ }, {});
51
+ const onFailureAction = workflow.onFailure
52
+ ? {
53
+ [`${workflow.id}-on-failure:${workflow.onFailure.name}`]: workflow.onFailure.run,
54
+ }
55
+ : {};
56
+ this.action_registry = Object.assign(Object.assign(Object.assign({}, this.action_registry), newActions), onFailureAction);
57
+ this.action_registry = ((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.name)
58
+ ? Object.assign(Object.assign({}, this.action_registry), { [`${workflow.id}:${workflow.concurrency.name}`]: workflow.concurrency.key }) : Object.assign({}, this.action_registry);
59
+ }
60
+ getHandler(workflows) {
61
+ for (const workflow of workflows) {
62
+ const wf = Object.assign(Object.assign({}, workflow), { id: this.client.config.namespace + workflow.id });
63
+ this.registerActions(wf);
64
+ }
65
+ return new handler_1.WebhookHandler(this, workflows);
66
+ }
67
+ registerWebhook(webhook) {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ return this.client.admin.registerWebhook(Object.assign({}, webhook));
70
+ });
71
+ }
45
72
  /**
46
73
  * @deprecated use registerWorkflow instead
47
74
  */
@@ -52,7 +79,7 @@ class Worker {
52
79
  }
53
80
  registerWorkflow(initWorkflow) {
54
81
  return __awaiter(this, void 0, void 0, function* () {
55
- var _a, _b, _c;
82
+ var _a, _b;
56
83
  const workflow = Object.assign(Object.assign({}, initWorkflow), { id: this.client.config.namespace + initWorkflow.id });
57
84
  try {
58
85
  const concurrency = ((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.name)
@@ -118,145 +145,154 @@ class Worker {
118
145
  catch (e) {
119
146
  throw new hatchet_error_1.default(`Could not register workflow: ${e.message}`);
120
147
  }
121
- const newActions = workflow.steps.reduce((acc, step) => {
122
- acc[`${workflow.id}:${step.name}`] = step.run;
123
- return acc;
124
- }, {});
125
- const onFailureAction = workflow.onFailure
126
- ? {
127
- [`${workflow.id}-on-failure:${workflow.onFailure.name}`]: workflow.onFailure.run,
128
- }
129
- : {};
130
- this.action_registry = Object.assign(Object.assign(Object.assign({}, this.action_registry), newActions), onFailureAction);
131
- this.action_registry = ((_c = workflow.concurrency) === null || _c === void 0 ? void 0 : _c.name)
132
- ? Object.assign(Object.assign({}, this.action_registry), { [`${workflow.id}:${workflow.concurrency.name}`]: workflow.concurrency.key }) : Object.assign({}, this.action_registry);
148
+ this.registerActions(workflow);
133
149
  });
134
150
  }
135
151
  registerAction(actionId, action) {
136
152
  this.action_registry[actionId] = action;
137
153
  }
138
154
  handleStartStepRun(action) {
139
- const { actionId } = action;
140
- try {
141
- const context = new step_1.Context(action, this.client);
142
- this.contexts[action.stepRunId] = context;
143
- const step = this.action_registry[actionId];
144
- if (!step) {
145
- this.logger.error(`Could not find step '${actionId}'`);
146
- return;
147
- }
148
- const run = () => __awaiter(this, void 0, void 0, function* () {
149
- return step(context);
150
- });
151
- const success = (result) => {
152
- this.logger.info(`Step run ${action.stepRunId} succeeded`);
153
- try {
154
- // Send the action event to the dispatcher
155
- const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_COMPLETED, result || null);
156
- this.client.dispatcher.sendStepActionEvent(event).catch((e) => {
157
- this.logger.error(`Could not send completed action event: ${e.message}`);
155
+ return __awaiter(this, void 0, void 0, function* () {
156
+ const { actionId } = action;
157
+ try {
158
+ const context = new step_1.Context(action, this.client);
159
+ this.contexts[action.stepRunId] = context;
160
+ const step = this.action_registry[actionId];
161
+ if (!step) {
162
+ this.logger.error(`Could not find step '${actionId}'`);
163
+ return;
164
+ }
165
+ const run = () => __awaiter(this, void 0, void 0, function* () {
166
+ return step(context);
167
+ });
168
+ const success = (result) => __awaiter(this, void 0, void 0, function* () {
169
+ this.logger.info(`Step run ${action.stepRunId} succeeded`);
170
+ try {
171
+ // Send the action event to the dispatcher
172
+ const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_COMPLETED, result || null);
173
+ yield this.client.dispatcher.sendStepActionEvent(event);
174
+ // delete the run from the futures
175
+ delete this.futures[action.stepRunId];
176
+ }
177
+ catch (actionEventError) {
178
+ this.logger.error(`Could not send completed action event: ${actionEventError.message}`);
158
179
  // send a failure event
159
- const failureEvent = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, e.message);
160
- this.client.dispatcher.sendStepActionEvent(failureEvent).catch((err2) => {
161
- this.logger.error(`Could not send failed action event: ${err2.message}`);
180
+ const failureEvent = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, actionEventError.message);
181
+ try {
182
+ yield this.client.dispatcher.sendStepActionEvent(failureEvent);
183
+ }
184
+ catch (failureEventError) {
185
+ this.logger.error(`Could not send failed action event: ${failureEventError.message}`);
186
+ }
187
+ this.logger.error(`Could not send action event: ${actionEventError.message}`);
188
+ }
189
+ });
190
+ const failure = (error) => __awaiter(this, void 0, void 0, function* () {
191
+ this.logger.error(`Step run ${action.stepRunId} failed: ${error.message}`);
192
+ if (error.stack) {
193
+ this.logger.error(error.stack);
194
+ }
195
+ try {
196
+ // Send the action event to the dispatcher
197
+ const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, {
198
+ message: error === null || error === void 0 ? void 0 : error.message,
199
+ stack: error === null || error === void 0 ? void 0 : error.stack,
162
200
  });
163
- });
164
- // delete the run from the futures
165
- delete this.futures[action.stepRunId];
166
- }
167
- catch (e) {
168
- this.logger.error(`Could not send action event: ${e.message}`);
169
- }
170
- };
171
- const failure = (error) => {
172
- this.logger.error(`Step run ${action.stepRunId} failed: ${error.message}`);
173
- if (error.stack) {
174
- this.logger.error(error.stack);
175
- }
176
- try {
177
- // Send the action event to the dispatcher
178
- const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, {
179
- message: error === null || error === void 0 ? void 0 : error.message,
180
- stack: error === null || error === void 0 ? void 0 : error.stack,
181
- });
182
- this.client.dispatcher.sendStepActionEvent(event).catch((e) => {
201
+ yield this.client.dispatcher.sendStepActionEvent(event);
202
+ // delete the run from the futures
203
+ delete this.futures[action.stepRunId];
204
+ }
205
+ catch (e) {
183
206
  this.logger.error(`Could not send action event: ${e.message}`);
184
- });
185
- // delete the run from the futures
186
- delete this.futures[action.stepRunId];
187
- }
188
- catch (e) {
207
+ }
208
+ });
209
+ const future = new hatchet_promise_1.default((() => __awaiter(this, void 0, void 0, function* () {
210
+ let result;
211
+ try {
212
+ result = yield run();
213
+ }
214
+ catch (e) {
215
+ yield failure(e);
216
+ return;
217
+ }
218
+ yield success(result);
219
+ }))());
220
+ this.futures[action.stepRunId] = future;
221
+ // Send the action event to the dispatcher
222
+ const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_STARTED);
223
+ this.client.dispatcher.sendStepActionEvent(event).catch((e) => {
189
224
  this.logger.error(`Could not send action event: ${e.message}`);
190
- }
191
- };
192
- const future = new hatchet_promise_1.default(run().then(success).catch(failure));
193
- this.futures[action.stepRunId] = future;
194
- // Send the action event to the dispatcher
195
- const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_STARTED);
196
- this.client.dispatcher.sendStepActionEvent(event).catch((e) => {
225
+ });
226
+ yield future.promise;
227
+ }
228
+ catch (e) {
197
229
  this.logger.error(`Could not send action event: ${e.message}`);
198
- });
199
- }
200
- catch (e) {
201
- this.logger.error(`Could not send action event: ${e.message}`);
202
- }
230
+ }
231
+ });
203
232
  }
204
233
  handleStartGroupKeyRun(action) {
205
- const { actionId } = action;
206
- try {
207
- const context = new step_1.Context(action, this.client);
208
- const key = action.getGroupKeyRunId;
209
- this.contexts[key] = context;
210
- this.logger.debug(`Starting group key run ${key}`);
211
- const step = this.action_registry[actionId];
212
- if (!step) {
213
- this.logger.error(`Could not find step '${actionId}'`);
214
- return;
215
- }
216
- const run = () => __awaiter(this, void 0, void 0, function* () {
217
- return step(context);
218
- });
219
- const success = (result) => {
220
- this.logger.info(`Step run ${action.stepRunId} succeeded`);
221
- try {
222
- // Send the action event to the dispatcher
223
- const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_COMPLETED, result);
224
- this.client.dispatcher.sendGroupKeyActionEvent(event).catch((e) => {
225
- this.logger.error(`Could not send action event: ${e.message}`);
226
- });
227
- // delete the run from the futures
228
- delete this.futures[key];
234
+ return __awaiter(this, void 0, void 0, function* () {
235
+ const { actionId } = action;
236
+ try {
237
+ const context = new step_1.Context(action, this.client);
238
+ const key = action.getGroupKeyRunId;
239
+ if (!key) {
240
+ this.logger.error(`No group key run id provided for action ${actionId}`);
241
+ return;
229
242
  }
230
- catch (e) {
231
- this.logger.error(`Could not send action event: ${e.message}`);
243
+ this.contexts[key] = context;
244
+ this.logger.debug(`Starting group key run ${key}`);
245
+ const step = this.action_registry[actionId];
246
+ if (!step) {
247
+ this.logger.error(`Could not find step '${actionId}'`);
248
+ return;
232
249
  }
233
- };
234
- const failure = (error) => {
235
- this.logger.error(`Step run ${key} failed: ${error.message}`);
236
- try {
237
- // Send the action event to the dispatcher
238
- const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_FAILED, error);
239
- this.client.dispatcher.sendGroupKeyActionEvent(event).catch((e) => {
250
+ const run = () => __awaiter(this, void 0, void 0, function* () {
251
+ return step(context);
252
+ });
253
+ const success = (result) => {
254
+ this.logger.info(`Step run ${action.stepRunId} succeeded`);
255
+ try {
256
+ // Send the action event to the dispatcher
257
+ const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_COMPLETED, result);
258
+ this.client.dispatcher.sendGroupKeyActionEvent(event).catch((e) => {
259
+ this.logger.error(`Could not send action event: ${e.message}`);
260
+ });
261
+ // delete the run from the futures
262
+ delete this.futures[key];
263
+ }
264
+ catch (e) {
240
265
  this.logger.error(`Could not send action event: ${e.message}`);
241
- });
242
- // delete the run from the futures
243
- delete this.futures[key];
244
- }
245
- catch (e) {
266
+ }
267
+ };
268
+ const failure = (error) => {
269
+ this.logger.error(`Step run ${key} failed: ${error.message}`);
270
+ try {
271
+ // Send the action event to the dispatcher
272
+ const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_FAILED, error);
273
+ this.client.dispatcher.sendGroupKeyActionEvent(event).catch((e) => {
274
+ this.logger.error(`Could not send action event: ${e.message}`);
275
+ });
276
+ // delete the run from the futures
277
+ delete this.futures[key];
278
+ }
279
+ catch (e) {
280
+ this.logger.error(`Could not send action event: ${e.message}`);
281
+ }
282
+ };
283
+ const future = new hatchet_promise_1.default(run().then(success).catch(failure));
284
+ this.futures[key] = future;
285
+ // Send the action event to the dispatcher
286
+ const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_STARTED);
287
+ this.client.dispatcher.sendGroupKeyActionEvent(event).catch((e) => {
246
288
  this.logger.error(`Could not send action event: ${e.message}`);
247
- }
248
- };
249
- const future = new hatchet_promise_1.default(run().then(success).catch(failure));
250
- this.futures[action.getGroupKeyRunId] = future;
251
- // Send the action event to the dispatcher
252
- const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_STARTED);
253
- this.client.dispatcher.sendGroupKeyActionEvent(event).catch((e) => {
289
+ });
290
+ yield future.promise;
291
+ }
292
+ catch (e) {
254
293
  this.logger.error(`Could not send action event: ${e.message}`);
255
- });
256
- }
257
- catch (e) {
258
- this.logger.error(`Could not send action event: ${e.message}`);
259
- }
294
+ }
295
+ });
260
296
  }
261
297
  getStepActionEvent(action, eventType, payload = '') {
262
298
  return {
@@ -272,6 +308,9 @@ class Worker {
272
308
  };
273
309
  }
274
310
  getGroupKeyActionEvent(action, eventType, payload = '') {
311
+ if (!action.getGroupKeyRunId) {
312
+ throw new hatchet_error_1.default('No group key run id provided');
313
+ }
275
314
  return {
276
315
  workerId: this.name,
277
316
  workflowRunId: action.workflowRunId,
@@ -283,26 +322,29 @@ class Worker {
283
322
  };
284
323
  }
285
324
  handleCancelStepRun(action) {
286
- try {
287
- this.logger.info(`Cancelling step run ${action.stepRunId}`);
288
- const { stepRunId } = action;
289
- const future = this.futures[stepRunId];
290
- const context = this.contexts[stepRunId];
291
- if (context && context.controller) {
292
- context.controller.abort('Cancelled by worker');
293
- delete this.contexts[stepRunId];
325
+ return __awaiter(this, void 0, void 0, function* () {
326
+ try {
327
+ this.logger.info(`Cancelling step run ${action.stepRunId}`);
328
+ const { stepRunId } = action;
329
+ const future = this.futures[stepRunId];
330
+ const context = this.contexts[stepRunId];
331
+ if (context && context.controller) {
332
+ context.controller.abort('Cancelled by worker');
333
+ delete this.contexts[stepRunId];
334
+ }
335
+ if (future) {
336
+ future.promise.catch(() => {
337
+ this.logger.info(`Cancelled step run ${action.stepRunId}`);
338
+ });
339
+ future.cancel('Cancelled by worker');
340
+ yield future.promise;
341
+ delete this.futures[stepRunId];
342
+ }
294
343
  }
295
- if (future) {
296
- future.promise.catch(() => {
297
- this.logger.info(`Cancelled step run ${action.stepRunId}`);
298
- });
299
- future.cancel('Cancelled by worker');
300
- delete this.futures[stepRunId];
344
+ catch (e) {
345
+ this.logger.error(`Could not cancel step run: ${e.message}`);
301
346
  }
302
- }
303
- catch (e) {
304
- this.logger.error(`Could not cancel step run: ${e.message}`);
305
- }
347
+ });
306
348
  }
307
349
  stop() {
308
350
  return __awaiter(this, void 0, void 0, function* () {
@@ -350,18 +392,7 @@ class Worker {
350
392
  _d = false;
351
393
  const action = _c;
352
394
  this.logger.info(`Worker ${this.name} received action ${action.actionId}:${action.actionType}`);
353
- if (action.actionType === dispatcher_1.ActionType.START_STEP_RUN) {
354
- this.handleStartStepRun(action);
355
- }
356
- else if (action.actionType === dispatcher_1.ActionType.CANCEL_STEP_RUN) {
357
- this.handleCancelStepRun(action);
358
- }
359
- else if (action.actionType === dispatcher_1.ActionType.START_GET_GROUP_KEY) {
360
- this.handleStartGroupKeyRun(action);
361
- }
362
- else {
363
- this.logger.error(`Worker ${this.name} received unknown action type ${action.actionType}`);
364
- }
395
+ void this.handleAction(action);
365
396
  }
366
397
  }
367
398
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -383,5 +414,24 @@ class Worker {
383
414
  }
384
415
  });
385
416
  }
417
+ handleAction(action) {
418
+ return __awaiter(this, void 0, void 0, function* () {
419
+ const type = action.actionType
420
+ ? (0, dispatcher_1.actionTypeFromJSON)(action.actionType)
421
+ : dispatcher_1.ActionType.START_STEP_RUN;
422
+ if (type === dispatcher_1.ActionType.START_STEP_RUN) {
423
+ yield this.handleStartStepRun(action);
424
+ }
425
+ else if (type === dispatcher_1.ActionType.CANCEL_STEP_RUN) {
426
+ yield this.handleCancelStepRun(action);
427
+ }
428
+ else if (type === dispatcher_1.ActionType.START_GET_GROUP_KEY) {
429
+ yield this.handleStartGroupKeyRun(action);
430
+ }
431
+ else {
432
+ this.logger.error(`Worker ${this.name} received unknown action type ${type}`);
433
+ }
434
+ });
435
+ }
386
436
  }
387
437
  exports.Worker = Worker;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [