@hatchet-dev/typescript-sdk 1.10.7 → 1.11.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.
@@ -807,6 +807,10 @@ export interface V1Webhook {
807
807
  sourceName: V1WebhookSourceName;
808
808
  /** The CEL expression to use for the event key. This is used to create the event key from the webhook payload. */
809
809
  eventKeyExpression: string;
810
+ /** The CEL expression to use for the scope. This is used to filter the correct workflow to trigger. */
811
+ scopeExpression?: string;
812
+ /** The static payload to use for the webhook. This is used to send a static payload with the webhook. */
813
+ staticPayload?: object;
810
814
  /** The type of authentication to use for the webhook */
811
815
  authType: V1WebhookAuthType;
812
816
  }
@@ -821,6 +825,10 @@ export interface V1CreateWebhookRequestBase {
821
825
  name: string;
822
826
  /** The CEL expression to use for the event key. This is used to create the event key from the webhook payload. */
823
827
  eventKeyExpression: string;
828
+ /** The CEL expression to use for the scope. This is used to filter the correct workflow to trigger. */
829
+ scopeExpression?: string;
830
+ /** The static payload to use for the webhook. This is used to send a static payload with the webhook. */
831
+ staticPayload?: object;
824
832
  }
825
833
  export interface V1WebhookBasicAuth {
826
834
  /** The username for basic auth */
@@ -863,6 +871,10 @@ export type V1CreateWebhookRequest = V1CreateWebhookRequestBasicAuth | V1CreateW
863
871
  export interface V1UpdateWebhookRequest {
864
872
  /** The CEL expression to use for the event key. This is used to create the event key from the webhook payload. */
865
873
  eventKeyExpression: string;
874
+ /** The CEL expression to use for the scope. This is used to filter the correct workflow to trigger. */
875
+ scopeExpression?: string;
876
+ /** The static payload to use for the webhook. This is used to send a static payload with the webhook. */
877
+ staticPayload?: object;
866
878
  }
867
879
  export interface V1CELDebugRequest {
868
880
  /** The CEL expression to evaluate */
@@ -187,7 +187,7 @@ class V0Worker {
187
187
  return step(context);
188
188
  });
189
189
  const success = (result) => __awaiter(this, void 0, void 0, function* () {
190
- this.logger.info(`Step run ${action.stepRunId} succeeded`);
190
+ this.logger.info(`Task run ${action.stepRunId} succeeded`);
191
191
  try {
192
192
  // Send the action event to the dispatcher
193
193
  const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_COMPLETED, false, result || null, action.retryCount);
@@ -212,7 +212,7 @@ class V0Worker {
212
212
  }
213
213
  });
214
214
  const failure = (error) => __awaiter(this, void 0, void 0, function* () {
215
- this.logger.error(`Step run ${action.stepRunId} failed: ${error.message}`);
215
+ this.logger.error(`Task run ${action.stepRunId} failed: ${error.message}`);
216
216
  if (error.stack) {
217
217
  this.logger.error(error.stack);
218
218
  }
@@ -255,7 +255,14 @@ class V0Worker {
255
255
  yield future.promise;
256
256
  }
257
257
  catch (e) {
258
- this.logger.error('Could not wait for step run to finish: ', e);
258
+ const message = (e === null || e === void 0 ? void 0 : e.message) || String(e);
259
+ if (message.includes('Cancelled')) {
260
+ this.logger.debug(`Task run ${action.stepRunId} was cancelled`);
261
+ }
262
+ else {
263
+ this.logger.error(`Could not wait for task run ${action.stepRunId} to finish. ` +
264
+ `See https://docs.hatchet.run/home/cancellation for best practices on handling cancellation: `, e);
265
+ }
259
266
  }
260
267
  }
261
268
  catch (e) {
@@ -284,7 +291,7 @@ class V0Worker {
284
291
  return step(context);
285
292
  });
286
293
  const success = (result) => {
287
- this.logger.info(`Step run ${action.stepRunId} succeeded`);
294
+ this.logger.info(`Task run ${action.stepRunId} succeeded`);
288
295
  try {
289
296
  // Send the action event to the dispatcher
290
297
  const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_COMPLETED, result);
@@ -302,7 +309,7 @@ class V0Worker {
302
309
  }
303
310
  };
304
311
  const failure = (error) => {
305
- this.logger.error(`Step run ${key} failed: ${error.message}`);
312
+ this.logger.error(`Task run ${key} failed: ${error.message}`);
306
313
  try {
307
314
  // Send the action event to the dispatcher
308
315
  const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_FAILED, error);
@@ -366,7 +373,7 @@ class V0Worker {
366
373
  return __awaiter(this, void 0, void 0, function* () {
367
374
  const { stepRunId } = action;
368
375
  try {
369
- this.logger.info(`Cancelling step run ${action.stepRunId}`);
376
+ this.logger.info(`Cancelling task run ${action.stepRunId}`);
370
377
  const future = this.futures[(0, action_listener_1.createActionKey)(action)];
371
378
  const context = this.contexts[(0, action_listener_1.createActionKey)(action)];
372
379
  if (context && context.controller) {
@@ -374,14 +381,15 @@ class V0Worker {
374
381
  }
375
382
  if (future) {
376
383
  future.promise.catch(() => {
377
- this.logger.info(`Cancelled step run ${action.stepRunId}`);
384
+ this.logger.info(`Cancelled task run ${action.stepRunId}`);
378
385
  });
379
386
  future.cancel('Cancelled by worker');
380
387
  yield future.promise;
381
388
  }
382
389
  }
383
390
  catch (e) {
384
- this.logger.error('Could not cancel step run: ', e);
391
+ // Expected: the promise rejects when cancelled
392
+ this.logger.debug(`Task run ${stepRunId} cancellation completed`);
385
393
  }
386
394
  finally {
387
395
  delete this.futures[(0, action_listener_1.createActionKey)(action)];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "1.10.7",
3
+ "version": "1.11.0",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -1,6 +1,11 @@
1
- import { BaseWorkflowDeclaration } from '../..';
2
- import { Workflow } from '../../../workflow';
3
1
  import { HatchetClient } from '../client';
2
+ export type TaskStatusMetrics = {
3
+ cancelled: number;
4
+ completed: number;
5
+ failed: number;
6
+ queued: number;
7
+ running: number;
8
+ };
4
9
  /**
5
10
  * MetricsClient is used to get metrics for workflows
6
11
  */
@@ -8,7 +13,13 @@ export declare class MetricsClient {
8
13
  tenantId: string;
9
14
  api: HatchetClient['api'];
10
15
  constructor(client: HatchetClient);
11
- getWorkflowMetrics(workflow: string | Workflow | BaseWorkflowDeclaration<any, any>, opts?: Parameters<typeof this.api.workflowGetMetrics>[1]): Promise<import("../../../clients/rest/generated/data-contracts").WorkflowMetrics>;
12
- getQueueMetrics(opts?: Parameters<typeof this.api.tenantGetQueueMetrics>[1] & {}): Promise<import("../../../clients/rest/generated/data-contracts").TenantQueueMetrics>;
13
- getTaskMetrics(opts?: Parameters<typeof this.api.tenantGetStepRunQueueMetrics>[1]): Promise<import("../../../clients/rest/generated/data-contracts").TenantStepRunQueueMetrics>;
16
+ /**
17
+ * Get task/run status metrics for a tenant.
18
+ *
19
+ * This backs the dashboard "runs list" status count badges.
20
+ *
21
+ * Endpoint: GET /api/v1/stable/tenants/{tenant}/task-metrics
22
+ */
23
+ getTaskStatusMetrics(query: Parameters<typeof this.api.v1TaskListStatusMetrics>[1], requestParams?: Parameters<typeof this.api.v1TaskListStatusMetrics>[2]): Promise<TaskStatusMetrics>;
24
+ getQueueMetrics(opts?: Parameters<typeof this.api.tenantGetStepRunQueueMetrics>[1]): Promise<import("../../../clients/rest/generated/data-contracts").TenantStepRunQueueMetrics>;
14
25
  }
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MetricsClient = void 0;
13
- const v1_1 = require("../..");
14
13
  /**
15
14
  * MetricsClient is used to get metrics for workflows
16
15
  */
@@ -19,24 +18,24 @@ class MetricsClient {
19
18
  this.tenantId = client.tenantId;
20
19
  this.api = client.api;
21
20
  }
22
- getWorkflowMetrics(workflow, opts) {
21
+ /**
22
+ * Get task/run status metrics for a tenant.
23
+ *
24
+ * This backs the dashboard "runs list" status count badges.
25
+ *
26
+ * Endpoint: GET /api/v1/stable/tenants/{tenant}/task-metrics
27
+ */
28
+ getTaskStatusMetrics(query, requestParams) {
23
29
  return __awaiter(this, void 0, void 0, function* () {
24
- const name = (0, v1_1.workflowNameString)(workflow);
25
- const { data } = yield this.api.workflowGetMetrics(name, opts);
26
- return data;
30
+ const { data } = yield this.api.v1TaskListStatusMetrics(this.tenantId, query, requestParams);
31
+ return data.reduce((acc, curr) => {
32
+ acc[curr.status.toLowerCase()] = curr.count;
33
+ return acc;
34
+ }, {});
27
35
  });
28
36
  }
29
37
  getQueueMetrics(opts) {
30
38
  return __awaiter(this, void 0, void 0, function* () {
31
- // TODO IMPORTANT workflow id is the uuid for the workflow... not its name
32
- // const stringWorkflows = opts?.workflows?
33
- const { data } = yield this.api.tenantGetQueueMetrics(this.tenantId, Object.assign({}, opts));
34
- return data;
35
- });
36
- }
37
- getTaskMetrics(opts) {
38
- return __awaiter(this, void 0, void 0, function* () {
39
- // TODO what is this...
40
39
  const { data } = yield this.api.tenantGetStepRunQueueMetrics(this.tenantId, opts);
41
40
  return data;
42
41
  });
@@ -423,7 +423,7 @@ class V1Worker {
423
423
  if (context.cancelled) {
424
424
  return;
425
425
  }
426
- this.logger.info(`Step run ${action.stepRunId} succeeded`);
426
+ this.logger.info(`Task run ${action.stepRunId} succeeded`);
427
427
  // Send the action event to the dispatcher
428
428
  const event = this.getStepActionEvent(action, dispatcher_1.StepActionEventType.STEP_EVENT_TYPE_COMPLETED, false, result || null, action.retryCount);
429
429
  yield this.client._v0.dispatcher.sendStepActionEvent(event);
@@ -452,7 +452,7 @@ class V1Worker {
452
452
  if (context.cancelled) {
453
453
  return;
454
454
  }
455
- this.logger.error(`Step run ${action.stepRunId} failed: ${error.message}`);
455
+ this.logger.error(`Task run ${action.stepRunId} failed: ${error.message}`);
456
456
  if (error.stack) {
457
457
  this.logger.error(error.stack);
458
458
  }
@@ -493,7 +493,14 @@ class V1Worker {
493
493
  yield future.promise;
494
494
  }
495
495
  catch (e) {
496
- this.logger.error('Could not wait for step run to finish: ', e);
496
+ const message = (e === null || e === void 0 ? void 0 : e.message) || String(e);
497
+ if (message.includes('Cancelled')) {
498
+ this.logger.debug(`Task run ${action.stepRunId} was cancelled`);
499
+ }
500
+ else {
501
+ this.logger.error(`Could not wait for task run ${action.stepRunId} to finish. ` +
502
+ `See https://docs.hatchet.run/home/cancellation for best practices on handling cancellation: `, e);
503
+ }
497
504
  }
498
505
  }
499
506
  catch (e) {
@@ -523,7 +530,7 @@ class V1Worker {
523
530
  return step(context);
524
531
  });
525
532
  const success = (result) => {
526
- this.logger.info(`Step run ${action.stepRunId} succeeded`);
533
+ this.logger.info(`Task run ${action.stepRunId} succeeded`);
527
534
  try {
528
535
  // Send the action event to the dispatcher
529
536
  const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_COMPLETED, result);
@@ -541,7 +548,7 @@ class V1Worker {
541
548
  }
542
549
  };
543
550
  const failure = (error) => {
544
- this.logger.error(`Step run ${key} failed: ${error.message}`);
551
+ this.logger.error(`Task run ${key} failed: ${error.message}`);
545
552
  try {
546
553
  // Send the action event to the dispatcher
547
554
  const event = this.getGroupKeyActionEvent(action, dispatcher_1.GroupKeyActionEventType.GROUP_KEY_EVENT_TYPE_FAILED, error);
@@ -605,7 +612,7 @@ class V1Worker {
605
612
  return __awaiter(this, void 0, void 0, function* () {
606
613
  const { stepRunId } = action;
607
614
  try {
608
- this.logger.info(`Cancelling step run ${action.stepRunId}`);
615
+ this.logger.info(`Cancelling task run ${action.stepRunId}`);
609
616
  const future = this.futures[stepRunId];
610
617
  const context = this.contexts[stepRunId];
611
618
  if (context && context.abortController) {
@@ -613,14 +620,15 @@ class V1Worker {
613
620
  }
614
621
  if (future) {
615
622
  future.promise.catch(() => {
616
- this.logger.info(`Cancelled step run ${action.stepRunId}`);
623
+ this.logger.info(`Cancelled task run ${action.stepRunId}`);
617
624
  });
618
625
  future.cancel('Cancelled by worker');
619
626
  yield future.promise;
620
627
  }
621
628
  }
622
629
  catch (e) {
623
- this.logger.error('Could not cancel step run: ', e);
630
+ // Expected: the promise rejects when cancelled
631
+ this.logger.debug(`Task run ${stepRunId} cancellation completed`);
624
632
  }
625
633
  finally {
626
634
  delete this.futures[stepRunId];
@@ -266,14 +266,14 @@ export declare class BaseWorkflowDeclaration<I extends InputType = UnknownInputT
266
266
  * @returns A promise that resolves with the workflow metrics.
267
267
  * @throws Error if the workflow is not bound to a Hatchet client.
268
268
  */
269
- metrics(opts?: Parameters<MetricsClient['getWorkflowMetrics']>[1]): Promise<import("../clients/rest/generated/data-contracts").WorkflowMetrics>;
269
+ metrics(opts?: Omit<Parameters<MetricsClient['getTaskStatusMetrics']>[0], 'workflows'>): Promise<import("./client/features/metrics").TaskStatusMetrics>;
270
270
  /**
271
271
  * Get queue metrics for the workflow.
272
272
  * @param opts Optional configuration for the metrics request.
273
273
  * @returns A promise that resolves with the workflow metrics.
274
274
  * @throws Error if the workflow is not bound to a Hatchet client.
275
275
  */
276
- queueMetrics(opts?: Omit<Parameters<MetricsClient['getQueueMetrics']>[0], 'workflows'>): Promise<import("../clients/rest/generated/data-contracts").TenantQueueMetrics>;
276
+ taskStatusMetrics(opts?: Omit<Parameters<MetricsClient['getTaskStatusMetrics']>[0], 'workflows'>): Promise<import("./client/features/metrics").TaskStatusMetrics>;
277
277
  /**
278
278
  * Get the current state of the workflow.
279
279
  * @returns A promise that resolves with the workflow state.
package/v1/declaration.js CHANGED
@@ -166,10 +166,17 @@ class BaseWorkflowDeclaration {
166
166
  * @throws Error if the workflow is not bound to a Hatchet client.
167
167
  */
168
168
  metrics(opts) {
169
- if (!this.client) {
170
- throw UNBOUND_ERR;
171
- }
172
- return this.client.metrics.getWorkflowMetrics(this.definition.name, opts);
169
+ return __awaiter(this, void 0, void 0, function* () {
170
+ if (!this.client) {
171
+ throw UNBOUND_ERR;
172
+ }
173
+ const workflow = yield this.client.workflows.get(this.definition.name);
174
+ return this.client.metrics.getTaskStatusMetrics({
175
+ since: (opts === null || opts === void 0 ? void 0 : opts.since) || new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(),
176
+ until: (opts === null || opts === void 0 ? void 0 : opts.until) || new Date().toISOString(),
177
+ workflow_ids: [workflow.metadata.id],
178
+ });
179
+ });
173
180
  }
174
181
  /**
175
182
  * Get queue metrics for the workflow.
@@ -177,11 +184,18 @@ class BaseWorkflowDeclaration {
177
184
  * @returns A promise that resolves with the workflow metrics.
178
185
  * @throws Error if the workflow is not bound to a Hatchet client.
179
186
  */
180
- queueMetrics(opts) {
181
- if (!this.client) {
182
- throw UNBOUND_ERR;
183
- }
184
- return this.client.metrics.getQueueMetrics(Object.assign(Object.assign({}, opts), { workflows: [this.definition.name] }));
187
+ taskStatusMetrics(opts) {
188
+ return __awaiter(this, void 0, void 0, function* () {
189
+ if (!this.client) {
190
+ throw UNBOUND_ERR;
191
+ }
192
+ const workflow = yield this.client.workflows.get(this.definition.name);
193
+ return this.client.metrics.getTaskStatusMetrics({
194
+ since: (opts === null || opts === void 0 ? void 0 : opts.since) || new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(),
195
+ until: (opts === null || opts === void 0 ? void 0 : opts.until) || new Date().toISOString(),
196
+ workflow_ids: [workflow.metadata.id],
197
+ });
198
+ });
185
199
  }
186
200
  /**
187
201
  * Get the current state of the workflow.
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "1.10.7";
1
+ export declare const HATCHET_VERSION = "1.11.0";
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.10.7';
4
+ exports.HATCHET_VERSION = '1.11.0';