poolifier 3.0.14 → 3.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/lib/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  /// <reference types="node" />
2
- import { TransferListItem, MessagePort, MessageChannel, Worker as Worker$1, WorkerOptions as WorkerOptions$1 } from 'node:worker_threads';
2
+ import { TransferListItem, MessagePort, MessageChannel, Worker as Worker$1 } from 'node:worker_threads';
3
3
  import { EventEmitter, EventEmitterAsyncResource } from 'node:events';
4
4
  import { EventLoopUtilization } from 'node:perf_hooks';
5
5
  import { AsyncResource } from 'node:async_hooks';
6
- import { Worker, ClusterSettings } from 'node:cluster';
6
+ import { ClusterSettings, Worker } from 'node:cluster';
7
7
 
8
8
  /**
9
9
  * Enumeration of kill behaviors.
@@ -29,7 +29,7 @@ type KillHandler = () => void | Promise<void>;
29
29
  /**
30
30
  * Options for workers.
31
31
  */
32
- interface WorkerOptions {
32
+ interface WorkerOptions$1 {
33
33
  /**
34
34
  * `killBehavior` dictates if your worker will be deleted in case a task is active on it.
35
35
  *
@@ -480,33 +480,50 @@ interface StrategyData {
480
480
  */
481
481
  interface IWorker {
482
482
  /**
483
- * Worker id.
483
+ * Cluster worker id.
484
484
  */
485
485
  readonly id?: number;
486
+ /**
487
+ * Worker thread worker id.
488
+ */
486
489
  readonly threadId?: number;
487
490
  /**
488
- * Registers an event listener.
491
+ * Registers an event handler.
489
492
  *
490
493
  * @param event - The event.
491
494
  * @param handler - The event handler.
492
495
  */
493
- readonly on: ((event: 'online', handler: OnlineHandler<this>) => void) & ((event: 'message', handler: MessageHandler<this>) => void) & ((event: 'error', handler: ErrorHandler<this>) => void) & ((event: 'exit', handler: ExitHandler<this>) => void);
496
+ readonly on: (event: string, handler: OnlineHandler<this> | MessageHandler<this> | ErrorHandler<this> | ExitHandler<this>) => void;
494
497
  /**
495
- * Registers a listener to the exit event that will only be performed once.
498
+ * Registers once an event handler.
496
499
  *
497
- * @param event - The `'exit'` event.
498
- * @param handler - The exit handler.
500
+ * @param event - The event.
501
+ * @param handler - The event handler.
502
+ */
503
+ readonly once: (event: string, handler: OnlineHandler<this> | MessageHandler<this> | ErrorHandler<this> | ExitHandler<this>) => void;
504
+ /**
505
+ * Stop all JavaScript execution in the worker thread as soon as possible.
506
+ * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted.
507
+ */
508
+ readonly terminate?: () => Promise<number>;
509
+ /**
510
+ * Cluster worker disconnect.
499
511
  */
500
- readonly once: (event: 'exit', handler: ExitHandler<this>) => void;
512
+ readonly disconnect?: () => void;
513
+ /**
514
+ * Cluster worker kill.
515
+ */
516
+ readonly kill?: (signal?: string) => void;
501
517
  }
502
518
  /**
503
- * Worker node event detail.
519
+ * Worker node options.
504
520
  *
505
521
  * @internal
506
522
  */
507
- interface WorkerNodeEventDetail {
508
- workerId: number;
509
- workerNodeKey?: number;
523
+ interface WorkerNodeOptions {
524
+ workerOptions?: WorkerOptions;
525
+ env?: Record<string, unknown>;
526
+ tasksQueueBackPressureSize: number;
510
527
  }
511
528
  /**
512
529
  * Worker node interface.
@@ -534,7 +551,7 @@ interface IWorkerNode<Worker extends IWorker, Data = unknown> extends EventEmitt
534
551
  */
535
552
  strategyData?: StrategyData;
536
553
  /**
537
- * Message channel (worker_threads only).
554
+ * Message channel (worker thread only).
538
555
  */
539
556
  readonly messageChannel?: MessageChannel;
540
557
  /**
@@ -589,9 +606,23 @@ interface IWorkerNode<Worker extends IWorker, Data = unknown> extends EventEmitt
589
606
  */
590
607
  readonly resetUsage: () => void;
591
608
  /**
592
- * Closes communication channel.
609
+ * Terminates the worker node.
610
+ */
611
+ readonly terminate: () => Promise<void>;
612
+ /**
613
+ * Registers a worker event handler.
614
+ *
615
+ * @param event - The event.
616
+ * @param handler - The event handler.
617
+ */
618
+ readonly registerWorkerEventHandler: (event: string, handler: OnlineHandler<Worker> | MessageHandler<Worker> | ErrorHandler<Worker> | ExitHandler<Worker>) => void;
619
+ /**
620
+ * Registers once a worker event handler.
621
+ *
622
+ * @param event - The event.
623
+ * @param handler - The event handler.
593
624
  */
594
- readonly closeChannel: () => void;
625
+ readonly registerOnceWorkerEventHandler: (event: string, handler: OnlineHandler<Worker> | MessageHandler<Worker> | ErrorHandler<Worker> | ExitHandler<Worker>) => void;
595
626
  /**
596
627
  * Gets task function worker usage statistics.
597
628
  *
@@ -607,6 +638,15 @@ interface IWorkerNode<Worker extends IWorker, Data = unknown> extends EventEmitt
607
638
  */
608
639
  readonly deleteTaskFunctionWorkerUsage: (name: string) => boolean;
609
640
  }
641
+ /**
642
+ * Worker node event detail.
643
+ *
644
+ * @internal
645
+ */
646
+ interface WorkerNodeEventDetail {
647
+ workerId: number;
648
+ workerNodeKey?: number;
649
+ }
610
650
 
611
651
  /**
612
652
  * Enumeration of worker choice strategies.
@@ -985,6 +1025,24 @@ interface PoolOptions<Worker extends IWorker> {
985
1025
  * Pool worker node tasks queue options.
986
1026
  */
987
1027
  tasksQueueOptions?: TasksQueueOptions;
1028
+ /**
1029
+ * Worker options.
1030
+ *
1031
+ * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
1032
+ */
1033
+ workerOptions?: WorkerOptions;
1034
+ /**
1035
+ * Key/value pairs to add to worker process environment.
1036
+ *
1037
+ * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
1038
+ */
1039
+ env?: Record<string, unknown>;
1040
+ /**
1041
+ * Cluster settings.
1042
+ *
1043
+ * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
1044
+ */
1045
+ settings?: ClusterSettings;
988
1046
  }
989
1047
  /**
990
1048
  * Contract definition for a poolifier pool.
@@ -1328,6 +1386,7 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1328
1386
  * @returns Worker nodes busyness boolean status.
1329
1387
  */
1330
1388
  protected internalBusy(): boolean;
1389
+ private isWorkerNodeBusy;
1331
1390
  private sendTaskFunctionOperationToWorker;
1332
1391
  private sendTaskFunctionOperationToWorkers;
1333
1392
  /** @inheritDoc */
@@ -1354,7 +1413,7 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1354
1413
  *
1355
1414
  * @param workerNodeKey - The worker node key.
1356
1415
  */
1357
- protected abstract destroyWorkerNode(workerNodeKey: number): Promise<void>;
1416
+ protected destroyWorkerNode(workerNodeKey: number): Promise<void>;
1358
1417
  /**
1359
1418
  * Setup hook to execute code before worker nodes are created in the abstract constructor.
1360
1419
  * Can be overridden.
@@ -1415,12 +1474,6 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1415
1474
  * @param transferList - The optional array of transferable objects.
1416
1475
  */
1417
1476
  protected abstract sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: TransferListItem[]): void;
