@hatchet-dev/typescript-sdk 0.8.0-alpha.3 → 0.9.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.
@@ -2,8 +2,10 @@ import { Channel, ClientFactory } from 'nice-grpc';
2
2
  import { CreateWorkflowVersionOpts, RateLimitDuration, WorkflowServiceClient } from '../../protoc/workflows';
3
3
  import { ClientConfig } from '../hatchet-client/client-config';
4
4
  import { Logger } from '../../util/logger';
5
+ import WorkflowRunRef from '../../util/workflow-run-ref';
5
6
  import { Api } from '../rest';
6
- import { WebhookWorkerCreateRequest, WorkflowRunStatus, WorkflowRunStatusList } from '../rest/generated/data-contracts';
7
+ import { WorkflowRunStatus, WorkflowRunStatusList } from '../rest/generated/data-contracts';
8
+ import { ListenerClient } from '../listener/listener-client';
7
9
  type WorkflowMetricsQuery = {
8
10
  workflowId?: string;
9
11
  workflowName?: string;
@@ -32,7 +34,8 @@ export declare class AdminClient {
32
34
  api: Api;
33
35
  tenantId: string;
34
36
  logger: Logger;
35
- constructor(config: ClientConfig, channel: Channel, factory: ClientFactory, api: Api, tenantId: string);
37
+ listenerClient: ListenerClient;
38
+ constructor(config: ClientConfig, channel: Channel, factory: ClientFactory, api: Api, tenantId: string, listenerClient: ListenerClient);
36
39
  /**
37
40
  * @deprecated use putWorkflow instead
38
41
  */
@@ -48,7 +51,6 @@ export declare class AdminClient {
48
51
  */
49
52
  put_rate_limit(key: string, limit: number, duration: RateLimitDuration): Promise<void>;
50
53
  putRateLimit(key: string, limit: number, duration?: RateLimitDuration): Promise<void>;
51
- webhook_create(data: WebhookWorkerCreateRequest): Promise<import("axios").AxiosResponse<import("../rest/generated/data-contracts").WebhookWorker, any>>;
52
54
  /**
53
55
  * @deprecated use runWorkflow instead
54
56
  */
@@ -58,7 +60,7 @@ export declare class AdminClient {
58
60
  childIndex?: number | undefined;
59
61
  childKey?: string | undefined;
60
62
  additionalMetadata?: Record<string, string> | undefined;
61
- }): Promise<string>;
63
+ }): Promise<WorkflowRunRef<object>>;
62
64
  /**
63
65
  * Run a new instance of a workflow with the given input. This will create a new workflow run and return the ID of the
64
66
  * new run.
@@ -67,13 +69,13 @@ export declare class AdminClient {
67
69
  * @param options an object containing the options to run the workflow
68
70
  * @returns the ID of the new workflow run
69
71
  */
70
- runWorkflow<T = object>(workflowName: string, input: T, options?: {
72
+ runWorkflow<Q = object, P = object>(workflowName: string, input: Q, options?: {
71
73
  parentId?: string | undefined;
72
74
  parentStepRunId?: string | undefined;
73
75
  childIndex?: number | undefined;
74
76
  childKey?: string | undefined;
75
77
  additionalMetadata?: Record<string, string> | undefined;
76
- }): Promise<string>;
78
+ }): WorkflowRunRef<P>;
77
79
  /**
78
80
  * @deprecated use listWorkflows instead
79
81
  */
