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

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,14 +2188,17 @@ var BaseHttpRequest = class {
2188
2188
 
2189
2189
  // src/task/Poller.ts
2190
2190
  var Poller = class {
2191
- constructor(pollFunction, performWorkFunction, pollerOptions, logger) {
2191
+ constructor(pollerId, pollFunction, performWorkFunction, pollerOptions, logger) {
2192
2192
  this.performWorkFunction = async () => {
2193
2193
  };
2194
2194
  this.polling = false;
2195
2195
  this._tasksInProcess = 0;
2196
+ this._counterAtO = 0;
2197
+ this._pollerId = "";
2196
2198
  this.options = {
2197
- pollInterval: 1e3,
2198
- concurrency: 1
2199
+ pollInterval: 100,
2200
+ concurrency: 1,
2201
+ warnAtO: 100
2199
2202
  };
2200
2203
  this.logger = noopLogger;
2201
2204
  /**
@@ -2222,10 +2225,30 @@ var Poller = class {
2222
2225
  };
2223
2226
  this.poll = async () => {
2224
2227
  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);
2228
+ try {
2229
+ const count = Math.max(
2230
+ 0,
2231
+ this.options.concurrency - this._tasksInProcess
2232
+ );
2233
+ if (count == 0) {
2234
+ this.logger.debug(
2235
+ "Max in process reached, Will skip polling for " + this._pollerId
2236
+ );
2237
+ this._counterAtO++;
2238
+ if (this._counterAtO > (this.options.warnAtO ?? 100)) {
2239
+ this.logger.info(
2240
+ `Not polling anything because in process tasks is maxed as concurrency level. ${this._pollerId}`
2241
+ );
2242
+ }
2243
+ } else {
2244
+ this._counterAtO = 0;
2245
+ const tasksResult = await this.pollFunction(count);
2246
+ this._tasksInProcess = this._tasksInProcess + (tasksResult ?? []).length;
2247
+ tasksResult.forEach(this.performWork);
2248
+ }
2249
+ } catch (e) {
2250
+ this.logger.error(`Error polling for tasks: ${e.message}`, e);
2251
+ }
2229
2252
  await new Promise(
2230
2253
  (r) => this.isPolling ? this.timeoutHandler = setTimeout(
2231
2254
  () => r(true),
@@ -2234,6 +2257,7 @@ var Poller = class {
2234
2257
  );
2235
2258
  }
2236
2259
  };
2260
+ this._pollerId = pollerId;
2237
2261
  this.pollFunction = pollFunction;
2238
2262
  this.performWorkFunction = performWorkFunction;
2239
2263
  this.options = { ...this.options, ...pollerOptions };
@@ -2256,6 +2280,13 @@ var DEFAULT_ERROR_MESSAGE = "An unknown error occurred";
2256
2280
  var MAX_RETRIES = 3;
2257
2281
  var noopErrorHandler = (__error) => {
2258
2282
  };
2283
+ var defaultRunnerOptions = {
2284
+ workerID: "",
2285
+ pollInterval: 100,
2286
+ domain: void 0,
2287
+ concurrency: 1,
2288
+ batchPollingTimeout: 100
2289
+ };
2259
2290
  var TaskRunner = class {
2260
2291
  constructor({
2261
2292
  worker,
@@ -2269,6 +2300,9 @@ var TaskRunner = class {
2269
2300
  */
2270
2301
  this.startPolling = () => {
2271
2302
  this.poller.startPolling();
2303
+ this.logger.info(
2304
+ `TaskWorker ${this.worker.taskDefName} initialized with concurrency of ${this.poller.options.concurrency} and poll interval of ${this.poller.options.pollInterval}`
2305
+ );
2272
2306
  };
2273
2307
  /**
2274
2308
  * Stops Polling for work
@@ -2278,13 +2312,15 @@ var TaskRunner = class {
2278
2312
  };
2279
2313
  this.batchPoll = async (count) => {
2280
2314
  const { workerID } = this.options;
2281
- const task = await this.taskResource.batchPoll(
2315
+ const tasks = await this.taskResource.batchPoll(
2282
2316
  this.worker.taskDefName,
2283
2317
  workerID,
2284
2318
  this.worker.domain ?? this.options.domain,
2285
- count
2319
+ count,
2320
+ this.options.batchPollingTimeout ?? 100
2321
+ // default batch poll defined in the method
2286
2322
  );
2287
- return task;
2323
+ return tasks;
2288
2324
  };
2289
2325
  this.updateTaskWithRetry = async (task, taskResult) => {
2290
2326
  let retryCount = 0;
@@ -2343,12 +2379,16 @@ var TaskRunner = class {
2343
2379
  this.taskResource = taskResource;
2344
2380
  this.logger = logger;
2345
2381
  this.worker = worker;
2346
- this.options = options;
2382
+ this.options = { ...defaultRunnerOptions, ...options };
2347
2383
  this.errorHandler = errorHandler;
2348
2384
  this.poller = new Poller(
2385
+ worker.taskDefName,
2349
2386
  this.batchPoll,
2350
2387
  this.executeTask,
2351
- { concurrency: options.concurrency, pollInterval: options.pollInterval },
2388
+ {
2389
+ concurrency: worker.concurrency ?? options.concurrency,
2390
+ pollInterval: worker.pollInterval ?? options.pollInterval
2391
+ },
2352
2392
  this.logger
2353
2393
  );
2354
2394
  }
@@ -2361,6 +2401,9 @@ var TaskRunner = class {
2361
2401
  concurrency: newOptions.concurrency,
2362
2402
  pollInterval: newOptions.pollInterval
2363
2403
  });
2404
+ this.logger.info(
2405
+ `TaskWorker ${this.worker.taskDefName} configuration updated with concurrency of ${this.poller.options.concurrency} and poll interval of ${this.poller.options.pollInterval}`
2406
+ );
2364
2407
  this.options = newOptions;
2365
2408
  }
2366
2409
  get getOptions() {
@@ -2372,24 +2415,36 @@ var TaskRunner = class {
2372
2415
  import os from "os";
2373
2416
  var defaultManagerOptions = {
2374
2417
  workerID: "",
2375
- pollInterval: 1e3,
2418
+ pollInterval: 100,
2376
2419
  domain: void 0,
2377
- concurrency: 1
2420
+ concurrency: 1,
2421
+ batchPollingTimeout: 100
2378
2422
  };
2379
2423
  function workerId(options) {
2380
2424
  return options.workerID ?? os.hostname();
2381
2425
  }
2382
2426
  var TaskManager = class {
2383
2427
  constructor(client, workers, config = {}) {
2384
- this.tasks = {};
2428
+ this.workerRunners = /* @__PURE__ */ new Map();
2385
2429
  this.polling = false;
2386
2430
  this.workerManagerWorkerOptions = (worker) => {
2387
2431
  return {
2388
2432
  ...this.options,
2389
2433
  concurrency: worker.concurrency ?? this.options.concurrency,
2434
+ pollInterval: worker.pollInterval ?? this.options.pollInterval,
2390
2435
  domain: worker.domain ?? this.options.domain
2391
2436
  };
2392
2437
  };
2438
+ this.updatePollingOptionForWorker = (workerTaskDefName, options) => {
2439
+ const maybeRunner = this.workerRunners.get(workerTaskDefName);
2440
+ if (maybeRunner != null) {
2441
+ maybeRunner.updateOptions(options);
2442
+ } else {
2443
+ this.logger.info(
2444
+ `No runner found for worker with taskDefName: ${workerTaskDefName}`
2445
+ );
2446
+ }
2447
+ };
2393
2448
  /**
2394
2449
  * new options will get merged to existing options
2395
2450
  * @param options new options to update polling options
@@ -2400,24 +2455,30 @@ var TaskManager = class {
2400
2455
  ...this.workerManagerWorkerOptions(worker),
2401
2456
  ...options
2402
2457
  };
2403
- const runners = this.tasks[worker.taskDefName];
2404
- runners.forEach((runner) => {
2405
- runner.updateOptions(newOptions);
2406
- });
2458
+ this.updatePollingOptionForWorker(worker.taskDefName, newOptions);
2407
2459
  });
2408
2460
  this.options.concurrency = options.concurrency ?? this.options.concurrency;
2409
2461
  this.options.pollInterval = options.pollInterval ?? this.options.pollInterval;
2410
2462
  };
2463
+ this.sanityCheck = () => {
2464
+ if (this.workers.length === 0) {
2465
+ throw new Error("No workers supplied to TaskManager");
2466
+ }
2467
+ const workerIDs = /* @__PURE__ */ new Set();
2468
+ for (const item of this.workers) {
2469
+ if (workerIDs.has(item.taskDefName)) {
2470
+ throw new Error(`Duplicate worker taskDefName: ${item.taskDefName}`);
2471
+ }
2472
+ workerIDs.add(item.taskDefName);
2473
+ }
2474
+ };
2411
2475
  /**
2412
2476
  * Start polling for tasks
2413
2477
  */
2414
2478
  this.startPolling = () => {
2479
+ this.sanityCheck();
2415
2480
  this.workers.forEach((worker) => {
2416
- this.tasks[worker.taskDefName] = [];
2417
2481
  const options = this.workerManagerWorkerOptions(worker);
2418
- this.logger.debug(
2419
- `Starting taskDefName=${worker.taskDefName} concurrency=${options.concurrency} domain=${options.domain}`
2420
- );
2421
2482
  const runner = new TaskRunner({
2422
2483
  worker,
2423
2484
  options,
@@ -2426,7 +2487,7 @@ var TaskManager = class {
2426
2487
  onError: this.errorHandler
2427
2488
  });
2428
2489
  runner.startPolling();
2429
- this.tasks[worker.taskDefName].push(runner);
2490
+ this.workerRunners.set(worker.taskDefName, runner);
2430
2491
  });
2431
2492
  this.polling = true;
2432
2493
  };
@@ -2434,11 +2495,10 @@ var TaskManager = class {
2434
2495
  * Stops polling for tasks
2435
2496
  */
2436
2497
  this.stopPolling = async () => {
2437
- for (const taskType in this.tasks) {
2438
- await Promise.all(
2439
- this.tasks[taskType].map((runner) => runner.stopPolling())
2440
- );
2441
- this.tasks[taskType] = [];
2498
+ for (const [workerTaskDefName, runner] of this.workerRunners) {
2499
+ this.logger.debug(`Stopping taskDefName=${workerTaskDefName}`);
2500
+ await runner.stopPolling();
2501
+ this.workerRunners.delete(workerTaskDefName);
2442
2502
  }
2443
2503
  this.polling = false;
2444
2504
  };