1418
- /**
1419
- * Creates a new worker.
1420
- *
1421
- * @returns Newly created worker.
1422
- */
1423
- protected abstract createWorker(): Worker;
1424
1477
  /**
1425
1478
  * Creates a new, completely set up worker node.
1426
1479
  *
@@ -1473,6 +1526,7 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1473
1526
  * @param workerNodeKey - The worker node key.
1474
1527
  */
1475
1528
  private sendStatisticsMessageToWorker;
1529
+ private handleTask;
1476
1530
  private redistributeQueuedTasks;
1477
1531
  private updateTaskStolenStatisticsWorkerUsage;
1478
1532
  private updateTaskSequentiallyStolenStatisticsWorkerUsage;
@@ -1499,17 +1553,23 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1499
1553
  */
1500
1554
  protected getWorkerInfo(workerNodeKey: number): WorkerInfo;
1501
1555
  /**
1502
- * Adds the given worker in the pool worker nodes.
1556
+ * Creates a worker node.
1503
1557
  *
1504
- * @param worker - The worker.
1558
+ * @returns The created worker node.
1559
+ */
1560
+ private createWorkerNode;
1561
+ /**
1562
+ * Adds the given worker node in the pool worker nodes.
1563
+ *
1564
+ * @param workerNode - The worker node.
1505
1565
  * @returns The added worker node key.
1506
1566
  * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
1507
1567
  */
