@hatchet-dev/typescript-sdk 1.1.0 → 1.1.2

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 CHANGED
@@ -102,7 +102,7 @@ pnpm test
102
102
 
103
103
  ## Contributing
104
104
 
105
- We welcome contributions! Please check out our [contributing guidelines](https://docs.hatchet.run/contributing) and join our [Discord community](https://discord.gg/ZMeUafwH89) for discussions and support.
105
+ We welcome contributions! Please check out our [contributing guidelines](https://docs.hatchet.run/contributing) and join our [Discord community](https://hatchet.run/discord) for discussions and support.
106
106
 
107
107
  ## License
108
108
 
@@ -51,7 +51,7 @@ export declare class V0Worker {
51
51
  registerAction<T, K>(actionId: string, action: StepRunFunction<T, K>): void;
52
52
  handleStartStepRun(action: Action): Promise<void>;
53
53
  handleStartGroupKeyRun(action: Action): Promise<void>;
54
- getStepActionEvent(action: Action, eventType: StepActionEventType, shouldNotRetry: boolean, payload?: any): StepActionEvent;
54
+ getStepActionEvent(action: Action, eventType: StepActionEventType, shouldNotRetry: boolean, payload?: any, retryCount?: number): StepActionEvent;
55
55
  getGroupKeyActionEvent(action: Action, eventType: GroupKeyActionEventType, payload?: any): GroupKeyActionEvent;
56
56
  handleCancelStepRun(action: Action): Promise<void>;
57
57
  stop(): Promise<void>;
@@ -346,13 +346,13 @@ class V0Worker {
346
346
  this.logger.info(`Step run ${action.stepRunId} succeeded`);
347
347
  try {
348
348
  // Send the action event to the dispatcher
349
- const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_COMPLETED, false, result || null);
349
+ const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_COMPLETED, false, result || null, action.retryCount);
350
350
  yield this.client.dispatcher.sendStepActionEvent(event);
351
351
  }
352
352
  catch (actionEventError) {
353
353
  this.logger.error(`Could not send completed action event: ${actionEventError.message || actionEventError}`);
354
354
  // send a failure event
355
- const failureEvent = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, false, actionEventError.message);
355
+ const failureEvent = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, false, actionEventError.message, action.retryCount);
356
356
  try {
357
357
  yield this.client.dispatcher.sendStepActionEvent(failureEvent);
358
358
  }
@@ -378,7 +378,7 @@ class V0Worker {
378
378
  const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, shouldNotRetry, {
379
379
  message: error === null || error === void 0 ? void 0 : error.message,
380
380
  stack: error === null || error === void 0 ? void 0 : error.stack,
381
- });
381
+ }, action.retryCount);
382
382
  yield this.client.dispatcher.sendStepActionEvent(event);
383
383
  }
384
384
  catch (e) {
@@ -403,7 +403,7 @@ class V0Worker {
403
403
  }))());
404
404
  this.futures[action.stepRunId] = future;
405
405
  // Send the action event to the dispatcher
406
- const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_STARTED, false);
406
+ const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_STARTED, false, undefined, action.retryCount);
407
407
  this.client.dispatcher.sendStepActionEvent(event).catch((e) => {
408
408
  this.logger.error(`Could not send action event: ${e.message}`);
409
409
  });
@@ -489,7 +489,7 @@ class V0Worker {
489
489
  }
490
490
  });
491
491
  }
