@hatchet-dev/typescript-sdk 1.13.1 → 1.14.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.
Files changed (245) hide show
  1. package/clients/dispatcher/dispatcher-client.d.ts +1 -1
  2. package/clients/dispatcher/heartbeat/heartbeat-controller.js +13 -1
  3. package/clients/dispatcher/heartbeat/heartbeat-worker.js +4 -3
  4. package/clients/event/event-client.d.ts +1 -2
  5. package/clients/event/event-client.js +2 -2
  6. package/clients/hatchet-client/client-config.d.ts +15 -5
  7. package/clients/hatchet-client/client-config.js +3 -0
  8. package/clients/hatchet-client/hatchet-logger.js +1 -0
  9. package/clients/hatchet-client/index.d.ts +1 -1
  10. package/clients/hatchet-client/index.js +1 -1
  11. package/clients/listeners/durable-listener/durable-listener-client.d.ts +6 -0
  12. package/clients/listeners/durable-listener/durable-listener-client.js +8 -0
  13. package/clients/listeners/durable-listener/pooled-durable-listener-client.d.ts +9 -2
  14. package/clients/listeners/durable-listener/pooled-durable-listener-client.js +67 -15
  15. package/clients/listeners/run-listener/child-listener-client.js +1 -0
  16. package/clients/listeners/run-listener/pooled-child-listener-client.d.ts +10 -2
  17. package/clients/listeners/run-listener/pooled-child-listener-client.js +63 -7
  18. package/clients/rest/api.js +10 -2
  19. package/clients/rest/generated/Api.d.ts +2 -2
  20. package/clients/rest/generated/data-contracts.d.ts +6 -0
  21. package/index.d.ts +2 -3
  22. package/index.js +2 -3
  23. package/{examples → legacy/examples}/affinity-workers.js +2 -2
  24. package/{examples → legacy/examples}/bulk-fanout-trigger.js +1 -1
  25. package/{examples → legacy/examples}/bulk-fanout-worker.js +1 -1
  26. package/{examples → legacy/examples}/bulk-trigger.js +1 -1
  27. package/{examples → legacy/examples}/byo-logger.js +1 -1
  28. package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-event.js +1 -1
  29. package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-worker.js +1 -1
  30. package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-event.js +1 -1
  31. package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-expression.js +2 -2
  32. package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-key-fn.js +2 -2
  33. package/{examples → legacy/examples}/crons/cron-worker.d.ts +1 -1
  34. package/{examples → legacy/examples}/crons/cron-worker.js +1 -1
  35. package/{examples → legacy/examples}/crons/programatic-crons.js +1 -1
  36. package/{examples → legacy/examples}/dag-worker.js +1 -1
  37. package/{examples → legacy/examples}/example-event-with-results.js +2 -2
  38. package/{examples → legacy/examples}/example-event.js +1 -1
  39. package/{examples → legacy/examples}/fanout-worker.js +1 -1
  40. package/{examples → legacy/examples}/logger.js +1 -1
  41. package/{examples → legacy/examples}/manual-trigger.js +1 -1
  42. package/{examples → legacy/examples}/multi-workflow.js +1 -1
  43. package/{examples → legacy/examples}/namespaced-worker.js +1 -1
  44. package/{examples → legacy/examples}/on-failure.js +1 -1
  45. package/{examples → legacy/examples}/rate-limit/events.js +1 -1
  46. package/{examples → legacy/examples}/rate-limit/worker.js +2 -2
  47. package/{examples → legacy/examples}/retries-with-backoff.js +1 -1
  48. package/{examples → legacy/examples}/retries-worker.js +1 -1
  49. package/{examples → legacy/examples}/scheduled-runs/programatic-schedules.js +1 -1
  50. package/{examples → legacy/examples}/simple-worker.d.ts +1 -1
  51. package/{examples → legacy/examples}/simple-worker.js +1 -1
  52. package/{examples → legacy/examples}/sticky-trigger.js +1 -1
  53. package/{examples → legacy/examples}/sticky-worker-with-check.js +2 -2
  54. package/{examples → legacy/examples}/sticky-worker.js +2 -2
  55. package/{examples → legacy/examples}/stream-by-additional-meta.js +2 -2
  56. package/legacy/legacy-client.d.ts +30 -0
  57. package/legacy/legacy-client.js +67 -0
  58. package/legacy/legacy-transformer.d.ts +38 -0
  59. package/legacy/legacy-transformer.js +181 -0
  60. package/legacy/step.d.ts +429 -0
  61. package/legacy/step.js +662 -0
  62. package/legacy/workflow.d.ts +488 -0
  63. package/legacy/workflow.js +72 -0
  64. package/package.json +2 -2
  65. package/step.d.ts +1 -430
  66. package/step.js +6 -647
  67. package/util/abort-error.d.ts +38 -0
  68. package/util/abort-error.js +58 -0
  69. package/util/hatchet-promise/hatchet-promise.d.ts +8 -1
  70. package/util/hatchet-promise/hatchet-promise.js +3 -2
  71. package/util/logger/index.d.ts +1 -0
  72. package/util/logger/index.js +1 -0
  73. package/util/logger/task-run-log.d.ts +3 -0
  74. package/util/logger/task-run-log.js +20 -0
  75. package/util/sleep.d.ts +5 -2
  76. package/util/sleep.js +27 -4
  77. package/util/workflow-run-ref.d.ts +7 -1
  78. package/util/workflow-run-ref.js +5 -3
  79. package/v1/client/admin.d.ts +8 -1
  80. package/v1/client/admin.js +22 -6
  81. package/v1/client/client.d.ts +36 -18
  82. package/v1/client/client.interface.d.ts +9 -2
  83. package/v1/client/client.js +47 -27
  84. package/v1/client/features/crons.d.ts +3 -3
  85. package/v1/client/features/crons.js +2 -2
  86. package/v1/client/features/schedules.js +2 -2
  87. package/v1/client/features/workflows.d.ts +5 -5
  88. package/v1/client/features/workflows.js +7 -8
  89. package/v1/client/worker/context.d.ts +49 -10
  90. package/v1/client/worker/context.js +92 -20
  91. package/v1/client/worker/deprecated/legacy-v1-worker.d.ts +3 -3
  92. package/v1/client/worker/deprecated/legacy-v1-worker.js +2 -2
  93. package/v1/client/worker/deprecated/legacy-worker.js +9 -7
  94. package/v1/client/worker/slot-utils.d.ts +3 -3
  95. package/v1/client/worker/worker-internal.d.ts +7 -16
  96. package/v1/client/worker/worker-internal.js +225 -209
  97. package/v1/client/worker/worker.d.ts +18 -18
  98. package/v1/client/worker/worker.js +45 -30
  99. package/v1/declaration.d.ts +28 -7
  100. package/v1/declaration.js +56 -6
  101. package/v1/examples/__e2e__/harness.d.ts +19 -0
  102. package/v1/examples/__e2e__/harness.js +70 -0
  103. package/v1/examples/affinity/affinity-workers.js +4 -4
  104. package/v1/examples/bulk_fanout/workflow.d.ts +9 -0
  105. package/v1/examples/bulk_fanout/workflow.js +34 -0
  106. package/v1/examples/bulk_operations/workflow.d.ts +3 -0
  107. package/v1/examples/bulk_operations/workflow.js +44 -0
  108. package/v1/examples/cancellation/cancellation-workflow.d.ts +2 -0
  109. package/v1/examples/cancellation/cancellation-workflow.js +69 -0
  110. package/v1/examples/cancellation/run.js +53 -0
  111. package/v1/examples/cancellation/worker.d.ts +1 -0
  112. package/v1/examples/cancellation/worker.js +29 -0
  113. package/v1/examples/concurrency-types.d.ts +5 -0
  114. package/v1/examples/concurrency-types.js +2 -0
  115. package/v1/examples/concurrency_cancel_in_progress/workflow.d.ts +9 -0
  116. package/v1/examples/concurrency_cancel_in_progress/workflow.js +45 -0
  117. package/v1/examples/concurrency_cancel_newest/workflow.d.ts +9 -0
  118. package/v1/examples/concurrency_cancel_newest/workflow.js +45 -0
  119. package/v1/examples/concurrency_limit_rr/load.d.ts +1 -0
  120. package/v1/examples/concurrency_limit_rr/load.js +54 -0
  121. package/v1/examples/concurrency_limit_rr/run.d.ts +1 -0
  122. package/v1/examples/concurrency_limit_rr/run.js +39 -0
  123. package/v1/examples/concurrency_limit_rr/worker.d.ts +1 -0
  124. package/v1/examples/concurrency_limit_rr/worker.js +24 -0
  125. package/v1/examples/concurrency_limit_rr/workflow.d.ts +12 -0
  126. package/v1/examples/concurrency_limit_rr/workflow.js +62 -0
  127. package/v1/examples/concurrency_multiple_keys/workflow.d.ts +12 -0
  128. package/v1/examples/concurrency_multiple_keys/workflow.js +42 -0
  129. package/v1/examples/concurrency_workflow_level/workflow.d.ts +13 -0
  130. package/v1/examples/concurrency_workflow_level/workflow.js +49 -0
  131. package/v1/examples/conditions/complex-workflow.d.ts +1 -0
  132. package/v1/examples/conditions/complex-workflow.js +107 -0
  133. package/v1/examples/conditions/event.d.ts +1 -0
  134. package/v1/examples/conditions/event.js +28 -0
  135. package/v1/examples/conditions/run.d.ts +1 -0
  136. package/v1/examples/conditions/run.js +25 -0
  137. package/v1/examples/conditions/worker.d.ts +1 -0
  138. package/v1/examples/conditions/worker.js +24 -0
  139. package/v1/examples/conditions/workflow.d.ts +11 -0
  140. package/v1/examples/conditions/workflow.js +41 -0
  141. package/v1/examples/durable/workflow.d.ts +7 -0
  142. package/v1/examples/durable/workflow.js +116 -0
  143. package/v1/examples/durable_event/event.d.ts +1 -0
  144. package/v1/examples/durable_event/event.js +28 -0
  145. package/v1/examples/durable_event/run.d.ts +1 -0
  146. package/v1/examples/durable_event/run.js +30 -0
  147. package/v1/examples/durable_event/worker.d.ts +1 -0
  148. package/v1/examples/durable_event/worker.js +24 -0
  149. package/v1/examples/durable_event/workflow.d.ts +6 -0
  150. package/v1/examples/durable_event/workflow.js +46 -0
  151. package/v1/examples/durable_sleep/event.d.ts +1 -0
  152. package/v1/examples/durable_sleep/event.js +28 -0
  153. package/v1/examples/durable_sleep/run.d.ts +1 -0
  154. package/v1/examples/durable_sleep/run.js +30 -0
  155. package/v1/examples/durable_sleep/worker.d.ts +1 -0
  156. package/v1/examples/durable_sleep/worker.js +24 -0
  157. package/v1/examples/durable_sleep/workflow.d.ts +1 -0
  158. package/v1/examples/durable_sleep/workflow.js +31 -0
  159. package/v1/examples/e2e-worker.d.ts +1 -0
  160. package/v1/examples/e2e-worker.js +82 -0
  161. package/v1/examples/events/event.d.ts +1 -0
  162. package/v1/examples/events/event.js +53 -0
  163. package/v1/examples/events/filter.d.ts +1 -0
  164. package/v1/examples/events/filter.js +32 -0
  165. package/v1/examples/events/worker.d.ts +1 -0
  166. package/v1/examples/events/worker.js +24 -0
  167. package/v1/examples/events/workflow.d.ts +19 -0
  168. package/v1/examples/events/workflow.js +60 -0
  169. package/v1/examples/high-memory/workflow-with-child.js +1 -1
  170. package/v1/examples/logger/byo-logger.d.ts +1 -0
  171. package/v1/examples/logger/byo-logger.js +73 -0
  172. package/v1/examples/logger/logger.d.ts +1 -0
  173. package/v1/examples/logger/logger.js +46 -0
  174. package/v1/examples/logger/workflow.d.ts +2 -0
  175. package/v1/examples/logger/workflow.js +38 -0
  176. package/v1/examples/multiple_wf_concurrency/workflow.js +3 -3
  177. package/v1/examples/on_failure/workflow.d.ts +1 -0
  178. package/v1/examples/on_failure/workflow.js +12 -7
  179. package/v1/examples/priority/workflow.js +1 -1
  180. package/v1/examples/retries/workflow.js +2 -2
  181. package/v1/examples/return_exceptions/workflow.d.ts +6 -0
  182. package/v1/examples/return_exceptions/workflow.js +22 -0
  183. package/v1/examples/run_details/workflow.d.ts +4 -0
  184. package/v1/examples/run_details/workflow.js +65 -0
  185. package/v1/examples/simple/e2e-workflows.d.ts +14 -0
  186. package/v1/examples/simple/e2e-workflows.js +27 -0
  187. package/v1/examples/simple/enqueue.js +2 -2
  188. package/v1/examples/sticky/workflow.js +2 -2
  189. package/v1/examples/timeout/run.d.ts +1 -0
  190. package/v1/examples/timeout/run.js +30 -0
  191. package/v1/examples/timeout/worker.d.ts +1 -0
  192. package/v1/examples/timeout/worker.js +25 -0
  193. package/v1/examples/timeout/workflow.d.ts +10 -0
  194. package/v1/examples/timeout/workflow.js +56 -0
  195. package/v1/examples/webhooks/workflow.d.ts +5 -0
  196. package/v1/examples/webhooks/workflow.js +23 -0
  197. package/v1/index.d.ts +1 -0
  198. package/v1/index.js +1 -0
  199. package/v1/parent-run-context-vars.d.ts +6 -0
  200. package/v1/parent-run-context-vars.js +3 -0
  201. package/v1/task.d.ts +22 -7
  202. package/v1/task.js +4 -1
  203. package/version.d.ts +1 -1
  204. package/version.js +1 -1
  205. package/workflow.d.ts +3 -491
  206. package/workflow.js +12 -62
  207. package/clients/hatchet-client/hatchet-client.d.ts +0 -35
  208. package/clients/hatchet-client/hatchet-client.js +0 -108
  209. package/clients/worker/handler.d.ts +0 -50
  210. package/clients/worker/handler.js +0 -214
  211. package/clients/worker/index.d.ts +0 -1
  212. package/clients/worker/index.js +0 -17
  213. package/clients/worker/worker.d.ts +0 -59
  214. package/clients/worker/worker.js +0 -568
  215. package/examples/webhooks.js +0 -45
  216. /package/{examples → legacy/examples}/affinity-workers.d.ts +0 -0
  217. /package/{examples → legacy/examples}/bulk-fanout-trigger.d.ts +0 -0
  218. /package/{examples → legacy/examples}/bulk-fanout-worker.d.ts +0 -0
  219. /package/{examples → legacy/examples}/bulk-trigger.d.ts +0 -0
  220. /package/{examples → legacy/examples}/byo-logger.d.ts +0 -0
  221. /package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-event.d.ts +0 -0
  222. /package/{examples → legacy/examples}/concurrency/cancel-in-progress/concurrency-worker.d.ts +0 -0
  223. /package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-event.d.ts +0 -0
  224. /package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-expression.d.ts +0 -0
  225. /package/{examples → legacy/examples}/concurrency/group-round-robin/concurrency-worker-key-fn.d.ts +0 -0
  226. /package/{examples → legacy/examples}/crons/programatic-crons.d.ts +0 -0
  227. /package/{examples → legacy/examples}/dag-worker.d.ts +0 -0
  228. /package/{examples → legacy/examples}/example-event-with-results.d.ts +0 -0
  229. /package/{examples → legacy/examples}/example-event.d.ts +0 -0
  230. /package/{examples → legacy/examples}/fanout-worker.d.ts +0 -0
  231. /package/{examples → legacy/examples}/logger.d.ts +0 -0
  232. /package/{examples → legacy/examples}/manual-trigger.d.ts +0 -0
  233. /package/{examples → legacy/examples}/multi-workflow.d.ts +0 -0
  234. /package/{examples → legacy/examples}/namespaced-worker.d.ts +0 -0
  235. /package/{examples → legacy/examples}/on-failure.d.ts +0 -0
  236. /package/{examples → legacy/examples}/rate-limit/events.d.ts +0 -0
  237. /package/{examples → legacy/examples}/rate-limit/worker.d.ts +0 -0
  238. /package/{examples → legacy/examples}/retries-with-backoff.d.ts +0 -0
  239. /package/{examples → legacy/examples}/retries-worker.d.ts +0 -0
  240. /package/{examples → legacy/examples}/scheduled-runs/programatic-schedules.d.ts +0 -0
  241. /package/{examples → legacy/examples}/sticky-trigger.d.ts +0 -0
  242. /package/{examples → legacy/examples}/sticky-worker-with-check.d.ts +0 -0
  243. /package/{examples → legacy/examples}/sticky-worker.d.ts +0 -0
  244. /package/{examples → legacy/examples}/stream-by-additional-meta.d.ts +0 -0
  245. /package/{examples/webhooks.d.ts → v1/examples/cancellation/run.d.ts} +0 -0
