feat: move usage and api to typedocs
[poolifier.git] / docs / api-details.md
1 ## [API](https://poolifier.github.io/poolifier/)
2
3 ### `pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`
4
5 `numberOfThreads/numberOfWorkers` (mandatory) Number of workers for this pool
6 `filePath` (mandatory) Path to a file with a worker implementation
7 `opts` (optional) An object with the pool options properties described below
8
9 ### `pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`
10
11 `min` (mandatory) Same as _FixedThreadPool_/_FixedClusterPool_ numberOfThreads/numberOfWorkers, this number of workers will be always active
12 `max` (mandatory) Max number of workers that this pool can contain, the new created workers will die after a threshold (default is 1 minute, you can override it in your worker implementation).
13 `filePath` (mandatory) Path to a file with a worker implementation
14 `opts` (optional) An object with the pool options properties described below
15
16 ### `pool.execute(data, name)`
17
18 `data` (optional) An object that you want to pass to your worker implementation
19 `name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'`
20
21 This method is available on both pool implementations and returns a promise with the task function execution response.
22
23 ### `pool.destroy()`
24
25 This method is available on both pool implementations and will call the terminate method on each worker.
26
27 ### `PoolOptions`
28
29 An object with these properties:
30
31 - `messageHandler` (optional) - A function that will listen for message event on each worker
32 - `errorHandler` (optional) - A function that will listen for error event on each worker
33 - `onlineHandler` (optional) - A function that will listen for online event on each worker
34 - `exitHandler` (optional) - A function that will listen for exit event on each worker
35 - `workerChoiceStrategy` (optional) - The worker choice strategy to use in this pool:
36
37 - `WorkerChoiceStrategies.ROUND_ROBIN`: Submit tasks to worker in a round robin fashion
38 - `WorkerChoiceStrategies.LEAST_USED`: Submit tasks to the worker with the minimum number of executed, executing and queued tasks
39 - `WorkerChoiceStrategies.LEAST_BUSY`: Submit tasks to the worker with the minimum tasks total execution and wait time
40 - `WorkerChoiceStrategies.LEAST_ELU`: Submit tasks to the worker with the minimum event loop utilization (ELU) (experimental)
41 - `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`: Submit tasks to worker by using a [weighted round robin scheduling algorithm](./src/pools/selection-strategies/README.md#weighted-round-robin) based on tasks execution time
42 - `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN`: Submit tasks to worker by using an [interleaved weighted round robin scheduling algorithm](./src/pools/selection-strategies/README.md#interleaved-weighted-round-robin) based on tasks execution time (experimental)
43 - `WorkerChoiceStrategies.FAIR_SHARE`: Submit tasks to worker by using a [fair share scheduling algorithm](./src/pools/selection-strategies/README.md#fair-share) based on tasks execution time (the default) or ELU active time
44
45 `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`, `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN` and `WorkerChoiceStrategies.FAIR_SHARE` strategies are targeted to heavy and long tasks.
46 Default: `WorkerChoiceStrategies.ROUND_ROBIN`
47
48 - `workerChoiceStrategyOptions` (optional) - The worker choice strategy options object to use in this pool.
49 Properties:
50
51 - `measurement` (optional) - The measurement to use in worker choice strategies: `runTime`, `waitTime` or `elu`.
52 - `runTime` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) runtime instead of the tasks average runtime in worker choice strategies.
53 - `waitTime` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) wait time instead of the tasks average wait time in worker choice strategies.
54 - `elu` (optional) - Use the tasks [median](./src/pools/selection-strategies/README.md#median) ELU instead of the tasks average ELU in worker choice strategies.
55 - `weights` (optional) - The worker weights to use in weighted round robin worker choice strategies: `{ 0: 200, 1: 300, ..., n: 100 }`.
56
57 Default: `{ runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }`
58
59 - `restartWorkerOnError` (optional) - Restart worker on uncaught error in this pool.
60 Default: `true`
61 - `enableEvents` (optional) - Events emission enablement in this pool.
62 Default: `true`
63 - `enableTasksQueue` (optional) - Tasks queue per worker enablement in this pool.
64 Default: `false`
65
66 - `tasksQueueOptions` (optional) - The worker tasks queue options object to use in this pool.
67 Properties:
68
69 - `concurrency` (optional) - The maximum number of tasks that can be executed concurrently on a worker.
70
71 Default: `{ concurrency: 1 }`
72
73 #### `ThreadPoolOptions extends PoolOptions`
74
75 - `workerOptions` (optional) - An object with the worker options to pass to worker. See [worker_threads](https://nodejs.org/api/worker_threads.html#worker_threads_new_worker_filename_options) for more details.
76
77 #### `ClusterPoolOptions extends PoolOptions`
78
79 - `env` (optional) - An object with the environment variables to pass to worker. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_fork_env) for more details.
80
81 - `settings` (optional) - An object with the cluster settings. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_settings) for more details.
82
83 ### `class YourWorker extends ThreadWorker/ClusterWorker`
84
85 `taskFunctions` (mandatory) The task function or task functions object `{ name_1: fn_1, ..., name_n: fn_n }` that you want to execute on the worker
86 `opts` (optional) An object with these properties:
87
88 - `maxInactiveTime` (optional) - Maximum waiting time in milliseconds for tasks on newly created workers. After this time newly created workers will die.
89 The last active time of your worker will be updated when it terminates a task.
90 If `killBehavior` is set to `KillBehaviors.HARD` this value represents also the timeout for the tasks that you submit to the pool, when this timeout expires your tasks is interrupted before completion and removed. The worker is killed if is not part of the minimum size of the pool.
91 If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
92 Default: `60000`
93
94 - `killBehavior` (optional) - Dictates if your worker will be deleted in case a task is active on it.
95 **KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **won't** be deleted.
96 **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted.
97 This option only apply to the newly created workers.
98 Default: `KillBehaviors.SOFT`
99
100 #### `YourWorker.hasTaskFunction(name)`
101
102 `name` (mandatory) The task function name
103
104 This method is available on both worker implementations and returns a boolean.
105
106 #### `YourWorker.addTaskFunction(name, fn)`
107
108 `name` (mandatory) The task function name
109 `fn` (mandatory) The task function
110
111 This method is available on both worker implementations and returns a boolean.
112
113 #### `YourWorker.removeTaskFunction(name)`
114
115 `name` (mandatory) The task function name
116
117 This method is available on both worker implementations and returns a boolean.
118
119 #### `YourWorker.listTaskFunctions()`
120
121 This method is available on both worker implementations and returns an array of the task function names.
122
123 #### `YourWorker.setDefaultTaskFunction(name)`
124
125 `name` (mandatory) The task function name
126
127 This method is available on both worker implementations and returns a boolean.