build(deps-dev): apply updates
[poolifier.git] / docs / api.md
CommitLineData
a47027a0
JB
1# [API](https://poolifier.github.io/poolifier/)
2
3## Table of contents
4
5- [Pool](#pool)
6 - [`pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`](#pool--new-fixedthreadpoolfixedclusterpoolnumberofthreadsnumberofworkers-filepath-opts)
7 - [`pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`](#pool--new-dynamicthreadpooldynamicclusterpoolmin-max-filepath-opts)
7d91a8cd 8 - [`pool.execute(data, name, transferList)`](#poolexecutedata-name-transferlist)
47352846 9 - [`pool.start()`](#poolstart)
a47027a0 10 - [`pool.destroy()`](#pooldestroy)
9eae3c69
JB
11 - [`pool.hasTaskFunction(name)`](#poolhastaskfunctionname)
12 - [`pool.addTaskFunction(name, fn)`](#pooladdtaskfunctionname-fn)
13 - [`pool.removeTaskFunction(name)`](#poolremovetaskfunctionname)
31847469 14 - [`pool.listTaskFunctionsProperties()`](#poollisttaskfunctionsproperties)
9eae3c69 15 - [`pool.setDefaultTaskFunction(name)`](#poolsetdefaulttaskfunctionname)
31d71267 16 - [Pool options](#pool-options)
a47027a0
JB
17- [Worker](#worker)
18 - [`class YourWorker extends ThreadWorker/ClusterWorker`](#class-yourworker-extends-threadworkerclusterworker)
19 - [`YourWorker.hasTaskFunction(name)`](#yourworkerhastaskfunctionname)
20 - [`YourWorker.addTaskFunction(name, fn)`](#yourworkeraddtaskfunctionname-fn)
21 - [`YourWorker.removeTaskFunction(name)`](#yourworkerremovetaskfunctionname)
31847469 22 - [`YourWorker.listTaskFunctionsProperties()`](#yourworkerlisttaskfunctionsproperties)
a47027a0
JB
23 - [`YourWorker.setDefaultTaskFunction(name)`](#yourworkersetdefaulttaskfunctionname)
24
25## Pool
26
27### `pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`
28
8c445a4e
JB
29`numberOfThreads/numberOfWorkers` (mandatory) Number of workers for this pool.
30`filePath` (mandatory) Path to a file with a worker implementation.
31`opts` (optional) An object with the pool options properties described below.
a47027a0
JB
32
33### `pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`
34
8c445a4e 35`min` (mandatory) Same as _FixedThreadPool_/_FixedClusterPool_ numberOfThreads/numberOfWorkers, this number of workers will be always active.
a36b9e1f 36`max` (mandatory) Max number of workers that this pool can contain, the newly created workers will die after a threshold (default is 1 minute, you can override it in your worker implementation).
8c445a4e
JB
37`filePath` (mandatory) Path to a file with a worker implementation.
38`opts` (optional) An object with the pool options properties described below.
a47027a0 39
7d91a8cd 40### `pool.execute(data, name, transferList)`
a47027a0 41
b51d8596 42`data` (optional) An object that you want to pass to your worker task function implementation.
637100d9 43`name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'`
5e4dbac9 44`transferList` (optional) An array of transferable objects that you want to transfer to your [`ThreadWorker`](#class-yourworker-extends-threadworkerclusterworker) worker implementation.
a47027a0
JB
45
46This method is available on both pool implementations and returns a promise with the task function execution response.
47
d0798374
JB
48### `pool.mapExecute(data, name, transferList)`
49
50`data` Iterable objects that you want to pass to your worker task function implementation.
51`name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'`
52`transferList` (optional) An array of transferable objects that you want to transfer to your [`ThreadWorker`](#class-yourworker-extends-threadworkerclusterworker) worker implementation.
53
54This method is available on both pool implementations and returns a promise with the task function execution responses array.
55
47352846
JB
56### `pool.start()`
57
58This method is available on both pool implementations and will start the minimum number of workers.
59
a47027a0
JB
60### `pool.destroy()`
61
62This method is available on both pool implementations and will call the terminate method on each worker.
63
30500265
JB
64### `pool.hasTaskFunction(name)`
65
679e657a 66`name` (mandatory) The task function name.
30500265
JB
67
68This method is available on both pool implementations and returns a boolean.
69
9eae3c69
JB
70### `pool.addTaskFunction(name, fn)`
71
e7277e38 72`name` (mandatory) The task function name.
3e3e2e5d 73`fn` (mandatory) The task function `(data?: Data) => Response | Promise<Response>` or task function object `{ taskFunction: (data?: Data) => Response | Promise<Response>, priority?: number, strategy?: WorkerChoiceStrategy }`. Priority range is the same as Unix nice levels.
9eae3c69
JB
74
75This method is available on both pool implementations and returns a boolean promise.
76
77### `pool.removeTaskFunction(name)`
78
79`name` (mandatory) The task function name.
80
81This method is available on both pool implementations and returns a boolean promise.
82
31847469 83### `pool.listTaskFunctionsProperties()`
90d7d101 84
31847469 85This method is available on both pool implementations and returns an array of the task function properties.
90d7d101 86
9eae3c69
JB
87### `pool.setDefaultTaskFunction(name)`
88
89`name` (mandatory) The task function name.
90
91This method is available on both pool implementations and returns a boolean promise.
92
31d71267 93### Pool options
a47027a0
JB
94
95An object with these properties:
96
d293923f
JB
97- `onlineHandler` (optional) - A function that will listen for online event on each worker.
98 Default: `() => {}`
99- `messageHandler` (optional) - A function that will listen for message event on each worker.
100 Default: `() => {}`
101- `errorHandler` (optional) - A function that will listen for error event on each worker.
102 Default: `() => {}`
103- `exitHandler` (optional) - A function that will listen for exit event on each worker.
104 Default: `() => {}`
105
bcfb06ce 106- `workerChoiceStrategy` (optional) - The default worker choice strategy to use in this pool:
a47027a0
JB
107
108 - `WorkerChoiceStrategies.ROUND_ROBIN`: Submit tasks to worker in a round robin fashion
e0843544 109 - `WorkerChoiceStrategies.LEAST_USED`: Submit tasks to the worker with the minimum number of executing and queued tasks
b2940269 110 - `WorkerChoiceStrategies.LEAST_BUSY`: Submit tasks to the worker with the minimum tasks execution time
a71b05bc 111 - `WorkerChoiceStrategies.LEAST_ELU`: Submit tasks to the worker with the minimum event loop utilization (ELU)
2904fcdb 112 - `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`: Submit tasks to worker by using a [weighted round robin scheduling algorithm](./worker-choice-strategies.md#weighted-round-robin) based on tasks execution time
bde1a004 113 - `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN`: Submit tasks to worker by using an [interleaved weighted round robin scheduling algorithm](./worker-choice-strategies.md#interleaved-weighted-round-robin-experimental) based on tasks execution time (experimental)
2904fcdb 114 - `WorkerChoiceStrategies.FAIR_SHARE`: Submit tasks to worker by using a [fair share scheduling algorithm](./worker-choice-strategies.md#fair-share) based on tasks execution time (the default) or ELU active time
a47027a0
JB
115
116 `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`, `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN` and `WorkerChoiceStrategies.FAIR_SHARE` strategies are targeted to heavy and long tasks.
117 Default: `WorkerChoiceStrategies.ROUND_ROBIN`
118
119- `workerChoiceStrategyOptions` (optional) - The worker choice strategy options object to use in this pool.
120 Properties:
121
122 - `measurement` (optional) - The measurement to use in worker choice strategies: `runTime`, `waitTime` or `elu`.
06916f85
JB
123 - `runTime` (optional) - Use the tasks [simple moving median](./worker-choice-strategies.md#simple-moving-median) runtime instead of the tasks simple moving average runtime in worker choice strategies.
124 - `waitTime` (optional) - Use the tasks [simple moving median](./worker-choice-strategies.md#simple-moving-median) wait time instead of the tasks simple moving average wait time in worker choice strategies.
125 - `elu` (optional) - Use the tasks [simple moving median](./worker-choice-strategies.md#simple-moving-median) ELU instead of the tasks simple moving average ELU in worker choice strategies.
181af286 126 - `weights` (optional) - The worker weights to use in weighted round robin worker choice strategies: `Record<number, number>`.
a47027a0 127
26ce26ca 128 Default: `{ runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }`
a47027a0 129
ce0ab2d7 130- `startWorkers` (optional) - Start the minimum number of workers at pool initialization.
d293923f 131 Default: `true`
a47027a0
JB
132- `restartWorkerOnError` (optional) - Restart worker on uncaught error in this pool.
133 Default: `true`
d67bed32 134- `enableEvents` (optional) - Pool events integrated with async resource emission enablement.
a47027a0
JB
135 Default: `true`
136- `enableTasksQueue` (optional) - Tasks queue per worker enablement in this pool.
137 Default: `false`
138
139- `tasksQueueOptions` (optional) - The worker tasks queue options object to use in this pool.
140 Properties:
141
fa3cc835 142 - `size` (optional) - The maximum number of tasks that can be queued on a worker before flagging it as back pressured. It must be a positive integer.
5137e2ae 143 - `concurrency` (optional) - The maximum number of tasks that can be executed concurrently on a worker. It must be a positive integer.
65542a35 144 - `taskStealing` (optional) - Task stealing enablement on idle.
af98b972 145 - `tasksStealingOnBackPressure` (optional) - Tasks stealing enablement under back pressure.
32b141fd 146 - `tasksFinishedTimeout` (optional) - Queued tasks finished timeout in milliseconds at worker termination.
445264e8 147
2eee7220 148 Default: `{ size: (pool maximum size)^2, concurrency: 1, taskStealing: true, tasksStealingOnBackPressure: false, tasksFinishedTimeout: 2000 }`
a47027a0 149
a47027a0
JB
150- `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.
151
a47027a0
JB
152- `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.
153
154- `settings` (optional) - An object with the cluster settings. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_settings) for more details.
155
156## Worker
157
158### `class YourWorker extends ThreadWorker/ClusterWorker`
159
3e3e2e5d 160`taskFunctions` (mandatory) The task function or task functions object `Record<string, (data?: Data) => Response | Promise<Response> | { taskFunction: (data?: Data) => Response | Promise<Response>, priority?: number, strategy?: WorkerChoiceStrategy }>` that you want to execute on the worker. Priority range is the same as Unix nice levels.
a47027a0
JB
161`opts` (optional) An object with these properties:
162
968dbbe7 163- `killBehavior` (optional) - Dictates if your worker will be deleted in case a task is active on it.
152e87a8
JB
164 **KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but the worker is stealing tasks or a task is executing or queued, then the worker **won't** be deleted.
165 **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but the worker is stealing tasks or a task is executing or queued, then the worker will be deleted.
968dbbe7
JB
166 This option only apply to the newly created workers.
167 Default: `KillBehaviors.SOFT`
168
c20084b6 169- `maxInactiveTime` (optional) - Maximum waiting time in milliseconds for tasks on newly created workers. After this time newly created workers will die. It must be a positive integer greater or equal than 5.
a47027a0
JB
170 The last active time of your worker will be updated when it terminates a task.
171 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.
172 If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
173 Default: `60000`
174
968dbbe7
JB
175- `killHandler` (optional) - A function that will be called when a worker is killed.
176 Default: `() => {}`
a47027a0
JB
177
178#### `YourWorker.hasTaskFunction(name)`
179
8c445a4e 180`name` (mandatory) The task function name.
a47027a0 181
30500265 182This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`.
a47027a0
JB
183
184#### `YourWorker.addTaskFunction(name, fn)`
185
8c445a4e 186`name` (mandatory) The task function name.
3e3e2e5d 187`fn` (mandatory) The task function `(data?: Data) => Response | Promise<Response>` or task function object `{ taskFunction: (data?: Data) => Response | Promise<Response>, priority?: number, strategy?: WorkerChoiceStrategy }`. Priority range is the same as Unix nice levels.
a47027a0 188
30500265 189This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`.
a47027a0
JB
190
191#### `YourWorker.removeTaskFunction(name)`
192
8c445a4e 193`name` (mandatory) The task function name.
a47027a0 194
30500265 195This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`.
a47027a0 196
31847469 197#### `YourWorker.listTaskFunctionsProperties()`
a47027a0 198
31847469 199This method is available on both worker implementations and returns an array of the task function properties.
a47027a0
JB
200
201#### `YourWorker.setDefaultTaskFunction(name)`
202
8c445a4e 203`name` (mandatory) The task function name.
a47027a0 204
30500265 205This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`.