@@ -107,13 +109,13 @@ export declare class AdminClient {
107
109
  /**
108
110
  * @deprecated use getWorkflowRun instead
109
111
  */
110
- get_workflow_run(workflowRunId: string): Promise<import("../rest/generated/data-contracts").WorkflowRun>;
112
+ get_workflow_run(workflowRunId: string): Promise<WorkflowRunRef<unknown>>;
111
113
  /**
112
114
  * Get a workflow run.
113
115
  * @param workflowRunId the id of the workflow run to get
114
116
  * @returns the workflow run
115
117
  */
116
- getWorkflowRun(workflowRunId: string): Promise<import("../rest/generated/data-contracts").WorkflowRun>;
118
+ getWorkflowRun(workflowRunId: string): Promise<WorkflowRunRef<unknown>>;
117
119
  /**
118
120
  * @deprecated use listWorkflowRuns instead
119
121
  */
@@ -147,14 +149,17 @@ export declare class AdminClient {
147
149
  */
148
150
  schedule_workflow(name: string, options?: {
149
151
  schedules?: Date[];
152
+ input?: object;
150
153
  }): Promise<void>;
151
154
  /**
152
155
  * Schedule a workflow to run at a specific time or times.
153
156
  * @param name the name of the workflow to schedule
154
157
  * @param options an object containing the schedules to set
158
+ * @param input an object containing the input to the workflow
155
159
  */
156
160
  scheduleWorkflow(name: string, options?: {
157
161
  schedules?: Date[];
162
+ input?: object;
158
163
  }): void;
159
164
  /**
160
165
  * @deprecated use getWorkflowMetrics instead
@@ -17,6 +17,7 @@ const workflows_1 = require("../../protoc/workflows");
17
17
  const hatchet_error_1 = __importDefault(require("../../util/errors/hatchet-error"));
18
18
  const logger_1 = require("../../util/logger");
19
19
  const retrier_1 = require("../../util/retrier");
20
+ const workflow_run_ref_1 = __importDefault(require("../../util/workflow-run-ref"));
20
21
  /**
21
22
  * AdminClient is a client for interacting with the Hatchet Admin API. This allows you to configure, trigger,
22
23
  * and monitor workflows.
@@ -34,12 +35,13 @@ const retrier_1 = require("../../util/retrier");
34
35
  * ```
35
36
  */
36
37
  class AdminClient {
37
- constructor(config, channel, factory, api, tenantId) {
38
+ constructor(config, channel, factory, api, tenantId, listenerClient) {
38
39
  this.config = config;
39
40
  this.client = factory.create(workflows_1.WorkflowServiceDefinition, channel);
40
41
  this.api = api;
41
42
  this.tenantId = tenantId;
42
43
  this.logger = new logger_1.Logger(`Admin`, config.log_level);
44
+ this.listenerClient = listenerClient;
43
45
  }
44
46
  /**
45
47
  * @deprecated use putWorkflow instead
@@ -88,11 +90,6 @@ class AdminClient {
88
90
  }
89
91
  });
90
92
  }
91
- webhook_create(data) {
92
- return __awaiter(this, void 0, void 0, function* () {
93
- return this.api.webhookCreate(this.tenantId, data);
94
- });
95
- }
96
93
  /**
97
94
  * @deprecated use runWorkflow instead
98
95
  */
@@ -110,22 +107,20 @@ class AdminClient {
110
107
  * @returns the ID of the new workflow run
111
108
  */
112
109
  runWorkflow(workflowName, input, options) {
113
- return __awaiter(this, void 0, void 0, function* () {
114
- let computedName = workflowName;
115
- try {
116
- if (this.config.namespace && !workflowName.startsWith(this.config.namespace)) {
117
- computedName = this.config.namespace + workflowName;
118
- }
119
- const inputStr = JSON.stringify(input);
120
- const resp = yield this.client.triggerWorkflow(Object.assign(Object.assign({ name: computedName, input: inputStr }, options), { additionalMetadata: (options === null || options === void 0 ? void 0 : options.additionalMetadata)
121
- ? JSON.stringify(options === null || options === void 0 ? void 0 : options.additionalMetadata)
122
- : undefined }));
123
- return resp.workflowRunId;
124
- }
125
- catch (e) {
126
- throw new hatchet_error_1.default(e.message);
110
+ let computedName = workflowName;
111
+ try {
112
+ if (this.config.namespace && !workflowName.startsWith(this.config.namespace)) {
113
+ computedName = this.config.namespace + workflowName;
127
114
  }
128
- });
115
+ const inputStr = JSON.stringify(input);
116
+ const resp = this.client.triggerWorkflow(Object.assign(Object.assign({ name: computedName, input: inputStr }, options), { additionalMetadata: (options === null || options === void 0 ? void 0 : options.additionalMetadata)
117
+ ? JSON.stringify(options === null || options === void 0 ? void 0 : options.additionalMetadata)
118
+ : undefined }));
119
+ return new workflow_run_ref_1.default(resp, this.listenerClient, options === null || options === void 0 ? void 0 : options.parentId);
120
+ }
121
+ catch (e) {
122
+ throw new hatchet_error_1.default(e.message);
123
+ }
129
124
  }
130
125
  /**
131
126
  * @deprecated use listWorkflows instead
@@ -201,8 +196,7 @@ class AdminClient {
201
196
  */
202
197
  getWorkflowRun(workflowRunId) {
203
198
  return __awaiter(this, void 0, void 0, function* () {
204
- const res = yield this.api.workflowRunGet(this.tenantId, workflowRunId);
205
- return res.data;
199
+ return new workflow_run_ref_1.default(workflowRunId, this.listenerClient);
206
200
  });
207
201
  }
208
202
  /**
@@ -236,12 +230,18 @@ class AdminClient {
236
230
  * Schedule a workflow to run at a specific time or times.
237
231
  * @param name the name of the workflow to schedule
238
232
  * @param options an object containing the schedules to set
233
+ * @param input an object containing the input to the workflow
239
234
  */
240
235
  scheduleWorkflow(name, options) {
241
236
  try {
237
+ let input;
238
+ if (options === null || options === void 0 ? void 0 : options.input) {
239
+ input = JSON.stringify(options.input);
240
+ }
242
241
  this.client.scheduleWorkflow({
243
242
  name,
244
243
  schedules: options === null || options === void 0 ? void 0 : options.schedules,
244
+ input,
245
245
  });
246
246
  }
247
247
  catch (e) {
@@ -1,57 +1,14 @@
1
1
  import { DispatcherClient as PbDispatcherClient, AssignedAction } from '../../protoc/dispatcher';
2
2
  import { ClientConfig } from '../hatchet-client/client-config';
3
3
  import { Logger } from '../../util/logger';
4
- import { z } from 'zod';
5
4
  import { DispatcherClient } from './dispatcher-client';
6
5
  import { Heartbeat } from './heartbeat/heartbeat-controller';
7
6
  declare enum ListenStrategy {
8
7
  LISTEN_STRATEGY_V1 = 1,
9
8
  LISTEN_STRATEGY_V2 = 2
10
9
  }
11
- export declare const ActionObject: z.ZodObject<{
12
- tenantId: z.ZodString;
13
- jobId: z.ZodString;
14
- jobName: z.ZodString;
15
- jobRunId: z.ZodString;
16
- stepId: z.ZodString;
17
- stepRunId: z.ZodString;
18
- actionId: z.ZodString;
19
- actionType: z.ZodEffects<z.ZodOptional<z.ZodNumber>, number | undefined, unknown>;
20
- actionPayload: z.ZodString;
21
- workflowRunId: z.ZodString;
22
- getGroupKeyRunId: z.ZodOptional<z.ZodString>;
23
- stepName: z.ZodString;
24
- retryCount: z.ZodNumber;
25
- }, "strip", z.ZodTypeAny, {
26
- tenantId: string;
27
- stepRunId: string;
28
- workflowRunId: string;
29
- jobId: string;
30
- jobName: string;
31
- jobRunId: string;
32
- stepId: string;
33
- actionId: string;
34
- actionPayload: string;
35
- stepName: string;
36
- retryCount: number;
37
- getGroupKeyRunId?: string | undefined;
38
- actionType?: number | undefined;
39
- }, {
40
- tenantId: string;
41
- stepRunId: string;
42
- workflowRunId: string;
43
- jobId: string;
44
- jobName: string;
45
- jobRunId: string;
46
- stepId: string;
47
- actionId: string;
48
- actionPayload: string;
49
- stepName: string;
50
- retryCount: number;
51
- getGroupKeyRunId?: string | undefined;
52
- actionType?: unknown;
53
- }>;
54
- export type Action = z.infer<typeof ActionObject>;
10
+ export interface Action extends AssignedAction {
11
+ }
55
12
  export declare class ActionListener {
56
13
  config: ClientConfig;
57
14
  client: PbDispatcherClient;
@@ -65,21 +22,7 @@ export declare class ActionListener {
65
22
  listenStrategy: ListenStrategy;
66
23
  heartbeat: Heartbeat;
67
24
  constructor(client: DispatcherClient, workerId: string, retryInterval?: number, retryCount?: number);
68
- actions: () => AsyncGenerator<{
69
- tenantId: string;
70
- stepRunId: string;
71
- workflowRunId: string;
72
- jobId: string;
73
- jobName: string;
74
- jobRunId: string;
75
- stepId: string;
76
- actionId: string;
77
- actionPayload: string;
78
- stepName: string;
79
- retryCount: number;
80
- getGroupKeyRunId?: string | undefined;
81
- actionType?: number | undefined;
82
- }, void, unknown>;
25
+ actions: () => AsyncGenerator<Action, void, unknown>;
83
26
  setListenStrategy(strategy: ListenStrategy): Promise<void>;
84
27
  getListenStrategy(): Promise<ListenStrategy>;
85
28
  incrementRetries(): Promise<void>;
@@ -32,13 +32,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
32
32
  return (mod && mod.__esModule) ? mod : { "default": mod };
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.ActionListener = exports.ActionObject = void 0;
36
- const dispatcher_1 = require("../../protoc/dispatcher");
35
+ exports.ActionListener = void 0;
37
36
  const nice_grpc_1 = require("nice-grpc");
38
37
  const sleep_1 = __importDefault(require("../../util/sleep"));
39
38
  const hatchet_error_1 = __importDefault(require("../../util/errors/hatchet-error"));
40
39
  const logger_1 = require("../../util/logger");
41
- const zod_1 = require("zod");
42
40
  const heartbeat_controller_1 = require("./heartbeat/heartbeat-controller");
43
41
  const DEFAULT_ACTION_LISTENER_RETRY_INTERVAL = 5000; // milliseconds
44
42
  const DEFAULT_ACTION_LISTENER_RETRY_COUNT = 20;
@@ -48,21 +46,6 @@ var ListenStrategy;
48
46
  ListenStrategy[ListenStrategy["LISTEN_STRATEGY_V1"] = 1] = "LISTEN_STRATEGY_V1";
49
47
  ListenStrategy[ListenStrategy["LISTEN_STRATEGY_V2"] = 2] = "LISTEN_STRATEGY_V2";
50
48
  })(ListenStrategy || (ListenStrategy = {}));
51
- exports.ActionObject = zod_1.z.object({
52
- tenantId: zod_1.z.string(),
53
- jobId: zod_1.z.string(),
54
- jobName: zod_1.z.string(),
55
- jobRunId: zod_1.z.string(),
56
- stepId: zod_1.z.string(),
57
- stepRunId: zod_1.z.string(),
58
- actionId: zod_1.z.string(),
59
- actionType: zod_1.z.preprocess((s) => (0, dispatcher_1.actionTypeFromJSON)(s), zod_1.z.number().optional()),
60
- actionPayload: zod_1.z.string(),
61
- workflowRunId: zod_1.z.string(),
62
- getGroupKeyRunId: zod_1.z.string().optional(),
63
- stepName: zod_1.z.string(),
64
- retryCount: zod_1.z.number(),
65
- });
66
49
  class ActionListener {
67
50
  constructor(client, workerId, retryInterval = DEFAULT_ACTION_LISTENER_RETRY_INTERVAL, retryCount = DEFAULT_ACTION_LISTENER_RETRY_COUNT) {
68
51
  this.lastConnectionAttempt = 0;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Logger } from '../../../util/logger';
3
2
  import { DispatcherClient as PbDispatcherClient } from '../../../protoc/dispatcher';
4
3
  import { Worker } from 'worker_threads';
@@ -29,5 +29,4 @@ export declare class HatchetClient {
29
29
  static init(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosConfig?: AxiosRequestConfig): HatchetClient;
30
30
  run(workflow: string | Workflow): Promise<Worker>;
31
31
  worker(workflow: string | Workflow, maxRuns?: number): Promise<Worker>;
32
- webhooks(workflows: Workflow[]): import("../worker/handler").WebhookHandler;
33
32
  }
@@ -111,8 +111,8 @@ class HatchetClient {
111
111
  this.api = (0, rest_1.default)(this.config.api_url, this.config.token, axiosOpts);
112
112
  this.event = new event_client_1.EventClient(this.config, (0, exports.channelFactory)(this.config, this.credentials), clientFactory);
113
113
  this.dispatcher = new dispatcher_client_1.DispatcherClient(this.config, (0, exports.channelFactory)(this.config, this.credentials), clientFactory);
114
- this.admin = new admin_client_1.AdminClient(this.config, (0, exports.channelFactory)(this.config, this.credentials), clientFactory, this.api, this.tenantId);
115
114
  this.listener = new listener_client_1.ListenerClient(this.config, (0, exports.channelFactory)(this.config, this.credentials), clientFactory, this.api);
115
+ this.admin = new admin_client_1.AdminClient(this.config, (0, exports.channelFactory)(this.config, this.credentials), clientFactory, this.api, this.tenantId, this.listener);
116
116
  this.logger = new logger_1.default('HatchetClient', this.config.log_level);
117
117
  this.logger.info(`Initialized HatchetClient`);
118
118
  }
@@ -142,11 +142,5 @@ class HatchetClient {
142
142
  return worker;
143
143
  });
144
144
  }
145
- webhooks(workflows) {
146
- const worker = new worker_1.Worker(this, {
147
- name: 'webhook-worker',
148
- });
149
- return worker.getHandler(workflows);
150
- }
151
145
  }
152
146
  exports.HatchetClient = HatchetClient;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { EventEmitter } from 'events';
3
2
  import { WorkflowRunEvent, SubscribeToWorkflowRunsRequest } from '../../protoc/dispatcher';
4
3
  import { ListenerClient } from './listener-client';
@@ -1,7 +1,6 @@
1
- /// <reference types="node" />
2
1
  import { Channel, ClientFactory } from 'nice-grpc';
3
2
  import { EventEmitter } from 'events';
4
- import { DispatcherClient as PbDispatcherClient } from '../../protoc/dispatcher';
3
+ import { DispatcherClient as PbDispatcherClient, DispatcherClient } from '../../protoc/dispatcher';
5
4
  import { ClientConfig } from '../hatchet-client/client-config';
6
5
  import { Logger } from '../../util/logger';
7
6
  import { Api } from '../rest';
@@ -23,21 +22,15 @@ export interface StepRunEvent {
23
22
  type: RunEventType;
24
23
  payload: string;
25
24
  }
26
- export declare class PollingAsyncListener {
27
- client: ListenerClient;
25
+ export declare class RunEventListener {
26
+ client: DispatcherClient;
28
27
  q: Array<StepRunEvent>;
29
28
  eventEmitter: EventEmitter<[never]>;
30
29
  pollInterval: any;
31
- constructor(workflowRunid: string, client: ListenerClient);
30
+ constructor(workflowRunid: string, client: DispatcherClient);
32
31
  emit(event: StepRunEvent): void;
33
32
  listen(workflowRunId: string): Promise<void>;
34
33
  retrySubscribe(workflowRunId: string): Promise<AsyncIterable<import("../../protoc/dispatcher").WorkflowEvent>>;
35
- getWorkflowRun(workflowRunId: string): Promise<{
36
- type: RunEventType;
37
- payload: string;
38
- } | undefined>;
39
- polling(workflowRunId: string): Promise<void>;
40
- close(): void;
41
34
  stream(): AsyncGenerator<StepRunEvent, void, unknown>;
42
35
  }
43
36
  export declare class ListenerClient {
@@ -45,9 +38,8 @@ export declare class ListenerClient {
45
38
  client: PbDispatcherClient;
46
39
  logger: Logger;
47
40
  api: Api;
48
- childListeners: Record<string, GrpcPooledListener>;
41
+ pooledListener: GrpcPooledListener | undefined;
49
42
  constructor(config: ClientConfig, channel: Channel, factory: ClientFactory, api: Api);
50
- getChildListener(workflowRunId: string, parentWorkflowRunId: string): import("./child-listener-client").Streamable;
51
- get(workflowRunId: string): PollingAsyncListener;
43
+ get(workflowRunId: string): import("./child-listener-client").Streamable;
52
44
  stream(workflowRunId: string): Promise<AsyncGenerator<StepRunEvent, void, unknown>>;
53
45
  }
@@ -32,7 +32,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
32
32
  return (mod && mod.__esModule) ? mod : { "default": mod };
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.ListenerClient = exports.PollingAsyncListener = exports.RunEventType = void 0;
35
+ exports.ListenerClient = exports.RunEventListener = exports.RunEventType = void 0;
36
36
  // eslint-disable-next-line max-classes-per-file
37
37
  const nice_grpc_1 = require("nice-grpc");
38
38
  const events_1 = require("events");
@@ -94,13 +94,12 @@ const workflowStatusMap = {
94
94
  [data_contracts_1.WorkflowRunStatus.RUNNING]: undefined,
95
95
  [data_contracts_1.WorkflowRunStatus.QUEUED]: undefined,
96
96
  };
97
- class PollingAsyncListener {
97
+ class RunEventListener {
98
98
  constructor(workflowRunid, client) {
99
99
  this.q = [];
100
100
  this.eventEmitter = new events_1.EventEmitter();
101
101
  this.client = client;
102
102
  this.listen(workflowRunid);
103
- this.polling(workflowRunid);
104
103
  }
105
104
  emit(event) {
106
105
  this.q.push(event);
@@ -110,14 +109,9 @@ class PollingAsyncListener {
110
109
  return __awaiter(this, void 0, void 0, function* () {
111
110
  var _a, e_1, _b, _c;
112
111
  var _d;
113
- let listener = this.client.client.subscribeToWorkflowEvents({
112
+ let listener = this.client.subscribeToWorkflowEvents({
114
113
  workflowRunId,
115
114
  });
116
- const res = yield this.getWorkflowRun(workflowRunId);
117
- if (res) {
118
- this.emit(res);
119
- this.close();
120
- }
121
115
  try {
122
116
  try {
123
117
  for (var _e = true, listener_1 = __asyncValues(listener), listener_1_1; listener_1_1 = yield listener_1.next(), _a = listener_1_1.done, !_a; _e = true) {
@@ -126,19 +120,10 @@ class PollingAsyncListener {
126
120
  const workflowEvent = _c;
127
121
  const eventType = (_d = resourceTypeMap[workflowEvent.resourceType]) === null || _d === void 0 ? void 0 : _d[workflowEvent.eventType];
128
122
  if (eventType) {
129
- if (eventType === RunEventType.WORKFLOW_RUN_EVENT_TYPE_COMPLETED) {
130
- // OPTIMZATION - consider including the workflow run data in the event?
131
- const data = yield this.getWorkflowRun(workflowRunId);
132
- if (data) {
133
- this.emit(data);
134
- }
135
- }
136
- else {
137
- this.emit({
138
- type: eventType,
139
- payload: workflowEvent.eventPayload,
140
- });
141
- }
123
+ this.emit({
124
+ type: eventType,
125
+ payload: workflowEvent.eventPayload,
126
+ });
142
127
  }
143
128
  }
144
129
  }
@@ -158,7 +143,6 @@ class PollingAsyncListener {
158
143
  listener = yield this.retrySubscribe(workflowRunId);
159
144
  }
160
145
  }
161
- setTimeout(() => this.close(), DEFAULT_EVENT_LISTENER_POLL_INTERVAL * 5);
162
146
  });
163
147
  }
164
148
  retrySubscribe(workflowRunId) {
@@ -167,7 +151,7 @@ class PollingAsyncListener {
167
151
  while (retries < DEFAULT_EVENT_LISTENER_RETRY_COUNT) {
168
152
  try {
169
153
  yield (0, sleep_1.default)(DEFAULT_EVENT_LISTENER_RETRY_INTERVAL);
170
- const listener = this.client.client.subscribeToWorkflowEvents({
154
+ const listener = this.client.subscribeToWorkflowEvents({
171
155
  workflowRunId,
172
156
  });
173
157
  return listener;
@@ -179,54 +163,6 @@ class PollingAsyncListener {
179
163
  throw new hatchet_error_1.default(`Could not subscribe to the worker after ${DEFAULT_EVENT_LISTENER_RETRY_COUNT} retries`);
180
164
  });
181
165
  }
182
- getWorkflowRun(workflowRunId) {
183
- return __awaiter(this, void 0, void 0, function* () {
184
- var _a, _b, _c;
185
- try {
186
- const res = yield this.client.api.workflowRunGet(this.client.config.tenant_id, workflowRunId);
187
- const stepRuns = (_c = (_b = (_a = res.data.jobRuns) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.stepRuns) !== null && _c !== void 0 ? _c : [];
188
- const stepRunOutput = stepRuns.reduce((acc, stepRun) => {
189
- var _a;
190
- acc[((_a = stepRun.step) === null || _a === void 0 ? void 0 : _a.readableId) || ''] = JSON.parse(stepRun.output || '{}');
191
- return acc;
192
- }, {});
193
- if (Object.keys(workflowStatusMap).includes(res.data.status)) {
194
- const type = workflowStatusMap[res.data.status];
195
- if (!type)
196
- return undefined;
197
- return {
198
- type,
199
- payload: JSON.stringify(stepRunOutput),
200
- };
201
- }
202
- return undefined;
203
- }
204
- catch (e) {
205
- throw new hatchet_error_1.default(e.message);
206
- }
207
- });
208
- }
209
- polling(workflowRunId) {
210
- return __awaiter(this, void 0, void 0, function* () {
211
- this.pollInterval = setInterval(() => __awaiter(this, void 0, void 0, function* () {
212
- try {
213
- const result = yield this.getWorkflowRun(workflowRunId);
214
- if (result) {
215
- this.emit(result);
216
- this.close();
217
- }
218
- }
219
- catch (e) {
220
- // TODO error handling
221
- }
222
- }), DEFAULT_EVENT_LISTENER_POLL_INTERVAL);
223
- });
224
- }
225
- close() {
226
- if (this.pollInterval) {
227
- clearInterval(this.pollInterval);
228
- }
229
- }
230
166
  stream() {
231
167
  return __asyncGenerator(this, arguments, function* stream_1() {
232
168
  var _a, e_2, _b, _c;
@@ -253,33 +189,28 @@ class PollingAsyncListener {
253
189
  });
254
190
  }
255
191
  }
256
- exports.PollingAsyncListener = PollingAsyncListener;
192
+ exports.RunEventListener = RunEventListener;
257
193
  class ListenerClient {
258
194
  constructor(config, channel, factory, api) {
259
- this.childListeners = {};
260
195
  this.config = config;
261
196
  this.client = factory.create(dispatcher_1.DispatcherDefinition, channel);
262
197
  this.logger = new logger_1.Logger(`Listener`, config.log_level);
263
198
  this.api = api;
264
199
  }
265
- getChildListener(workflowRunId, parentWorkflowRunId) {
266
- if (!this.childListeners[parentWorkflowRunId]) {
267
- this.childListeners[parentWorkflowRunId] = new child_listener_client_1.GrpcPooledListener(this, () => {
268
- // cleanup listener when all children are done
269
- delete this.childListeners[parentWorkflowRunId];
200
+ get(workflowRunId) {
201
+ if (!this.pooledListener) {
202
+ this.pooledListener = new child_listener_client_1.GrpcPooledListener(this, () => {
203
+ this.pooledListener = undefined;
270
204
  });
271
205
  }
272
- return this.childListeners[parentWorkflowRunId].subscribe({
206
+ return this.pooledListener.subscribe({
273
207
  workflowRunId,
274
208
  });
275
209
  }
276
- get(workflowRunId) {
277
- const listener = new PollingAsyncListener(workflowRunId, this);
278
- return listener;
279
- }
280
210
  stream(workflowRunId) {
281
211
  return __awaiter(this, void 0, void 0, function* () {
282
- return this.get(workflowRunId).stream();
212
+ const listener = new RunEventListener(workflowRunId, this.client);
213
+ return listener.stream();
283
214
  });
284
215
  }
285
216
  }
@@ -1,4 +1,4 @@
1
- import { APIMeta, AcceptInviteRequest, CreateAPITokenRequest, CreateAPITokenResponse, CreatePullRequestFromStepRun, CreateSNSIntegrationRequest, CreateTenantAlertEmailGroupRequest, CreateTenantInviteRequest, CreateTenantRequest, EventData, EventKey, EventKeyList, EventList, EventOrderByDirection, EventOrderByField, EventSearch, GetStepRunDiffResponse, LinkGithubRepositoryRequest, ListAPIMetaIntegration, ListAPITokensResponse, ListGithubAppInstallationsResponse, ListGithubBranchesResponse, ListGithubReposResponse, ListPullRequestsResponse, ListSNSIntegrations, ListSlackWebhooks, LogLineLevelField, LogLineList, LogLineOrderByDirection, LogLineOrderByField, LogLineSearch, PullRequestState, RejectInviteRequest, ReplayEventRequest, RerunStepRunRequest, SNSIntegration, StepRun, StepRunEventList, Tenant, TenantAlertEmailGroup, TenantAlertEmailGroupList, TenantAlertingSettings, TenantInvite, TenantInviteList, TenantMember, TenantMemberList, TenantResourcePolicy, TriggerWorkflowRunRequest, UpdateTenantAlertEmailGroupRequest, UpdateTenantInviteRequest, UpdateTenantRequest, User, UserChangePasswordRequest, UserLoginRequest, UserRegisterRequest, UserTenantMembershipsList, WebhookWorker, WebhookWorkerCreateRequest, WebhookWorkerListResponse, Worker, WorkerList, Workflow, WorkflowID, WorkflowList, WorkflowMetrics, WorkflowRun, WorkflowRunList, WorkflowRunStatus, WorkflowRunStatusList, WorkflowRunsCancelRequest, WorkflowRunsMetrics, WorkflowVersion, WorkflowVersionDefinition } from './data-contracts';
1
+ import { APIMeta, AcceptInviteRequest, CreateAPITokenRequest, CreateAPITokenResponse, CreatePullRequestFromStepRun, CreateSNSIntegrationRequest, CreateTenantAlertEmailGroupRequest, CreateTenantInviteRequest, CreateTenantRequest, EventData, EventKey, EventKeyList, EventList, EventOrderByDirection, EventOrderByField, EventSearch, GetStepRunDiffResponse, LinkGithubRepositoryRequest, ListAPIMetaIntegration, ListAPITokensResponse, ListGithubAppInstallationsResponse, ListGithubBranchesResponse, ListGithubReposResponse, ListPullRequestsResponse, ListSNSIntegrations, ListSlackWebhooks, LogLineLevelField, LogLineList, LogLineOrderByDirection, LogLineOrderByField, LogLineSearch, PullRequestState, RejectInviteRequest, ReplayEventRequest, RerunStepRunRequest, SNSIntegration, StepRun, StepRunEventList, Tenant, TenantAlertEmailGroup, TenantAlertEmailGroupList, TenantAlertingSettings, TenantInvite, TenantInviteList, TenantMember, TenantMemberList, TenantResourcePolicy, TriggerWorkflowRunRequest, UpdateTenantAlertEmailGroupRequest, UpdateTenantInviteRequest, UpdateTenantRequest, User, UserChangePasswordRequest, UserLoginRequest, UserRegisterRequest, UserTenantMembershipsList, Worker, WorkerList, Workflow, WorkflowID, WorkflowList, WorkflowMetrics, WorkflowRun, WorkflowRunList, WorkflowRunStatus, WorkflowRunStatusList, WorkflowRunsCancelRequest, WorkflowRunsMetrics, WorkflowVersion, WorkflowVersionDefinition } from './data-contracts';
2
2
  import { HttpClient, RequestParams } from './http-client';
3
3
  export declare class Api<SecurityDataType = unknown> extends HttpClient<SecurityDataType> {
4
4
  /**
@@ -535,7 +535,7 @@ export declare class Api<SecurityDataType = unknown> extends HttpClient<Security
535
535
  * @secure
536
536
  */
537
537
  workflowRunCancel: (tenant: string, data: WorkflowRunsCancelRequest, params?: RequestParams) => Promise<import("axios").AxiosResponse<{
538
- workflowRunIds?: string[] | undefined;
538
+ workflowRunIds?: string[];
539
539
  }, any>>;
540
540
  /**
541
541
  * @description Get a workflow for a tenant
@@ -919,21 +919,4 @@ export declare class Api<SecurityDataType = unknown> extends HttpClient<Security
919
919
  * @secure
920
920
  */
921
921
  githubAppListBranches: (ghInstallation: string, ghRepoOwner: string, ghRepoName: string, params?: RequestParams) => Promise<import("axios").AxiosResponse<ListGithubBranchesResponse, any>>;
922
- /**
923
- * @description Lists all webhooks
924
- *
925
- * @name WebhookList
926
- * @summary List webhooks
927
- * @request GET:/api/v1/webhook-workers/{tenant}
928
- * @secure
929
- */
930
- webhookList: (tenant: string, params?: RequestParams) => Promise<import("axios").AxiosResponse<WebhookWorkerListResponse, any>>;
931
- /**
932
- * @description Creates a webhook
933
- *
934
- * @name WebhookCreate
935
- * @summary Create a webhook
936
- * @request POST:/api/v1/webhook-workers/{tenant}/create
937
- */
938
- webhookCreate: (tenant: string, data: WebhookWorkerCreateRequest, params?: RequestParams) => Promise<import("axios").AxiosResponse<WebhookWorker, any>>;
939
922
  }
@@ -761,23 +761,6 @@ class Api extends http_client_1.HttpClient {
761
761
  * @secure
762
762
  */
763
763
  this.githubAppListBranches = (ghInstallation, ghRepoOwner, ghRepoName, params = {}) => this.request(Object.assign({ path: `/api/v1/github-app/installations/${ghInstallation}/repos/${ghRepoOwner}/${ghRepoName}/branches`, method: 'GET', secure: true, format: 'json' }, params));
764
- /**
765
- * @description Lists all webhooks
766
- *
767
- * @name WebhookList
768
- * @summary List webhooks
769
- * @request GET:/api/v1/webhook-workers/{tenant}
770
- * @secure
771
- */
772
- this.webhookList = (tenant, params = {}) => this.request(Object.assign({ path: `/api/v1/webhook-workers/${tenant}`, method: 'GET', secure: true, format: 'json' }, params));
773
- /**
774
- * @description Creates a webhook
775
- *
776
- * @name WebhookCreate
777
- * @summary Create a webhook
778
- * @request POST:/api/v1/webhook-workers/{tenant}/create
779
- */
780
- this.webhookCreate = (tenant, data, params = {}) => this.request(Object.assign({ path: `/api/v1/webhook-workers/${tenant}/create`, method: 'POST', body: data, type: http_client_1.ContentType.Json, format: 'json' }, params));
781
764
  }
782
765
  }
783
766
  exports.Api = Api;
@@ -894,28 +894,3 @@ export interface WorkflowMetrics {
894
894
  /** The total number of concurrency group keys. */
895
895
  groupKeyCount?: number;
896
896
  }
897
- export interface WebhookWorker {
898
- metadata: APIResourceMeta;
899
- /** The webhook url. */
900
- url: string;
901
- /** The secret key for validation. */
902
- secret: string;
903
- }
904
- export interface WebhookWorkerCreateRequest {
905
- /** The webhook url. */
906
- url: string;
907
- /** The workflow IDs or names to register for this webhook worker. If not provided, workflows will be automatically detected. */
908
- workflows?: string[];
909
- /**
910
- * The secret key for validation. If not provided, a random secret will be generated.
911
- * @minLength 32
912
- */
913
- secret?: string;
914
- }
915
- export interface WebhookWorkerCreateResponse {
916
- worker?: WebhookWorker;
917
- }
918
- export interface WebhookWorkerListResponse {
919
- pagination?: PaginationResponse;
920
- rows?: WebhookWorker[];
921
- }