492
- getStepActionEvent(action, eventType, shouldNotRetry, payload = '') {
492
+ getStepActionEvent(action, eventType, shouldNotRetry, payload = '', retryCount = 0) {
493
493
  return {
494
494
  workerId: this.name,
495
495
  jobId: action.jobId,
@@ -501,6 +501,7 @@ class V0Worker {
501
501
  eventType,
502
502
  eventPayload: JSON.stringify(payload),
503
503
  shouldNotRetry,
504
+ retryCount,
504
505
  };
505
506
  }
506
507
  getGroupKeyActionEvent(action, eventType, payload = '') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
package/step.js CHANGED
@@ -413,7 +413,13 @@ class Context {
413
413
  return resp;
414
414
  });
415
415
  try {
416
- const resp = yield this.client.admin.runWorkflows(workflowRuns);
416
+ // Batch workflowRuns in groups of 500
417
+ let resp = [];
418
+ for (let i = 0; i < workflowRuns.length; i += 500) {
419
+ const batch = workflowRuns.slice(i, i + 500);
420
+ const batchResp = yield this.client.admin.runWorkflows(batch);
421
+ resp = resp.concat(batchResp);
422
+ }
417
423
  const res = [];
418
424
  resp.forEach((ref, index) => {
419
425
  const wf = workflows[index].workflow;
@@ -13,6 +13,7 @@ import { WorkflowsClient } from './features/workflows';
13
13
  import { RunsClient } from './features/runs';
14
14
  import { CreateStandaloneDurableTaskOpts } from '../task';
15
15
  import { InputType, OutputType, UnknownInputType, StrictWorkflowOutputType } from '../types';
16
+ import { RatelimitsClient } from './features';
16
17
  /**
17
18
  * HatchetV1 implements the main client interface for interacting with the Hatchet workflow engine.
18
19
  * It provides methods for creating and executing workflows, as well as managing workers.
@@ -159,6 +160,12 @@ export declare class HatchetClient implements IHatchetClient {
159
160
  * @returns A metrics client instance
160
161
  */
161
162
  get metrics(): MetricsClient;
163
+ private _ratelimits;
164
+ /**
165
+ * Get the rate limits client for creating and managing rate limits
166
+ * @returns A rate limits client instance
167
+ */
168
+ get ratelimits(): RatelimitsClient;
162
169
  private _runs;
163
170
  /**
164
171
  * Get the runs client for creating and managing runs
@@ -27,6 +27,7 @@ const metrics_1 = require("./features/metrics");
27
27
  const workers_1 = require("./features/workers");
28
28
  const workflows_1 = require("./features/workflows");
29
29
  const runs_1 = require("./features/runs");
30
+ const features_1 = require("./features");
30
31
  /**
31
32
  * HatchetV1 implements the main client interface for interacting with the Hatchet workflow engine.
32
33
  * It provides methods for creating and executing workflows, as well as managing workers.
@@ -210,6 +211,16 @@ class HatchetClient {
210
211
  }
211
212
  return this._metrics;
212
213
  }
214
+ /**
215
+ * Get the rate limits client for creating and managing rate limits
216
+ * @returns A rate limits client instance
217
+ */
218
+ get ratelimits() {
219
+ if (!this._ratelimits) {
220
+ this._ratelimits = new features_1.RatelimitsClient(this);
221
+ }
222
+ return this._ratelimits;
223
+ }
213
224
  /**
214
225
  * Get the runs client for creating and managing runs
215
226
  * @returns A runs client instance
package/v1/declaration.js CHANGED
@@ -63,8 +63,26 @@ class BaseWorkflowDeclaration {
63
63
  throw UNBOUND_ERR;
64
64
  }
65
65
  if (Array.isArray(input)) {
66
- // FIXME use bulk endpoint?
67
- return Promise.all(input.map((i) => this.run(i, options, _standaloneTaskName)));
66
+ let resp = [];
67
+ for (let i = 0; i < input.length; i += 500) {
68
+ const batch = input.slice(i, i + 500);
69
+ const batchResp = yield this.client._v0.admin.runWorkflows(batch.map((inp) => ({
70
+ workflowName: this.definition.name,
71
+ input: inp,
72
+ options,
73
+ })));
74
+ resp = resp.concat(batchResp);
75
+ }
76
+ const res = [];
77
+ resp.forEach((ref, index) => {
78
+ const wf = input[index].workflow;
79
+ if (wf instanceof TaskWorkflowDeclaration) {
80
+ // eslint-disable-next-line no-param-reassign
81
+ ref._standalone_task_name = wf._standalone_task_name;
82
+ }
83
+ res.push(ref.result());
84
+ });
85
+ return Promise.all(res);
68
86
  }
69
87
  const res = this.client._v0.admin.runWorkflow(this.definition.name, input, options);
70
88
  if (_standaloneTaskName) {
@@ -10,10 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const hatchet_client_1 = require("../hatchet-client");
13
- const workflow_1 = require("./workflow");
14
13
  function main() {
15
14
  return __awaiter(this, void 0, void 0, function* () {
16
- const res = yield hatchet_client_1.hatchet.events.push(workflow_1.SIMPLE_EVENT, {
15
+ // Pushing an Event
16
+ const res = yield hatchet_client_1.hatchet.events.push('simple-event:create', {
17
17
  Message: 'hello',
18
18
  });
19
19
  // !!
@@ -6,10 +6,8 @@ exports.SIMPLE_EVENT = 'simple-event:create';
6
6
  // ❓ Run workflow on event
7
7
  exports.lower = hatchet_client_1.hatchet.workflow({
8
8
  name: 'lower',
9
- on: {
10
- // 👀 Declare the event that will trigger the workflow
11
- event: exports.SIMPLE_EVENT,
12
- },
9
+ // 👀 Declare the event that will trigger the workflow
10
+ onEvents: ['simple-event:create'],
13
11
  });
14
12
  // !!
15
13
  exports.lower.task({
@@ -1,5 +1 @@
1
- type RateLimitInput = {
2
- userId: string;
3
- };
4
- export declare const rateLimitWorkflow: import("../..").WorkflowDeclaration<RateLimitInput, {}>;
5
1
  export {};
@@ -1,29 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rateLimitWorkflow = void 0;
4
3
  const workflows_1 = require("../../../protoc/v1/workflows");
5
4
  const hatchet_client_1 = require("../hatchet-client");
6
- exports.rateLimitWorkflow = hatchet_client_1.hatchet.workflow({
7
- name: 'RateLimitWorkflow',
5
+ // Upsert Rate Limit
6
+ hatchet_client_1.hatchet.ratelimits.upsert({
7
+ key: 'api-service-rate-limit',
8
+ limit: 10,
9
+ duration: workflows_1.RateLimitDuration.SECOND,
8
10
  });
9
11
  // !!
10
12
  // ❓ Static
11
- const RATE_LIMIT_KEY = 'test-limit';
12
- const task1 = exports.rateLimitWorkflow.task({
13
+ const RATE_LIMIT_KEY = 'api-service-rate-limit';
14
+ const task1 = hatchet_client_1.hatchet.task({
13
15
  name: 'task1',
14
- fn: (input) => {
15
- console.log('executed task1');
16
- },
17
16
  rateLimits: [
18
17
  {
19
18
  staticKey: RATE_LIMIT_KEY,
20
19
  units: 1,
21
20
  },
22
21
  ],
22
+ fn: (input) => {
23
+ console.log('executed task1');
24
+ },
23
25
  });
24
26
  // !!
25
27
  // ❓ Dynamic
26
- const task2 = exports.rateLimitWorkflow.task({
28
+ const task2 = hatchet_client_1.hatchet.task({
27
29
  name: 'task2',
28
30
  fn: (input) => {
29
31
  console.log('executed task2 for user: ', input.userId);
@@ -0,0 +1,59 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ /* eslint-disable no-console */
13
+ const hatchet_client_1 = require("../hatchet-client");
14
+ const workflow_1 = require("./workflow");
15
+ function main() {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ // ❓ Bulk Run a Task
18
+ const res = yield workflow_1.simple.run([
19
+ {
20
+ Message: 'HeLlO WoRlD',
21
+ },
22
+ {
23
+ Message: 'Hello MoOn',
24
+ },
25
+ ]);
26
+ // 👀 Access the results of the Task
27
+ console.log(res[0].TransformedMessage);
28
+ console.log(res[1].TransformedMessage);
29
+ // !!
30
+ // ❓ Bulk Run Tasks from within a Task
31
+ const parent = hatchet_client_1.hatchet.task({
32
+ name: 'simple',
33
+ fn: (input, ctx) => __awaiter(this, void 0, void 0, function* () {
34
+ // Bulk run two tasks in parallel
35
+ const child = yield ctx.bulkRunChildren([
36
+ {
37
+ workflow: workflow_1.simple,
38
+ input: {
39
+ Message: 'Hello, World!',
40
+ },
41
+ },
42
+ {
43
+ workflow: workflow_1.simple,
44
+ input: {
45
+ Message: 'Hello, Moon!',
46
+ },
47
+ },
48
+ ]);
49
+ return {
50
+ TransformedMessage: `${child[0].TransformedMessage} ${child[1].TransformedMessage}`,
51
+ };
52
+ }),
53
+ });
54
+ // !!
55
+ });
56
+ }
57
+ if (require.main === module) {
58
+ main();
59
+ }
@@ -22,7 +22,16 @@ function main() {
22
22
  // !!
23
23
  // eslint-disable-next-line no-console
24
24
  console.log(cron.metadata.id);
25
- yield hatchet_client_1.hatchet.crons.delete(cron);
25
+ // ❓ Delete
26
+ yield hatchet_client_1.hatchet.crons.delete(cronId);
27
+ // !!
28
+ // ❓ List
29
+ const crons = yield hatchet_client_1.hatchet.crons.list({
30
+ workflowId: workflow_1.simple.id,
31
+ });
32
+ // !!
33
+ // eslint-disable-next-line no-console
34
+ console.log(crons);
26
35
  });
27
36
  }