1508
1568
  private addWorkerNode;
1509
1569
  /**
1510
- * Removes the given worker from the pool worker nodes.
1570
+ * Removes the worker node from the pool worker nodes.
1511
1571
  *
1512
- * @param worker - The worker.
1572
+ * @param workerNode - The worker node.
1513
1573
  */
1514
1574
  private removeWorkerNode;
1515
1575
  protected flagWorkerNodeAsNotReady(workerNodeKey: number): void;
@@ -1530,23 +1590,6 @@ declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Resp
1530
1590
  private flushTasksQueues;
1531
1591
  }
1532
1592
 
1533
- /**
1534
- * Options for a poolifier cluster pool.
1535
- */
1536
- interface ClusterPoolOptions extends PoolOptions<Worker> {
1537
- /**
1538
- * Key/value pairs to add to worker process environment.
1539
- *
1540
- * @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
1541
- */
1542
- env?: Record<string, unknown>;
1543
- /**
1544
- * Cluster settings.
1545
- *
1546
- * @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
1547
- */
1548
- settings?: ClusterSettings;
1549
- }
1550
1593
  /**
1551
1594
  * A cluster pool with a fixed number of workers.
1552
1595
  *
@@ -1556,7 +1599,7 @@ interface ClusterPoolOptions extends PoolOptions<Worker> {
1556
1599
  * @since 2.0.0
1557
1600
  */
1558
1601
  declare class FixedClusterPool<Data = unknown, Response = unknown> extends AbstractPool<Worker, Data, Response> {
1559
- protected readonly opts: ClusterPoolOptions;
1602
+ protected readonly opts: PoolOptions<Worker>;
1560
1603
  /**
1561
1604
  * Constructs a new poolifier fixed cluster pool.
1562
1605
  *
@@ -1564,14 +1607,12 @@ declare class FixedClusterPool<Data = unknown, Response = unknown> extends Abstr
1564
1607
  * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
1565
1608
  * @param opts - Options for this fixed cluster pool.
1566
1609
  */
1567
- constructor(numberOfWorkers: number, filePath: string, opts?: ClusterPoolOptions);
1610
+ constructor(numberOfWorkers: number, filePath: string, opts?: PoolOptions<Worker>);
1568
1611
  /** @inheritDoc */
1569
1612
  protected setupHook(): void;
1570
1613
  /** @inheritDoc */
1571
1614
  protected isMain(): boolean;
1572
1615
  /** @inheritDoc */
1573
- protected destroyWorkerNode(workerNodeKey: number): Promise<void>;
1574
- /** @inheritDoc */
1575
1616
  protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>): void;
1576
1617
  /** @inheritDoc */
1577
1618
  protected sendStartupMessageToWorker(workerNodeKey: number): void;
@@ -1582,8 +1623,6 @@ declare class FixedClusterPool<Data = unknown, Response = unknown> extends Abstr
1582
1623
  /** @inheritDoc */
1583
1624
  protected deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1584
1625
  /** @inheritDoc */
1585
- protected createWorker(): Worker;
1586
- /** @inheritDoc */
1587
1626
  protected get type(): PoolType;
1588
1627
  /** @inheritDoc */
1589
1628
  protected get worker(): WorkerType;
@@ -1612,24 +1651,13 @@ declare class DynamicClusterPool<Data = unknown, Response = unknown> extends Fix
1612
1651
  * @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
1613
1652
  * @param opts - Options for this dynamic cluster pool.
1614
1653
  */