@@ -1,568 +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.V0Worker = void 0;
23
- const hatchet_error_1 = __importDefault(require("../../util/errors/hatchet-error"));
24
- const action_listener_1 = require("../dispatcher/action-listener");
25
- const dispatcher_1 = require("../../protoc/dispatcher");
26
- const hatchet_promise_1 = __importDefault(require("../../util/hatchet-promise/hatchet-promise"));
27
- const workflows_1 = require("../../protoc/workflows");
28
- const handler_1 = require("./handler");
29
- const task_1 = require("../../v1/task");
30
- const apply_namespace_1 = require("../../util/apply-namespace");
31
- const step_1 = require("../../step");
32
- class V0Worker {
33
- constructor(client, options) {
34
- this.workflow_registry = [];
35
- this.futures = {};
36
- this.contexts = {};
37
- this.registeredWorkflowPromises = [];
38
- this.labels = {};
39
- this.client = client;
40
- this.name = (0, apply_namespace_1.applyNamespace)(options.name, this.client.config.namespace);
41
- this.action_registry = {};
42
- this.maxRuns = options.maxRuns;
43
- this.labels = options.labels || {};
44
- process.on('SIGTERM', () => this.exitGracefully(true));
45
- process.on('SIGINT', () => this.exitGracefully(true));
46
- this.killing = false;
47
- this.handle_kill = options.handleKill === undefined ? true : options.handleKill;
48
- this.logger = client.config.logger(`Worker/${this.name}`, this.client.config.log_level);
49
- }
50
- registerActions(workflow) {
51
- var _a;
52
- const newActions = workflow.steps.reduce((acc, step) => {
53
- acc[`${workflow.id}:${step.name.toLowerCase()}`] = step.run;
54
- return acc;
55
- }, {});
56
- const onFailureAction = workflow.onFailure
57
- ? {
58
- [`${workflow.id}-on-failure:${workflow.onFailure.name}`]: workflow.onFailure.run,
59
- }
60
- : {};
61
- this.action_registry = Object.assign(Object.assign(Object.assign({}, this.action_registry), newActions), onFailureAction);
62
- this.action_registry =
63
- ((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.name) && workflow.concurrency.key
64
- ? Object.assign(Object.assign({}, this.action_registry), { [`${workflow.id}:${workflow.concurrency.name.toLowerCase()}`]: workflow.concurrency.key }) : Object.assign({}, this.action_registry);
65
- }
66
- getHandler(workflows) {
67
- // TODO v1
68
- for (const workflow of workflows) {
69
- const wf = Object.assign(Object.assign({}, workflow), { id: (0, apply_namespace_1.applyNamespace)(workflow.id, this.client.config.namespace) });
70
- this.registerActions(wf);
71
- }
72
- return new handler_1.WebhookHandler(this, workflows);
73
- }
74
- registerWebhook(webhook) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- return this.client.admin.registerWebhook(Object.assign({}, webhook));
77
- });
78
- }
79
- /**
80
- * @deprecated use registerWorkflow instead
81
- */
82
- register_workflow(initWorkflow) {
83
- return __awaiter(this, void 0, void 0, function* () {
84
- return this.registerWorkflow(initWorkflow);
85
- });
86
- }
87
- registerWorkflow(initWorkflow) {
88
- return __awaiter(this, void 0, void 0, function* () {
89
- var _a, _b, _c;
90
- const workflow = Object.assign(Object.assign({}, initWorkflow), { id: (0, apply_namespace_1.applyNamespace)(initWorkflow.id, this.client.config.namespace).toLowerCase() });
91
- try {
92
- if (((_a = workflow.concurrency) === null || _a === void 0 ? void 0 : _a.key) && workflow.concurrency.expression) {
93
- throw new hatchet_error_1.default('Cannot have both key function and expression in workflow concurrency configuration');
94
- }
95
- const concurrency = ((_b = workflow.concurrency) === null || _b === void 0 ? void 0 : _b.name) || ((_c = workflow.concurrency) === null || _c === void 0 ? void 0 : _c.expression)
96
- ? {
97
- action: !workflow.concurrency.expression
98
- ? `${workflow.id}:${workflow.concurrency.name}`
99
- : undefined,
100
- maxRuns: workflow.concurrency.maxRuns || 1,
101
- expression: workflow.concurrency.expression,
102
- limitStrategy: workflow.concurrency.limitStrategy || workflows_1.ConcurrencyLimitStrategy.CANCEL_IN_PROGRESS,
103
- }
104
- : undefined;
105
- const onFailureJob = workflow.onFailure
106
- ? {
107
- name: `${workflow.id}-on-failure`,
108
- description: workflow.description,
109
- steps: [
110
- {
111
- readableId: workflow.onFailure.name,
112
- action: `${workflow.id}-on-failure:${workflow.onFailure.name}`,
113
- timeout: workflow.onFailure.timeout || '60s',
114
- inputs: '{}',
115
- parents: [],
116
- userData: '{}',
117
- retries: workflow.onFailure.retries || 0,
118
- rateLimits: (0, step_1.mapRateLimit)(workflow.onFailure.rate_limits),
119
- workerLabels: {}, // no worker labels for on failure steps
120
- },
121
- ],
122
- }
123
- : undefined;
124
- const registeredWorkflow = this.client.admin.putWorkflow({
125
- name: workflow.id,
126
- description: workflow.description,
127
- version: workflow.version || '',
128
- eventTriggers: workflow.on && workflow.on.event
129
- ? [(0, apply_namespace_1.applyNamespace)(workflow.on.event, this.client.config.namespace)]
130
- : [],
131
- cronTriggers: workflow.on && workflow.on.cron ? [workflow.on.cron] : [],
132
- scheduledTriggers: [],
133
- concurrency,
134
- scheduleTimeout: workflow.scheduleTimeout,
135
- onFailureJob,
136
- sticky: workflow.sticky,
137
- jobs: [
138
- {
139
- name: workflow.id,
140
- description: workflow.description,
141
- steps: workflow.steps.map((step) => {
142
- var _a, _b, _c;
143
- return ({
144
- readableId: step.name,
145
- action: `${workflow.id}:${step.name}`,
146
- timeout: step.timeout || '60s',
147
- inputs: '{}',
148
- parents: (_a = step.parents) !== null && _a !== void 0 ? _a : [],
149
- userData: '{}',
150
- retries: step.retries || 0,
151
- rateLimits: (0, step_1.mapRateLimit)(step.rate_limits),
152
- workerLabels: toPbWorkerLabel(step.worker_labels),
153
- backoffFactor: (_b = step.backoff) === null || _b === void 0 ? void 0 : _b.factor,
154
- backoffMaxSeconds: (_c = step.backoff) === null || _c === void 0 ? void 0 : _c.maxSeconds,
155
- });
156
- }),
157
- },
158
- ],
159
- });
160
- this.registeredWorkflowPromises.push(registeredWorkflow);
161
- yield registeredWorkflow;
162
- this.workflow_registry.push(workflow);
163
- }
164
- catch (e) {
165
- throw new hatchet_error_1.default(`Could not register workflow: ${e.message}`);
166
- }
167
- this.registerActions(workflow);
168
- });
169
- }
170
- registerAction(actionId, action) {
171
- this.action_registry[actionId.toLowerCase()] = action;
172
- }
173
- handleStartStepRun(action) {
174
- return __awaiter(this, void 0, void 0, function* () {
175
- const { actionId } = action;
176
- try {
177
- // Note: we always use a DurableContext since its a superset of the Context class
178
- const context = new step_1.V0DurableContext(action, this.client, this);
179
- this.contexts[(0, action_listener_1.createActionKey)(action)] = context;
180
- const step = this.action_registry[actionId];
181
- if (!step) {
182
- this.logger.error(`Registered actions: '${Object.keys(this.action_registry).join(', ')}'`);
183
- this.logger.error(`Could not find step '${actionId}'`);
184
- return;
185
- }
186
- const run = () => __awaiter(this, void 0, void 0, function* () {
187
- const { middleware } = this.client.config;
188
- if (middleware === null || middleware === void 0 ? void 0 : middleware.before) {
189
- const hooks = Array.isArray(middleware.before) ? middleware.before : [middleware.before];
190
- for (const hook of hooks) {
191
- const extra = yield hook(context.input, context);
192
- if (extra !== undefined) {
193
- const merged = Object.assign(Object.assign({}, context.input), extra);
194
- context.input = merged;
195
- if (context.data && typeof context.data === 'object') {
196
- context.data.input = merged;
197
- }
198
- }
199
- }
200
- }
201
- let result = yield step(context);
202
- if (middleware === null || middleware === void 0 ? void 0 : middleware.after) {
203
- const hooks = Array.isArray(middleware.after) ? middleware.after : [middleware.after];
204
- for (const hook of hooks) {
205
- const extra = yield hook(result, context, context.input);
206
- if (extra !== undefined) {
207
- result = Object.assign(Object.assign({}, result), extra);
208
- }
209
- }
210
- }
211
- return result;
212
- });
213
- const success = (result) => __awaiter(this, void 0, void 0, function* () {
214
- this.logger.info(`Task run ${action.taskRunExternalId} succeeded`);
215
- try {
216
- // Send the action event to the dispatcher
217
- const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_COMPLETED, false, result || null, action.retryCount);
218
- yield this.client.dispatcher.sendStepActionEvent(event);
219
- }
220
- catch (actionEventError) {
221
- this.logger.error(`Could not send completed action event: ${actionEventError.message || actionEventError}`);
222
- // send a failure event
223
- const failureEvent = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, false, actionEventError.message, action.retryCount);
224
- try {
225
- yield this.client.dispatcher.sendStepActionEvent(failureEvent);
226
- }
227
- catch (failureEventError) {
228
- this.logger.error(`Could not send failed action event: ${failureEventError.message || failureEventError}`);
229
- }
230
- this.logger.error(`Could not send action event: ${actionEventError.message || actionEventError}`);
231
- }
232
- finally {
233
- // delete the run from the futures
234
- delete this.futures[(0, action_listener_1.createActionKey)(action)];
235
- delete this.contexts[(0, action_listener_1.createActionKey)(action)];
236
- }
237
- });
238
- const failure = (error) => __awaiter(this, void 0, void 0, function* () {
239
- this.logger.error(`Task run ${action.taskRunExternalId} failed: ${error.message}`);
240
- if (error.stack) {
241
- this.logger.error(error.stack);
242
- }
243
- const shouldNotRetry = error instanceof task_1.NonRetryableError;
244
- try {
245
- // Send the action event to the dispatcher
246
- const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_FAILED, shouldNotRetry, {
247
- message: error === null || error === void 0 ? void 0 : error.message,
248
- stack: error === null || error === void 0 ? void 0 : error.stack,
249
- }, action.retryCount);
250
- yield this.client.dispatcher.sendStepActionEvent(event);
251
- }
252
- catch (e) {
253
- this.logger.error(`Could not send action event: ${e.message}`);
254
- }
255
- finally {
256
- // delete the run from the futures
257
- delete this.futures[(0, action_listener_1.createActionKey)(action)];
258
- delete this.contexts[(0, action_listener_1.createActionKey)(action)];
259
- }
260
- });
261
- const future = new hatchet_promise_1.default((() => __awaiter(this, void 0, void 0, function* () {
262
- let result;
263
- try {
264
- result = yield run();
265
- }
266
- catch (e) {
267
- yield failure(e);
268
- return;
269
- }
270
- yield success(result);
271
- }))());
272
- this.futures[(0, action_listener_1.createActionKey)(action)] = future;
273
- // Send the action event to the dispatcher
274
- const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_STARTED, false, undefined, action.retryCount);
275
- this.client.dispatcher.sendStepActionEvent(event).catch((e) => {
276
- this.logger.error(`Could not send action event: ${e.message}`);
277
- });
278
- try {
279
- yield future.promise;
280
- }
281
- catch (e) {
282
- const message = (e === null || e === void 0 ? void 0 : e.message) || String(e);
283
- if (message.includes('Cancelled')) {
284
- this.logger.debug(`Task run ${action.taskRunExternalId} was cancelled`);
285
- }
286
- else {
287
- this.logger.error(`Could not wait for task run ${action.taskRunExternalId} to finish. ` +
288
- `See https://docs.hatchet.run/home/cancellation for best practices on handling cancellation: `, e);
289
- }
290
- }
291
- }
292
- catch (e) {
293
- this.logger.error('Could not send action event (outer): ', e);
294
- }
295
- });
296
- }
297
- handleStartGroupKeyRun(action) {
298
- return __awaiter(this, void 0, void 0, function* () {
299
- const { actionId } = action;
300
- try {
301
- const context = new step_1.V0Context(action, this.client, this);
302
- const key = (0, action_listener_1.createActionKey)(action);
303
- if (!key) {
304
- this.logger.error(`No group key run id provided for action ${actionId}`);
305
- return;
306
- }
307
- this.contexts[key] = context;
308
- this.logger.debug(`Starting group key run ${key}`);
309
- const step = this.action_registry[actionId];
310
- if (!step) {
311
- this.logger.error(`Could not find step '${actionId}'`);
312
- return;
313
- }
314
- const run = () => __awaiter(this, void 0, void 0, function* () {
315
- return step(context);
316
- });
317
- const success = (result) => {
318
- this.logger.info(`Task run ${action.taskRunExternalId} succeeded`);
319
- try {
320
- // Send the action event to the dispatcher
321
- const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_COMPLETED, result);
322
- this.client.dispatcher.sendGroupKeyActionEvent(event).catch((e) => {
323
- this.logger.error(`Could not send action event: ${e.message}`);
324
- });
325
- }
326
- catch (e) {
327
- this.logger.error(`Could not send action event: ${e.message}`);
328
- }
329
- finally {
330
- // delete the run from the futures
331
- delete this.futures[key];
332
- delete this.contexts[key];
333
- }
334
- };
335
- const failure = (error) => {
336
- this.logger.error(`Task run ${key} failed: ${error.message}`);
337
- try {
338
- // Send the action event to the dispatcher
339
- const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_FAILED, error);
340
- this.client.dispatcher.sendGroupKeyActionEvent(event).catch((e) => {
341
- this.logger.error(`Could not send action event: ${e.message}`);
342
- });
343
- }
344
- catch (e) {
345
- this.logger.error(`Could not send action event: ${e.message}`);
346
- }
347
- finally {
348
- // delete the run from the futures
349
- delete this.futures[key];
350
- delete this.contexts[key];
351
- }
352
- };
353
- const future = new hatchet_promise_1.default(run().then(success).catch(failure));
354
- this.futures[key] = future;
355
- // Send the action event to the dispatcher
356
- const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_STARTED);
357
- this.client.dispatcher.sendGroupKeyActionEvent(event).catch((e) => {
358
- this.logger.error(`Could not send action event: ${e.message}`);
359
- });
360
- yield future.promise;
361
- }
362
- catch (e) {
363
- this.logger.error(`Could not send action event: ${e.message}`);
364
- }
365
- });
366
- }
367
- getStepActionEvent(action, eventType, shouldNotRetry, payload = '', retryCount = 0) {
368
- return {
369
- workerId: this.name,
370
- jobId: action.jobId,
371
- jobRunId: action.jobRunId,
372
- taskId: action.taskId,
373
- taskRunExternalId: action.taskRunExternalId,
374
- actionId: action.actionId,
375
- eventTimestamp: new Date(),
376
- eventType,
377
- eventPayload: JSON.stringify(payload),
378
- shouldNotRetry,
379
- retryCount,
380
- };
381
- }
382
- getGroupKeyActionEvent(action, eventType, payload = '') {
383
- if (!action.getGroupKeyRunId) {
384
- throw new hatchet_error_1.default('No group key run id provided');
385
- }
386
- return {
387
- workerId: this.name,
388
- workflowRunId: action.workflowRunId,
389
- getGroupKeyRunId: action.getGroupKeyRunId,
390
- actionId: action.actionId,
391
- eventTimestamp: new Date(),
392
- eventType,
393
- eventPayload: JSON.stringify(payload),
394
- };
395
- }
396
- handleCancelStepRun(action) {
397
- return __awaiter(this, void 0, void 0, function* () {
398
- const { taskRunExternalId } = action;
399
- try {
400
- this.logger.info(`Cancelling task run ${action.taskRunExternalId}`);
401
- const future = this.futures[(0, action_listener_1.createActionKey)(action)];
402
- const context = this.contexts[(0, action_listener_1.createActionKey)(action)];
403
- if (context && context.controller) {
404
- context.controller.abort('Cancelled by worker');
405
- }
406
- if (future) {
407
- future.promise.catch(() => {
408
- this.logger.info(`Cancelled task run ${action.taskRunExternalId}`);
409
- });
410
- future.cancel('Cancelled by worker');
411
- yield future.promise;
412
- }
413
- }
414
- catch (e) {
415
- // Expected: the promise rejects when cancelled
416
- this.logger.debug(`Task run ${taskRunExternalId} cancellation completed`);
417
- }
418
- finally {
419
- delete this.futures[(0, action_listener_1.createActionKey)(action)];
420
- delete this.contexts[(0, action_listener_1.createActionKey)(action)];
421
- }
422
- });
423
- }
424
- stop() {
425
- return __awaiter(this, void 0, void 0, function* () {
426
- yield this.exitGracefully(false);
427
- });
428
- }
429
- exitGracefully(handleKill) {
430
- return __awaiter(this, void 0, void 0, function* () {
431
- var _a;
432
- this.killing = true;
433
- this.logger.info('Starting to exit...');
434
- try {
435
- yield ((_a = this.listener) === null || _a === void 0 ? void 0 : _a.unregister());
436
- }
437
- catch (e) {
438
- this.logger.error(`Could not unregister listener: ${e.message}`);
439
- }
440
- this.logger.info('Gracefully exiting hatchet worker, running tasks will attempt to finish...');
441
- // attempt to wait for futures to finish
442
- yield Promise.all(Object.values(this.futures).map(({ promise }) => promise));
443
- this.logger.info('Successfully finished pending tasks.');
444
- if (handleKill) {
445
- this.logger.info('Exiting hatchet worker...');
446
- process.exit(0);
447
- }
448
- });
449
- }
450
- start() {
451
- return __awaiter(this, void 0, void 0, function* () {
452
- var _a, e_1, _b, _c;
453
- // ensure all workflows are registered
454
- yield Promise.all(this.registeredWorkflowPromises);
455
- if (Object.keys(this.action_registry).length === 0) {
456
- return;
457
- }
458
- try {
459
- this.listener = yield this.client.dispatcher.getActionListener({
460
- workerName: this.name,
461
- services: ['default'],
462
- actions: Object.keys(this.action_registry),
463
- maxRuns: this.maxRuns,
464
- labels: this.labels,
465
- });
466
- this.workerId = this.listener.workerId;
467
- const generator = this.listener.actions();
468
- this.logger.info(`Worker ${this.name} listening for actions`);
469
- try {
470
- for (var _d = true, generator_1 = __asyncValues(generator), generator_1_1; generator_1_1 = yield generator_1.next(), _a = generator_1_1.done, !_a; _d = true) {
471
- _c = generator_1_1.value;
472
- _d = false;
473
- const action = _c;
474
- this.logger.info(`Worker ${this.name} received action ${action.actionId}:${action.actionType}`);
475
- void this.handleAction(action);
476
- }
477
- }
478
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
479
- finally {
480
- try {
481
- if (!_d && !_a && (_b = generator_1.return)) yield _b.call(generator_1);
482
- }
483
- finally { if (e_1) throw e_1.error; }
484
- }
485
- }
486
- catch (e) {
487
- if (this.killing) {
488
- this.logger.info(`Exiting worker, ignoring error: ${e.message}`);
489
- return;
490
- }
491
- this.logger.error(`Could not run worker: ${e.message}`);
492
- throw new hatchet_error_1.default(`Could not run worker: ${e.message}`);
493
- }
494
- });
495
- }
496
- handleAction(action) {
497
- return __awaiter(this, void 0, void 0, function* () {
498
- const type = action.actionType
499
- ? (0, dispatcher_1.actionTypeFromJSON)(action.actionType)
500
- : dispatcher_1.ActionType.START_STEP_RUN;
501
- if (type === dispatcher_1.ActionType.START_STEP_RUN) {
502
- yield this.handleStartStepRun(action);
503
- }
504
- else if (type === dispatcher_1.ActionType.CANCEL_STEP_RUN) {
505
- yield this.handleCancelStepRun(action);
506
- }
507
- else if (type === dispatcher_1.ActionType.START_GET_GROUP_KEY) {
508
- yield this.handleStartGroupKeyRun(action);
509
- }
510
- else {
511
- this.logger.error(`Worker ${this.name} received unknown action type ${type}`);
512
- }
513
- });
514
- }
515
- upsertLabels(labels) {
516
- return __awaiter(this, void 0, void 0, function* () {
517
- this.labels = labels;
518
- if (!this.workerId) {
519
- this.logger.warn('Worker not registered.');
520
- return this.labels;
521
- }
522
- this.client.dispatcher.upsertWorkerLabels(this.workerId, labels);
523
- return this.labels;
524
- });
525
- }
526
- }
527
- exports.V0Worker = V0Worker;
528
- function toPbWorkerLabel(in_) {
529
- if (!in_) {
530
- return {};
531
- }
532
- return Object.entries(in_).reduce((acc, [key, value]) => {
533
- if (!value) {
534
- return Object.assign(Object.assign({}, acc), { [key]: {
535
- strValue: undefined,
536
- intValue: undefined,
537
- } });
538
- }
539
- if (typeof value === 'string') {
540
- return Object.assign(Object.assign({}, acc), { [key]: {
541
- strValue: value,
542
- intValue: undefined,
543
- } });
544
- }
545
- if (typeof value === 'number') {
546
- return Object.assign(Object.assign({}, acc), { [key]: {
547
- strValue: undefined,
548
- intValue: value,
549
- } });
550
- }
551
- return Object.assign(Object.assign({}, acc), { [key]: {
552
- strValue: typeof value.value === 'string' ? value.value : undefined,
553
- intValue: typeof value.value === 'number' ? value.value : undefined,
554
- required: value.required,
555
- weight: value.weight,
556
- comparator: value.comparator,
557
- } });
558
- }, {});
559
- }
560
- function onFailureTaskName(workflow) {
561
- return `${workflow.name}:on-failure-task`;
562
- }
563
- function getLeaves(tasks) {
564
- return tasks.filter((task) => isLeafTask(task, tasks));
565
- }
566
- function isLeafTask(task, allTasks) {
567
- return !allTasks.some((t) => { var _a; return (_a = t.parents) === null || _a === void 0 ? void 0 : _a.some((p) => p.name === task.name); });
568
- }
@@ -1,45 +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 sdk_1 = __importDefault(require("../sdk"));
16
- const data_contracts_1 = require("../clients/rest/generated/data-contracts");
17
- const hatchet = sdk_1.default.init();
18
- function main() {
19
- return __awaiter(this, void 0, void 0, function* () {
20
- var _a, _b, _c, _d;
21
- const webhookName = `example-webhook-${Date.now()}`;
22
- const namespace = (_b = (_a = hatchet.config) === null || _a === void 0 ? void 0 : _a.namespace) !== null && _b !== void 0 ? _b : 'default';
23
- const webhook = yield hatchet.webhooks.create({
24
- sourceName: data_contracts_1.V1WebhookSourceName.GENERIC,
25
- name: webhookName,
26
- eventKeyExpression: `'${namespace}/webhook:' + input.type`,
27
- scopeExpression: 'input.customer_id',
28
- staticPayload: { customer_id: 'cust-123', environment: 'production' },
29
- auth: { username: 'test_user', password: 'test_password' },
30
- });
31
- console.log('Created webhook:', webhook.name, webhook.scopeExpression, webhook.staticPayload);
32
- const one = yield hatchet.webhooks.get(webhook.name);
33
- console.log('Get webhook:', one.name);
34
- yield hatchet.webhooks.update(webhook.name, {
35
- scopeExpression: 'input.environment',
36
- });
37
- const updated = yield hatchet.webhooks.get(webhook.name);
38
- console.log('Updated scope expression:', updated.scopeExpression);
39
- const list = yield hatchet.webhooks.list({ limit: 10, offset: 0 });
40
- console.log('List webhooks:', (_d = (_c = list.rows) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0);
41
- yield hatchet.webhooks.delete(webhook.name);
42
- console.log('Deleted example webhook');
43
- });
44
- }
45
- main();
File without changes
File without changes
File without changes
File without changes
File without changes