28
37
  if (require.main === module) {
@@ -1 +1 @@
1
- export {};
1
+ export declare function extra(): Promise<void>;
@@ -9,7 +9,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.extra = extra;
12
13
  /* eslint-disable no-console */
14
+ const hatchet_client_1 = require("../hatchet-client");
13
15
  const workflow_1 = require("./workflow");
14
16
  function main() {
15
17
  return __awaiter(this, void 0, void 0, function* () {
@@ -22,6 +24,35 @@ function main() {
22
24
  // !!
23
25
  });
24
26
  }
27
+ function extra() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ // ❓ Running Multiple Tasks
30
+ const res1 = workflow_1.simple.run({
31
+ Message: 'HeLlO WoRlD',
32
+ });
33
+ const res2 = workflow_1.simple.run({
34
+ Message: 'Hello MoOn',
35
+ });
36
+ const results = yield Promise.all([res1, res2]);
37
+ console.log(results[0].TransformedMessage);
38
+ console.log(results[1].TransformedMessage);
39
+ // !!
40
+ // ❓ Spawning Tasks from within a Task
41
+ const parent = hatchet_client_1.hatchet.task({
42
+ name: 'parent',
43
+ fn: (input, ctx) => __awaiter(this, void 0, void 0, function* () {
44
+ // Simply call ctx.runChild with the task you want to run
45
+ const child = yield ctx.runChild(workflow_1.simple, {
46
+ Message: 'HeLlO WoRlD',
47
+ });
48
+ return {
49
+ result: child.TransformedMessage,
50
+ };
51
+ }),
52
+ });
53
+ // !!
54
+ });
55
+ }
25
56
  if (require.main === module) {
26
57
  main();
27
58
  }
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "1.1.0";
1
+ export declare const HATCHET_VERSION = "1.1.2";
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 = '1.1.0';
4
+ exports.HATCHET_VERSION = '1.1.2';
@@ -1,29 +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
- Object.defineProperty(exports, "__esModule", { value: true });
12
- /* eslint-disable no-console */
13
- const workflow_1 = require("./workflow");
14
- function main() {
15
- return __awaiter(this, void 0, void 0, function* () {
16
- try {
17
- const res = yield workflow_1.rateLimitWorkflow.run({ userId: 'abc' });
18
- console.log(res);
19
- }
20
- catch (e) {
21
- console.log('error', e);
22
- }
23
- });
24
- }
25
- if (require.main === module) {
26
- main()
27
- .catch(console.error)
28
- .finally(() => process.exit(0));
29
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,24 +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
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const hatchet_client_1 = require("../hatchet-client");
13
- const workflow_1 = require("./workflow");
14
- function main() {
15
- return __awaiter(this, void 0, void 0, function* () {
16
- const worker = yield hatchet_client_1.hatchet.worker('rate-limit-worker', {
17
- workflows: [workflow_1.rateLimitWorkflow],
18
- });
19
- yield worker.start();
20
- });
21
- }
22
- if (require.main === module) {
23
- main();
24
- }