1615
- constructor(min: number, max: number, filePath: string, opts?: ClusterPoolOptions);
1654
+ constructor(min: number, max: number, filePath: string, opts?: PoolOptions<Worker>);
1616
1655
  /** @inheritDoc */
1617
1656
  protected get type(): PoolType;
1618
1657
  /** @inheritDoc */
1619
1658
  protected get busy(): boolean;
1620
1659
  }
1621
1660
 
1622
- /**
1623
- * Options for a poolifier thread pool.
1624
- */
1625
- interface ThreadPoolOptions extends PoolOptions<Worker$1> {
1626
- /**
1627
- * Worker options.
1628
- *
1629
- * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
1630
- */
1631
- workerOptions?: WorkerOptions$1;
1632
- }
1633
1661
  /**
1634
1662
  * A thread pool with a fixed number of threads.
1635
1663
  *
@@ -1639,7 +1667,7 @@ interface ThreadPoolOptions extends PoolOptions<Worker$1> {
1639
1667
  * @since 0.0.1
1640
1668
  */
1641
1669
  declare class FixedThreadPool<Data = unknown, Response = unknown> extends AbstractPool<Worker$1, Data, Response> {
1642
- protected readonly opts: ThreadPoolOptions;
1670
+ protected readonly opts: PoolOptions<Worker$1>;
1643
1671
  /**
1644
1672
  * Constructs a new poolifier fixed thread pool.
1645
1673
  *
@@ -1647,12 +1675,10 @@ declare class FixedThreadPool<Data = unknown, Response = unknown> extends Abstra
1647
1675
  * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
1648
1676
  * @param opts - Options for this fixed thread pool.
1649
1677
  */
1650
- constructor(numberOfThreads: number, filePath: string, opts?: ThreadPoolOptions);
1678
+ constructor(numberOfThreads: number, filePath: string, opts?: PoolOptions<Worker$1>);
1651
1679
  /** @inheritDoc */
1652
1680
  protected isMain(): boolean;
1653
1681
  /** @inheritDoc */
1654
- protected destroyWorkerNode(workerNodeKey: number): Promise<void>;
1655
- /** @inheritDoc */
1656
1682
  protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: TransferListItem[]): void;
1657
1683
  /** @inheritDoc */
1658
1684
  protected sendStartupMessageToWorker(workerNodeKey: number): void;
@@ -1663,8 +1689,6 @@ declare class FixedThreadPool<Data = unknown, Response = unknown> extends Abstra
1663
1689
  /** @inheritDoc */
1664
1690
  protected deregisterWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
1665
1691
  /** @inheritDoc */
1666
- protected createWorker(): Worker$1;
1667
- /** @inheritDoc */
1668
1692
  protected get type(): PoolType;
1669
1693
  /** @inheritDoc */
1670
1694
  protected get worker(): WorkerType;
@@ -1693,7 +1717,7 @@ declare class DynamicThreadPool<Data = unknown, Response = unknown> extends Fixe
1693
1717
  * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
1694
1718
  * @param opts - Options for this dynamic thread pool.
1695
1719
  */
1696
- constructor(min: number, max: number, filePath: string, opts?: ThreadPoolOptions);
1720
+ constructor(min: number, max: number, filePath: string, opts?: PoolOptions<Worker$1>);
1697
1721
  /** @inheritDoc */
1698
1722
  protected get type(): PoolType;
1699
1723
  /** @inheritDoc */
