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

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