@io-orkes/conductor-javascript 1.2.0 → 1.2.1-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.mjs CHANGED
@@ -2188,11 +2188,11 @@ var BaseHttpRequest = class {
2188
2188
 
2189
2189
  // src/task/Poller.ts
2190
2190
  var Poller = class {
2191
- constructor(pollFunction, pollerOptions, logger) {
2192
- this.concurrentCalls = [];
2193
- this.pollFunction = async () => {
2191
+ constructor(pollFunction, performWorkFunction, pollerOptions, logger) {
2192
+ this.performWorkFunction = async () => {
2194
2193
  };
2195
2194
  this.polling = false;
2195
+ this._tasksInProcess = 0;
2196
2196
  this.options = {
2197
2197
  pollInterval: 1e3,
2198
2198
  concurrency: 1
@@ -2205,78 +2205,48 @@ var Poller = class {
2205
2205
  if (this.polling) {
2206
2206
  throw new Error("Runner is already started");
2207
2207
  }
2208
- return this.poll();
2208
+ this._tasksInProcess = 0;
2209
+ this.polling = true;
2210
+ this.poll();
2209
2211
  };
2210
2212
  /**
2211
2213
  * Stops Polling for work
2212
2214
  */
2213
2215
  this.stopPolling = async () => {
2214
- await Promise.all(this.concurrentCalls.map((call) => call.stop()));
2215
2216
  this.polling = false;
2217
+ clearTimeout(this.timeoutHandler);
2218
+ };
2219
+ this.performWork = async (work) => {
2220
+ await this.performWorkFunction(work);
2221
+ this._tasksInProcess--;
2216
2222
  };
2217
2223
  this.poll = async () => {
2218
- if (!this.polling) {
2219
- this.polling = true;
2220
- for (let i = 0; i < this.options.concurrency; i++) {
2221
- this.concurrentCalls.push(this.singlePoll());
2222
- }
2224
+ while (this.isPolling) {
2225
+ const count = this.options.concurrency > this._tasksInProcess ? this.options.concurrency - this._tasksInProcess : this._tasksInProcess;
2226
+ const taskResult = await this.pollFunction(count);
2227
+ this._tasksInProcess = taskResult.length;
2228
+ taskResult.forEach(this.performWork);
2229
+ await new Promise(
2230
+ (r) => this.isPolling ? this.timeoutHandler = setTimeout(
2231
+ () => r(true),
2232
+ this.options.pollInterval
2233
+ ) : r(true)
2234
+ );
2223
2235
  }
2224
2236
  };
2225
- this.singlePoll = () => {
2226
- let poll = this.polling;
2227
- let timeout;
2228
- const pollingCall = async () => {
2229
- while (poll) {
2230
- await this.pollFunction();
2231
- await new Promise(
2232
- (r) => poll ? timeout = setTimeout(() => r(true), this.options.pollInterval) : r(true)
2233
- );
2234
- }
2235
- };
2236
- return {
2237
- promise: pollingCall(),
2238
- stop: () => new Promise((r) => {
2239
- clearTimeout(timeout);
2240
- poll = false;
2241
- this.logger.debug("stopping single poll call");
2242
- r(true);
2243
- })
2244
- };
2245
- };
2246
2237
  this.pollFunction = pollFunction;
2238
+ this.performWorkFunction = performWorkFunction;
2247
2239
  this.options = { ...this.options, ...pollerOptions };
2248
2240
  this.logger = logger || noopLogger;
2249
2241
  }
2250
2242
  get isPolling() {
2251
2243
  return this.polling;
2252
2244
  }
2253
- /**
2254
- * adds or shuts down concurrent calls based on the concurrency setting
2255
- * @param concurrency
2256
- */
2257
- updateConcurrency(concurrency) {
2258
- if (concurrency > 0 && concurrency !== this.options.concurrency) {
2259
- if (concurrency < this.options.concurrency) {
2260
- const result = this.concurrentCalls.splice(
2261
- 0,
2262
- this.options.concurrency - concurrency
2263
- );
2264
- result.forEach((call) => {
2265
- call.stop();
2266
- this.logger.debug("stopping some spawned calls");
2267
- });
2268
- } else {
2269
- for (let i = 0; i < concurrency - this.options.concurrency; i++) {
2270
- this.concurrentCalls.push(this.singlePoll());
2271
- this.logger.debug("spawning additional poll calls");
2272
- }
2273
- }
2274
- this.options.concurrency = concurrency;
2275
- }
2245
+ get tasksInProcess() {
2246
+ return this._tasksInProcess;
2276
2247
  }
2277
2248
  updateOptions(options) {
2278
2249
  const newOptions = { ...this.options, ...options };
2279
- this.updateConcurrency(newOptions.concurrency);
2280
2250
  this.options = newOptions;
2281
2251
  }
2282
2252
  };
@@ -2306,23 +2276,15 @@ var TaskRunner = class {
2306
2276
  this.stopPolling = async () => {
2307
2277
  await this.poller.stopPolling();
2308
2278
  };
2309
- this.pollAndExecute = async () => {
2310
- try {
2311
- const { workerID } = this.options;
2312
- const task = await this.taskResource.poll(
2313
- this.worker.taskDefName,
2314
- workerID,
2315
- this.worker.domain ?? this.options.domain
2316
- );
2317
- if (task && task.taskId) {
2318
- await this.executeTask(task);
2319
- } else {
2320
- this.logger.debug(`No tasks for ${this.worker.taskDefName}`);
2321
- }
2322
- } catch (unknownError) {
2323
- this.handleUnknownError(unknownError);
2324
- this.errorHandler(unknownError);
2325
- }
2279
+ this.batchPoll = async (count) => {
2280
+ const { workerID } = this.options;
2281
+ const task = await this.taskResource.batchPoll(
2282
+ this.worker.taskDefName,
2283
+ workerID,
2284
+ this.worker.domain ?? this.options.domain,
2285
+ count
2286
+ );
2287
+ return task;
2326
2288
  };
2327
2289
  this.updateTaskWithRetry = async (task, taskResult) => {
2328
2290
  let retryCount = 0;
@@ -2384,7 +2346,8 @@ var TaskRunner = class {
2384
2346
  this.options = options;
2385
2347
  this.errorHandler = errorHandler;
2386
2348
  this.poller = new Poller(
2387
- this.pollAndExecute,
2349
+ this.batchPoll,
2350
+ this.executeTask,
2388
2351
  { concurrency: options.concurrency, pollInterval: options.pollInterval },
2389
2352
  this.logger
2390
2353
  );