@@ -1710,7 +1734,7 @@ declare class DynamicThreadPool<Data = unknown, Response = unknown> extends Fixe
1710
1734
  declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, Data = unknown, Response = unknown> {
1711
1735
  protected readonly isMain: boolean;
1712
1736
  private readonly mainWorker;
1713
- protected opts: WorkerOptions;
1737
+ protected opts: WorkerOptions$1;
1714
1738
  /**
1715
1739
  * Worker id.
1716
1740
  */
@@ -1739,7 +1763,7 @@ declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, D
1739
1763
  * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
1740
1764
  * @param opts - Options for the worker.
1741
1765
  */
1742
- constructor(isMain: boolean, mainWorker: MainWorker, taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
1766
+ constructor(isMain: boolean, mainWorker: MainWorker, taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions$1);
1743
1767
  private checkWorkerOptions;
1744
1768
  /**
1745
1769
  * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
@@ -1892,7 +1916,7 @@ declare class ClusterWorker<Data = unknown, Response = unknown> extends Abstract
1892
1916
  * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
1893
1917
  * @param opts - Options for the worker.
1894
1918
  */
1895
- constructor(taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
1919
+ constructor(taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions$1);
1896
1920
  /** @inheritDoc */
1897
1921
  protected handleReadyMessage(message: MessageValue<Data>): void;
1898
1922
  /** @inheritDoc */
@@ -1926,7 +1950,7 @@ declare class ThreadWorker<Data = unknown, Response = unknown> extends AbstractW
1926
1950
  * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
1927
1951
  * @param opts - Options for the worker.
1928
1952
  */
1929
- constructor(taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
1953
+ constructor(taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions$1);
1930
1954
  /** @inheritDoc */
1931
1955
  protected handleReadyMessage(message: MessageValue<Data>): void;
1932
1956
  /** @inheritDoc */
@@ -2034,4 +2058,4 @@ declare class Deque<T> {
2034
2058
  */
2035
2059
  declare const availableParallelism: () => number;
2036
2060
 
2037
- export { AbstractPool, AbstractWorker, CircularArray, type ClusterPoolOptions, ClusterWorker, Deque, DynamicClusterPool, DynamicThreadPool, type ErrorHandler, type EventLoopUtilizationMeasurementStatistics, type ExitHandler, FixedClusterPool, FixedThreadPool, type IPool, type IWorker, type IWorkerChoiceStrategy, type IWorkerNode, type KillBehavior, KillBehaviors, type KillHandler, type Measurement, type MeasurementOptions, type MeasurementStatistics, type MeasurementStatisticsRequirements, Measurements, type MessageHandler, type MessageValue, Node, type OnlineHandler, type PoolEvent, PoolEvents, type PoolInfo, type PoolOptions, type PoolType, PoolTypes, type PromiseResponseWrapper, type StrategyData, type StrategyPolicy, type Task, type TaskAsyncFunction, type TaskFunction, type TaskFunctionOperationResult, type TaskFunctions, type TaskPerformance, type TaskStatistics, type TaskStatisticsRequirements, type TaskSyncFunction, type TasksQueueOptions, type ThreadPoolOptions, ThreadWorker, WorkerChoiceStrategies, type WorkerChoiceStrategy, WorkerChoiceStrategyContext, type WorkerChoiceStrategyOptions, type WorkerError, type WorkerInfo, type WorkerNodeEventDetail, type WorkerOptions, type WorkerStatistics, type WorkerType, WorkerTypes, type WorkerUsage, type Writable, availableParallelism };
2061
+ export { AbstractPool, AbstractWorker, CircularArray, ClusterWorker, Deque, DynamicClusterPool, DynamicThreadPool, type ErrorHandler, type EventLoopUtilizationMeasurementStatistics, type ExitHandler, FixedClusterPool, FixedThreadPool, type IPool, type IWorker, type IWorkerChoiceStrategy, type IWorkerNode, type KillBehavior, KillBehaviors, type KillHandler, type Measurement, type MeasurementOptions, type MeasurementStatistics, type MeasurementStatisticsRequirements, Measurements, type MessageHandler, type MessageValue, Node, type OnlineHandler, type PoolEvent, PoolEvents, type PoolInfo, type PoolOptions, type PoolType, PoolTypes, type PromiseResponseWrapper, type StrategyData, type StrategyPolicy, type Task, type TaskAsyncFunction, type TaskFunction, type TaskFunctionOperationResult, type TaskFunctions, type TaskPerformance, type TaskStatistics, type TaskStatisticsRequirements, type TaskSyncFunction, type TasksQueueOptions, ThreadWorker, WorkerChoiceStrategies, type WorkerChoiceStrategy, WorkerChoiceStrategyContext, type WorkerChoiceStrategyOptions, type WorkerError, type WorkerInfo, type WorkerNodeEventDetail, type WorkerNodeOptions, type WorkerOptions$1 as WorkerOptions, type WorkerStatistics, type WorkerType, WorkerTypes, type WorkerUsage, type Writable, availableParallelism };