@hatchet-dev/typescript-sdk 0.16.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ import { Channel, ClientFactory } from 'nice-grpc';
2
2
  import { EventsServiceClient } from '../../protoc/events/events';
3
3
  import { ClientConfig } from '../hatchet-client/client-config';
4
4
  import { Logger } from '../../util/logger';
5
+ import { retrier } from '../../util/retrier';
5
6
  export declare enum LogLevel {
6
7
  INFO = "INFO",
7
8
  WARN = "WARN",
@@ -18,6 +19,7 @@ export interface EventWithMetadata<T> {
18
19
  export declare class EventClient {
19
20
  config: ClientConfig;
20
21
  client: EventsServiceClient;
22
+ retrier: typeof retrier;
21
23
  logger: Logger;
22
24
  constructor(config: ClientConfig, channel: Channel, factory: ClientFactory);
23
25
  push<T>(type: string, input: T, options?: PushEventOptions): Promise<import("../../protoc/events/events").Event>;
@@ -30,6 +30,7 @@ class EventClient {
30
30
  this.config = config;
31
31
  this.client = factory.create(events_1.EventsServiceDefinition, channel);
32
32
  this.logger = new logger_1.Logger(`Dispatcher`, config.log_level);
33
+ this.retrier = retrier_1.retrier;
33
34
  }
34
35
  push(type, input, options = {}) {
35
36
  var _a;
@@ -43,7 +44,7 @@ class EventClient {
43
44
  : undefined,
44
45
  };
45
46
  try {
46
- const e = this.client.push(req);
47
+ const e = this.retrier(() => __awaiter(this, void 0, void 0, function* () { return this.client.push(req); }), this.logger);
47
48
  this.logger.info(`Event pushed: ${namespacedType}`);
48
49
  return e;
49
50
  }
@@ -74,9 +75,9 @@ class EventClient {
74
75
  events,
75
76
  };
76
77
  try {
77
- const e = this.client.bulkPush(req);
78
+ const res = this.retrier(() => __awaiter(this, void 0, void 0, function* () { return this.client.bulkPush(req); }), this.logger);
78
79
  this.logger.info(`Bulk events pushed for type: ${namespacedType}`);
79
- return e;
80
+ return res;
80
81
  }
81
82
  catch (e) {
82
83
  throw new hatchet_error_1.default(e.message);
@@ -48,7 +48,7 @@ class Worker {
48
48
  registerActions(workflow) {
49
49
  var _a;
50
50
  const newActions = workflow.steps.reduce((acc, step) => {
51
- acc[`${workflow.id}:${step.name}`] = step.run;
51
+ acc[`${workflow.id}:${step.name.toLowerCase()}`] = step.run;
52
52
  return acc;
53
53
  }, {});
54
54
  const onFailureAction = workflow.onFailure
@@ -59,7 +59,7 @@ class Worker {
59
59
  this.action_registry = Object.assign(Object.assign(Object.assign({}, this.action_registry), newActions), onFailureAction);
60
60
  this.action_registry =
61
61
  ((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.name) && workflow.concurrency.key
62
- ? Object.assign(Object.assign({}, this.action_registry), { [`${workflow.id}:${workflow.concurrency.name}`]: workflow.concurrency.key }) : Object.assign({}, this.action_registry);
62
+ ? Object.assign(Object.assign({}, this.action_registry), { [`${workflow.id}:${workflow.concurrency.name.toLowerCase()}`]: workflow.concurrency.key }) : Object.assign({}, this.action_registry);
63
63
  }
64
64
  getHandler(workflows) {
65
65
  for (const workflow of workflows) {
@@ -136,7 +136,7 @@ class Worker {
136
136
  name: workflow.id,
137
137
  description: workflow.description,
138
138
  steps: workflow.steps.map((step) => {
139
- var _a;
139
+ var _a, _b, _c;
140
140
  return ({
141
141
  readableId: step.name,
142
142
  action: `${workflow.id}:${step.name}`,
@@ -147,6 +147,8 @@ class Worker {
147
147
  retries: step.retries || 0,
148
148
  rateLimits: (0, step_1.mapRateLimit)(step.rate_limits),
149
149
  workerLabels: toPbWorkerLabel(step.worker_labels),
150
+ backoffFactor: (_b = step.backoff) === null || _b === void 0 ? void 0 : _b.factor,
151
+ backoffMaxSeconds: (_c = step.backoff) === null || _c === void 0 ? void 0 : _c.maxSeconds,
150
152
  });
151
153
  }),
152
154
  },
@@ -163,7 +165,7 @@ class Worker {
163
165
  });
164
166
  }
165
167
  registerAction(actionId, action) {
166
- this.action_registry[actionId] = action;
168
+ this.action_registry[actionId.toLowerCase()] = action;
167
169
  }
168
170
  handleStartStepRun(action) {
169
171
  return __awaiter(this, void 0, void 0, function* () {
@@ -173,6 +175,7 @@ class Worker {
173
175
  this.contexts[action.stepRunId] = context;
174
176
  const step = this.action_registry[actionId];
175
177
  if (!step) {
178
+ this.logger.error(`Registered actions: '${Object.keys(this.action_registry).join(', ')}'`);
176
179
  this.logger.error(`Could not find step '${actionId}'`);
177
180
  return;
178
181
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,56 @@
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 sdk_1 = __importDefault(require("../sdk"));
16
+ const hatchet = sdk_1.default.init();
17
+ let numRetries = 0;
18
+ // ❓ Backoff
19
+ const workflow = {
20
+ // ... normal workflow definition
21
+ id: 'retries-with-backoff',
22
+ description: 'Backoff',
23
+ // ,
24
+ steps: [
25
+ {
26
+ name: 'backoff-step',
27
+ // ... step definition
28
+ run: (ctx) => __awaiter(void 0, void 0, void 0, function* () {
29
+ if (numRetries < 5) {
30
+ numRetries += 1;
31
+ throw new Error('failed');
32
+ }
33
+ return { backoff: 'completed' };
34
+ }),
35
+ // ,
36
+ retries: 10,
37
+ // 👀 Backoff configuration
38
+ backoff: {
39
+ // 👀 Maximum number of seconds to wait between retries
40
+ maxSeconds: 60,
41
+ // 👀 Factor to increase the wait time between retries.
42
+ // This sequence will be 2s, 4s, 8s, 16s, 32s, 60s... due to the maxSeconds limit
43
+ factor: 2,
44
+ },
45
+ },
46
+ ],
47
+ };
48
+ // ‼️
49
+ function main() {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ const worker = yield hatchet.worker('backoff-worker');
52
+ yield worker.registerWorkflow(workflow);
53
+ worker.start();
54
+ });
55
+ }
56
+ main();
package/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export * from './clients/rest';
6
6
  export * from './clients/admin';
7
7
  export * from './util/workflow-run-ref';
8
8
  export default Hatchet;
9
+ export { Hatchet };
package/index.js CHANGED
@@ -14,7 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.Hatchet = void 0;
17
18
  const hatchet_client_1 = require("./clients/hatchet-client");
19
+ Object.defineProperty(exports, "Hatchet", { enumerable: true, get: function () { return hatchet_client_1.HatchetClient; } });
18
20
  __exportStar(require("./workflow"), exports);
19
21
  __exportStar(require("./step"), exports);
20
22
  __exportStar(require("./clients/worker"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "0.16.0",
3
+ "version": "0.17.1",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -54,6 +54,7 @@
54
54
  "event:concurrency:rr": "npm run exec -- ./src/examples/concurrency/group-round-robin/concurrency-event.ts",
55
55
  "worker:playground": "npm run exec -- ./src/examples/playground.ts",
56
56
  "worker:retries": "npm run exec -- ./src/examples/retries-worker.ts",
57
+ "worker:retries-with-backoff": "npm run exec -- ./src/examples/retries-with-backoff.ts",
57
58
  "worker:multi-workflow": "npm run exec -- ./src/examples/multi-workflow.ts",
58
59
  "worker:logger": "npm run exec -- ./src/examples/logger.ts",
59
60
  "api": "npm run exec -- ./src/examples/api.ts",
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // Code generated by protoc-gen-ts_proto. DO NOT EDIT.
3
3
  // versions:
4
- // protoc-gen-ts_proto v2.3.0
4
+ // protoc-gen-ts_proto v2.5.0
5
5
  // protoc v3.19.1
6
6
  // source: dispatcher/dispatcher.proto
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // Code generated by protoc-gen-ts_proto. DO NOT EDIT.
3
3
  // versions:
4
- // protoc-gen-ts_proto v2.3.0
4
+ // protoc-gen-ts_proto v2.5.0
5
5
  // protoc v3.19.1
6
6
  // source: events/events.proto
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // Code generated by protoc-gen-ts_proto. DO NOT EDIT.
3
3
  // versions:
4
- // protoc-gen-ts_proto v2.3.0
4
+ // protoc-gen-ts_proto v2.5.0
5
5
  // protoc v3.19.1
6
6
  // source: google/protobuf/timestamp.proto
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // Code generated by protoc-gen-ts_proto. DO NOT EDIT.
3
3
  // versions:
4
- // protoc-gen-ts_proto v2.3.0
4
+ // protoc-gen-ts_proto v2.5.0
5
5
  // protoc v3.19.1
6
6
  // source: workflows/workflows.proto
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
package/step.d.ts CHANGED
@@ -89,6 +89,16 @@ export declare const CreateStepSchema: z.ZodObject<{
89
89
  comparator?: WorkerLabelComparator | undefined;
90
90
  weight?: number | undefined;
91
91
  }>]>>>>>;
92
+ backoff: z.ZodOptional<z.ZodObject<{
93
+ factor: z.ZodOptional<z.ZodNumber>;
94
+ maxSeconds: z.ZodOptional<z.ZodNumber>;
95
+ }, "strip", z.ZodTypeAny, {
96
+ factor?: number | undefined;
97
+ maxSeconds?: number | undefined;
98
+ }, {
99
+ factor?: number | undefined;
100
+ maxSeconds?: number | undefined;
101
+ }>>;
92
102
  }, "strip", z.ZodTypeAny, {
93
103
  name: string;
94
104
  timeout?: string | undefined;
@@ -108,6 +118,10 @@ export declare const CreateStepSchema: z.ZodObject<{
108
118
  comparator?: WorkerLabelComparator | undefined;
109
119
  weight?: number | undefined;
110
120
  } | undefined> | undefined;
121
+ backoff?: {
122
+ factor?: number | undefined;
123
+ maxSeconds?: number | undefined;
124
+ } | undefined;
111
125
  }, {
112
126
  name: string;
113
127
  timeout?: string | undefined;
@@ -127,6 +141,10 @@ export declare const CreateStepSchema: z.ZodObject<{
127
141
  comparator?: WorkerLabelComparator | undefined;
128
142
  weight?: number | undefined;
129
143
  } | undefined> | undefined;
144
+ backoff?: {
145
+ factor?: number | undefined;
146
+ maxSeconds?: number | undefined;
147
+ } | undefined;
130
148
  }>;
131
149
  export type JsonObject = {
132
150
  [Key in string]: JsonValue;
package/step.js CHANGED
@@ -84,6 +84,12 @@ exports.CreateStepSchema = z.object({
84
84
  retries: z.number().optional(),
85
85
  rate_limits: z.array(exports.CreateRateLimitSchema).optional(),
86
86
  worker_labels: z.record(z.lazy(() => exports.DesiredWorkerLabelSchema)).optional(),
87
+ backoff: z
88
+ .object({
89
+ factor: z.number().optional(),
90
+ maxSeconds: z.number().optional(),
91
+ })
92
+ .optional(),
87
93
  });
88
94
  class ContextWorker {
89
95
  constructor(worker) {
package/util/retrier.js CHANGED
@@ -14,9 +14,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.retrier = retrier;
16
16
  const sleep_1 = __importDefault(require("./sleep"));
17
- const DEFAULT_RETRY_INTERVAL = 1; // seconds
17
+ const DEFAULT_RETRY_INTERVAL = 0.1; // seconds
18
18
  const DEFAULT_RETRY_COUNT = 8;
19
- const MAX_JITTER = 400; // milliseconds
19
+ const MAX_JITTER = 100; // milliseconds
20
20
  function retrier(fn_1, logger_1) {
21
21
  return __awaiter(this, arguments, void 0, function* (fn, logger, retries = DEFAULT_RETRY_COUNT, interval = DEFAULT_RETRY_INTERVAL) {
22
22
  let lastError;
@@ -43,10 +43,10 @@ function getWorkflowRunId(workflowRunId) {
43
43
  if (e.code && e.code === nice_grpc_1.Status.ALREADY_EXISTS) {
44
44
  throw new DedupeViolationErr(e.details);
45
45
  }
46
- throw new Error('Invalid workflowRunId');
46
+ throw e;
47
47
  }
48
48
  }
49
- throw new Error('Invalid workflowRunId');
49
+ throw new Error('Invalid workflowRunId: must be a string or a promise');
50
50
  });
51
51
  }
52
52
  class WorkflowRunRef {
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "0.16.0";
1
+ export declare const HATCHET_VERSION = "0.17.1";
package/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HATCHET_VERSION = void 0;
4
- exports.HATCHET_VERSION = '0.16.0';
4
+ exports.HATCHET_VERSION = '0.17.1';
package/workflow.d.ts CHANGED
@@ -44,6 +44,16 @@ declare const StepsSchema: z.ZodArray<z.ZodObject<{
44
44
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
45
45
  weight?: number | undefined;
46
46
  }>]>>>>>;
47
+ backoff: z.ZodOptional<z.ZodObject<{
48
+ factor: z.ZodOptional<z.ZodNumber>;
49
+ maxSeconds: z.ZodOptional<z.ZodNumber>;
50
+ }, "strip", z.ZodTypeAny, {
51
+ factor?: number | undefined;
52
+ maxSeconds?: number | undefined;
53
+ }, {
54
+ factor?: number | undefined;
55
+ maxSeconds?: number | undefined;
56
+ }>>;
47
57
  }, "strip", z.ZodTypeAny, {
48
58
  name: string;
49
59
  timeout?: string | undefined;
@@ -63,6 +73,10 @@ declare const StepsSchema: z.ZodArray<z.ZodObject<{
63
73
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
64
74
  weight?: number | undefined;
65
75
  } | undefined> | undefined;
76
+ backoff?: {
77
+ factor?: number | undefined;
78
+ maxSeconds?: number | undefined;
79
+ } | undefined;
66
80
  }, {
67
81
  name: string;
68
82
  timeout?: string | undefined;
@@ -82,6 +96,10 @@ declare const StepsSchema: z.ZodArray<z.ZodObject<{
82
96
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
83
97
  weight?: number | undefined;
84
98
  } | undefined> | undefined;
99
+ backoff?: {
100
+ factor?: number | undefined;
101
+ maxSeconds?: number | undefined;
102
+ } | undefined;
85
103
  }>, "many">;
86
104
  export type Steps = z.infer<typeof StepsSchema>;
87
105
  export declare const ConcurrencyLimitStrategy: typeof PbConcurrencyLimitStrategy;
@@ -178,6 +196,16 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
178
196
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
179
197
  weight?: number | undefined;
180
198
  }>]>>>>>;
199
+ backoff: z.ZodOptional<z.ZodObject<{
200
+ factor: z.ZodOptional<z.ZodNumber>;
201
+ maxSeconds: z.ZodOptional<z.ZodNumber>;
202
+ }, "strip", z.ZodTypeAny, {
203
+ factor?: number | undefined;
204
+ maxSeconds?: number | undefined;
205
+ }, {
206
+ factor?: number | undefined;
207
+ maxSeconds?: number | undefined;
208
+ }>>;
181
209
  }, "strip", z.ZodTypeAny, {
182
210
  name: string;
183
211
  timeout?: string | undefined;
@@ -197,6 +225,10 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
197
225
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
198
226
  weight?: number | undefined;
199
227
  } | undefined> | undefined;
228
+ backoff?: {
229
+ factor?: number | undefined;
230
+ maxSeconds?: number | undefined;
231
+ } | undefined;
200
232
  }, {
201
233
  name: string;
202
234
  timeout?: string | undefined;
@@ -216,6 +248,10 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
216
248
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
217
249
  weight?: number | undefined;
218
250
  } | undefined> | undefined;
251
+ backoff?: {
252
+ factor?: number | undefined;
253
+ maxSeconds?: number | undefined;
254
+ } | undefined;
219
255
  }>, "many">;
220
256
  onFailure: z.ZodOptional<z.ZodObject<{
221
257
  name: z.ZodString;
@@ -260,6 +296,16 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
260
296
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
261
297
  weight?: number | undefined;
262
298
  }>]>>>>>;
299
+ backoff: z.ZodOptional<z.ZodObject<{
300
+ factor: z.ZodOptional<z.ZodNumber>;
301
+ maxSeconds: z.ZodOptional<z.ZodNumber>;
302
+ }, "strip", z.ZodTypeAny, {
303
+ factor?: number | undefined;
304
+ maxSeconds?: number | undefined;
305
+ }, {
306
+ factor?: number | undefined;
307
+ maxSeconds?: number | undefined;
308
+ }>>;
263
309
  }, "strip", z.ZodTypeAny, {
264
310
  name: string;
265
311
  timeout?: string | undefined;
@@ -279,6 +325,10 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
279
325
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
280
326
  weight?: number | undefined;
281
327
  } | undefined> | undefined;
328
+ backoff?: {
329
+ factor?: number | undefined;
330
+ maxSeconds?: number | undefined;
331
+ } | undefined;
282
332
  }, {
283
333
  name: string;
284
334
  timeout?: string | undefined;
@@ -298,6 +348,10 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
298
348
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
299
349
  weight?: number | undefined;
300
350
  } | undefined> | undefined;
351
+ backoff?: {
352
+ factor?: number | undefined;
353
+ maxSeconds?: number | undefined;
354
+ } | undefined;
301
355
  }>>;
302
356
  }, "strip", z.ZodTypeAny, {
303
357
  description: string;
@@ -320,6 +374,10 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
320
374
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
321
375
  weight?: number | undefined;
322
376
  } | undefined> | undefined;
377
+ backoff?: {
378
+ factor?: number | undefined;
379
+ maxSeconds?: number | undefined;
380
+ } | undefined;
323
381
  }[];
324
382
  id: string;
325
383
  version?: string | undefined;
@@ -352,6 +410,10 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
352
410
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
353
411
  weight?: number | undefined;
354
412
  } | undefined> | undefined;
413
+ backoff?: {
414
+ factor?: number | undefined;
415
+ maxSeconds?: number | undefined;
416
+ } | undefined;
355
417
  } | undefined;
356
418
  }, {
357
419
  description: string;
@@ -374,6 +436,10 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
374
436
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
375
437
  weight?: number | undefined;
376
438
  } | undefined> | undefined;
439
+ backoff?: {
440
+ factor?: number | undefined;
441
+ maxSeconds?: number | undefined;
442
+ } | undefined;
377
443
  }[];
378
444
  id: string;
379
445
  version?: string | undefined;
@@ -406,6 +472,10 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
406
472
  comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
407
473
  weight?: number | undefined;
408
474
  } | undefined> | undefined;
475
+ backoff?: {
476
+ factor?: number | undefined;
477
+ maxSeconds?: number | undefined;
478
+ } | undefined;
409
479
  } | undefined;
410
480
  }>;
411
481
  export interface Workflow extends z.infer<typeof CreateWorkflowSchema> {