@io-orkes/conductor-javascript 1.0.1 → 1.1.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.
package/dist/index.mjs CHANGED
@@ -18,11 +18,7 @@ var __spreadValues = (a, b) => {
18
18
  };
19
19
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
20
 
21
- // src/task/TaskRunner.ts
22
- var DEFAULT_ERROR_MESSAGE = "An unknown error occurred";
23
- var MAX_RETRIES = 3;
24
- var noopErrorHandler = (__error) => {
25
- };
21
+ // src/task/helpers.ts
26
22
  var noopLogger = {
27
23
  debug: (...args) => {
28
24
  },
@@ -31,6 +27,88 @@ var noopLogger = {
31
27
  error: (...args) => {
32
28
  }
33
29
  };
30
+
31
+ // src/task/Poller.ts
32
+ var Poller = class {
33
+ constructor(pollFunction, pollerOptions, logger) {
34
+ this.concurrentCalls = [];
35
+ this.pollFunction = async () => {
36
+ };
37
+ this.isPolling = false;
38
+ this.options = {
39
+ pollInterval: 1e3,
40
+ concurrency: 1
41
+ };
42
+ this.logger = noopLogger;
43
+ this.startPolling = () => {
44
+ if (this.isPolling) {
45
+ throw new Error("Runner is already started");
46
+ }
47
+ return this.poll();
48
+ };
49
+ this.stopPolling = () => {
50
+ this.isPolling = false;
51
+ this.concurrentCalls.forEach((call) => call.stop());
52
+ };
53
+ this.poll = async () => {
54
+ if (!this.isPolling) {
55
+ this.isPolling = true;
56
+ for (let i = 0; i < this.options.concurrency; i++) {
57
+ this.concurrentCalls.push(this.singlePoll());
58
+ }
59
+ }
60
+ };
61
+ this.singlePoll = () => {
62
+ let poll = this.isPolling;
63
+ let timeout;
64
+ const pollingCall = async () => {
65
+ while (poll) {
66
+ await this.pollFunction();
67
+ await new Promise((r) => timeout = setTimeout(() => r(true), this.options.pollInterval));
68
+ }
69
+ };
70
+ return {
71
+ promise: pollingCall(),
72
+ stop: () => {
73
+ clearTimeout(timeout);
74
+ poll = false;
75
+ this.logger.debug("stopping single poll call");
76
+ }
77
+ };
78
+ };
79
+ this.pollFunction = pollFunction;
80
+ this.options = __spreadValues(__spreadValues({}, this.options), pollerOptions);
81
+ this.logger = logger || noopLogger;
82
+ }
83
+ updateConcurrency(concurrency) {
84
+ if (concurrency > 0 && concurrency !== this.options.concurrency) {
85
+ if (concurrency < this.options.concurrency) {
86
+ const result = this.concurrentCalls.splice(0, this.options.concurrency - concurrency);
87
+ result.forEach((call) => {
88
+ call.stop();
89
+ this.logger.debug("stopping some spawned calls");
90
+ });
91
+ } else {
92
+ for (let i = 0; i < concurrency - this.options.concurrency; i++) {
93
+ this.concurrentCalls.push(this.singlePoll());
94
+ this.logger.debug("spawning additional poll calls");
95
+ }
96
+ }
97
+ this.options.concurrency = concurrency;
98
+ }
99
+ }
100
+ updateOptions(options) {
101
+ const newOptions = __spreadValues(__spreadValues({}, this.options), options);
102
+ this.updateConcurrency(newOptions.concurrency);
103
+ this.options = newOptions;
104
+ }
105
+ };
106
+
107
+ // src/task/TaskRunner.ts
108
+ var DEFAULT_ERROR_MESSAGE = "An unknown error occurred";
109
+ var MAX_RETRIES = 3;
110
+ var noopErrorHandler = (__error) => {
111
+ };
34
112
  var TaskRunner = class {
35
113
  constructor({
36
114
  worker,
@@ -39,32 +117,24 @@ var TaskRunner = class {
39
117
  logger = noopLogger,
40
118
  onError: errorHandler = noopErrorHandler
41
119
  }) {
42
- this.isPolling = false;
43
120
  this.startPolling = () => {
44
- if (this.isPolling) {
45
- throw new Error("Runner is already started");
46
- }
47
- this.isPolling = true;
48
- return this.poll();
121
+ this.poller.startPolling();
49
122
  };
50
123
  this.stopPolling = () => {
51
- this.isPolling = false;
124
+ this.poller.stopPolling();
52
125
  };
53
- this.poll = async () => {
54
- while (this.isPolling) {
55
- try {
56
- const { workerID } = this.options;
57
- const task = await this.taskResource.poll(this.worker.taskDefName, workerID, this.options.domain);
58
- if (task && task.taskId) {
59
- await this.executeTask(task);
60
- } else {
61
- this.logger.debug(`No tasks for ${this.worker.taskDefName}`);
62
- }
63
- } catch (unknownError) {
64
- this.handleUnknownError(unknownError);
65
- this.errorHandler(unknownError);
126
+ this.pollAndExecute = async () => {
127
+ try {
128
+ const { workerID } = this.options;
129
+ const task = await this.taskResource.poll(this.worker.taskDefName, workerID, this.worker.domain ?? this.options.domain);
130
+ if (task && task.taskId) {
131
+ await this.executeTask(task);
132
+ } else {
133
+ this.logger.debug(`No tasks for ${this.worker.taskDefName}`);
66
134
  }
67
- await new Promise((r) => setTimeout(() => r(true), this.options.pollInterval));
135
+ } catch (unknownError) {
136
+ this.handleUnknownError(unknownError);
137
+ this.errorHandler(unknownError);
68
138
  }
69
139
  };
70
140
  this.updateTaskWithRetry = async (task, taskResult) => {
@@ -118,6 +188,18 @@ var TaskRunner = class {
118
188
  this.worker = worker;
119
189
  this.options = options;
120
190
  this.errorHandler = errorHandler;
191
+ this.poller = new Poller(this.pollAndExecute, { concurrency: options.concurrency, pollInterval: options.pollInterval }, this.logger);
192
+ }
193
+ updateOptions(options) {
194
+ const newOptions = __spreadValues(__spreadValues({}, this.options), options);
195
+ this.poller.updateOptions({
196
+ concurrency: newOptions.concurrency,
197
+ pollInterval: newOptions.pollInterval
198
+ });
199
+ this.options = newOptions;
200
+ }
201
+ get getOptions() {
202
+ return this.options;
121
203
  }
122
204
  };
123
205
 
@@ -2087,25 +2169,35 @@ function workerId(options) {
2087
2169
  var TaskManager = class {
2088
2170
  constructor(client, workers, config = {}) {
2089
2171
  this.tasks = {};
2172
+ this.workerManagerWorkerOptions = (worker) => {
2173
+ return __spreadProps(__spreadValues({}, this.taskManageOptions), {
2174
+ concurrency: worker.concurrency ?? this.taskManageOptions.concurrency,
2175
+ domain: worker.domain ?? this.taskManageOptions.domain
2176
+ });
2177
+ };
2178
+ this.updatePollingOptions = (options) => {
2179
+ this.workers.forEach((worker) => {
2180
+ const newOptions = __spreadValues(__spreadValues({}, this.workerManagerWorkerOptions(worker)), options);
2181
+ const runners = this.tasks[worker.taskDefName];
2182
+ runners.forEach((runner) => {
2183
+ runner.updateOptions(newOptions);
2184
+ });
2185
+ });
2186
+ };
2090
2187
  this.startPolling = () => {
2091
2188
  this.workers.forEach((worker) => {
2092
2189
  this.tasks[worker.taskDefName] = [];
2093
- const options = __spreadProps(__spreadValues({}, this.taskManageOptions), {
2094
- concurrency: worker.concurrency ?? this.taskManageOptions.concurrency,
2095
- domain: worker.domain ?? this.taskManageOptions.domain
2096
- });
2190
+ const options = this.workerManagerWorkerOptions(worker);
2097
2191
  this.logger.debug(`Starting taskDefName=${worker.taskDefName} concurrency=${options.concurrency} domain=${options.domain}`);
2098
- for (let i = 0; i < options.concurrency; i++) {
2099
- const runner = new TaskRunner({
2100
- worker,
2101
- options,
2102
- taskResource: this.client.taskResource,
2103
- logger: this.logger,
2104
- onError: this.errorHandler
2105
- });
2106
- runner.startPolling();
2107
- this.tasks[worker.taskDefName].push(runner);
2108
- }
2192
+ const runner = new TaskRunner({
2193
+ worker,
2194
+ options,
2195
+ taskResource: this.client.taskResource,
2196
+ logger: this.logger,
2197
+ onError: this.errorHandler
2198
+ });
2199
+ runner.startPolling();
2200
+ this.tasks[worker.taskDefName].push(runner);
2109
2201
  });
2110
2202
  };
2111
2203
  this.stopPolling = () => {