refactor: reorder pool options validation
[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)
90d7d101 11 - [`pool.listTaskFunctions()`](#poollisttaskfunctions)
a47027a0
JB
12 - [`PoolOptions`](#pooloptions)
13 - [`ThreadPoolOptions extends PoolOptions`](#threadpooloptions-extends-pooloptions)
14 - [`ClusterPoolOptions extends PoolOptions`](#clusterpooloptions-extends-pooloptions)
15- [Worker](#worker)
16 - [`class YourWorker extends ThreadWorker/ClusterWorker`](#class-yourworker-extends-threadworkerclusterworker)
17 - [`YourWorker.hasTaskFunction(name)`](#yourworkerhastaskfunctionname)
18 - [`YourWorker.addTaskFunction(name, fn)`](#yourworkeraddtaskfunctionname-fn)
19 - [`YourWorker.removeTaskFunction(name)`](#yourworkerremovetaskfunctionname)
20 - [`YourWorker.listTaskFunctions()`](#yourworkerlisttaskfunctions)
21 - [`YourWorker.setDefaultTaskFunction(name)`](#yourworkersetdefaulttaskfunctionname)
22
23## Pool
24
25### `pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`
26
27`numberOfThreads/numberOfWorkers` (mandatory) Number of workers for this pool
28`filePath` (mandatory) Path to a file with a worker implementation
29`opts` (optional) An object with the pool options properties described below
30
31### `pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`
32
33`min` (mandatory) Same as _FixedThreadPool_/_FixedClusterPool_ numberOfThreads/numberOfWorkers, this number of workers will be always active
a36b9e1f 34`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).
a47027a0
JB
35`filePath` (mandatory) Path to a file with a worker implementation
36`opts` (optional) An object with the pool options properties described below
37
7d91a8cd 38### `pool.execute(data, name, transferList)`
a47027a0
JB
39
40`data` (optional) An object that you want to pass to your worker implementation
637100d9 41`name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'`
7d91a8cd 42`transferList` (optional) An array of transferable objects that you want to transfer to your [worker_threads](https://nodejs.org/api/worker_threads.html) worker implementation
a47027a0
JB
43
44This method is available on both pool implementations and returns a promise with the task function execution response.
45
47352846
JB
46### `pool.start()`
47
48This method is available on both pool implementations and will start the minimum number of workers.
49
a47027a0
JB
50### `pool.destroy()`
51
52This method is available on both pool implementations and will call the terminate method on each worker.
53
90d7d101
JB
54### `pool.listTaskFunctions()`
55
56This method is available on both pool implementations and returns an array of the task function names.
57
a47027a0
JB
58### `PoolOptions`
59
60An object with these properties:
61
d293923f
JB
62- `onlineHandler` (optional) - A function that will listen for online event on each worker.
63 Default: `() => {}`
64- `messageHandler` (optional) - A function that will listen for message event on each worker.
65 Default: `() => {}`
66- `errorHandler` (optional) - A function that will listen for error event on each worker.
67 Default: `() => {}`
68- `exitHandler` (optional) - A function that will listen for exit event on each worker.
69 Default: `() => {}`
70
a47027a0
JB
71- `workerChoiceStrategy` (optional) - The worker choice strategy to use in this pool:
72
73 - `WorkerChoiceStrategies.ROUND_ROBIN`: Submit tasks to worker in a round robin fashion
74 - `WorkerChoiceStrategies.LEAST_USED`: Submit tasks to the worker with the minimum number of executed, executing and queued tasks
75 - `WorkerChoiceStrategies.LEAST_BUSY`: Submit tasks to the worker with the minimum tasks total execution and wait time
a71b05bc 76 - `WorkerChoiceStrategies.LEAST_ELU`: Submit tasks to the worker with the minimum event loop utilization (ELU)
2904fcdb 77 - `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 78 - `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 79 - `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
80
81 `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`, `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN` and `WorkerChoiceStrategies.FAIR_SHARE` strategies are targeted to heavy and long tasks.
82 Default: `WorkerChoiceStrategies.ROUND_ROBIN`
83
84- `workerChoiceStrategyOptions` (optional) - The worker choice strategy options object to use in this pool.
85 Properties:
86
8c0b113f 87 - `retries` (optional) - The number of retries to perform if no worker is eligible.
a47027a0 88 - `measurement` (optional) - The measurement to use in worker choice strategies: `runTime`, `waitTime` or `elu`.
06916f85
JB
89 - `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.
90 - `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.
91 - `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.
a47027a0
JB
92 - `weights` (optional) - The worker weights to use in weighted round robin worker choice strategies: `{ 0: 200, 1: 300, ..., n: 100 }`.
93
8c0b113f 94 Default: `{ retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }`
a47027a0 95
d293923f
JB
96- `startWorkers` (optional) - Start the minimum number of workers at pool creation.
97 Default: `true`
a47027a0
JB
98- `restartWorkerOnError` (optional) - Restart worker on uncaught error in this pool.
99 Default: `true`
100- `enableEvents` (optional) - Events emission enablement in this pool.
101 Default: `true`
102- `enableTasksQueue` (optional) - Tasks queue per worker enablement in this pool.
103 Default: `false`
104
105- `tasksQueueOptions` (optional) - The worker tasks queue options object to use in this pool.
106 Properties:
107
fa3cc835 108 - `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 109 - `concurrency` (optional) - The maximum number of tasks that can be executed concurrently on a worker. It must be a positive integer.
d293923f 110 - `taskStealing` (optional) - Task stealing enablement.
47352846 111 - `tasksStealingOnBackPressure` (optional) - Tasks stealing enablement on back pressure.
445264e8 112
dbd73092 113 Default: `{ size: (pool maximum size)^2, concurrency: 1, taskStealing: true, tasksStealingOnBackPressure: true }`
a47027a0
JB
114
115#### `ThreadPoolOptions extends PoolOptions`
116
117- `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.
118
119#### `ClusterPoolOptions extends PoolOptions`
120
121- `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.
122
123- `settings` (optional) - An object with the cluster settings. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_settings) for more details.
124
125## Worker
126
127### `class YourWorker extends ThreadWorker/ClusterWorker`
128
129`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
130`opts` (optional) An object with these properties:
131
968dbbe7
JB
132- `killBehavior` (optional) - Dictates if your worker will be deleted in case a task is active on it.
133 **KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **won't** be deleted.
134 **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted.
135 This option only apply to the newly created workers.
136 Default: `KillBehaviors.SOFT`
137
a47027a0
JB
138- `maxInactiveTime` (optional) - Maximum waiting time in milliseconds for tasks on newly created workers. After this time newly created workers will die.
139 The last active time of your worker will be updated when it terminates a task.
140 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.
141 If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
142 Default: `60000`
143
968dbbe7
JB
144- `killHandler` (optional) - A function that will be called when a worker is killed.
145 Default: `() => {}`
a47027a0
JB
146
147#### `YourWorker.hasTaskFunction(name)`
148
149`name` (mandatory) The task function name
150
151This method is available on both worker implementations and returns a boolean.
152
153#### `YourWorker.addTaskFunction(name, fn)`
154
155`name` (mandatory) The task function name
156`fn` (mandatory) The task function
157
158This method is available on both worker implementations and returns a boolean.
159
160#### `YourWorker.removeTaskFunction(name)`
161
162`name` (mandatory) The task function name
163
164This method is available on both worker implementations and returns a boolean.
165
166#### `YourWorker.listTaskFunctions()`
167
168This method is available on both worker implementations and returns an array of the task function names.
169
170#### `YourWorker.setDefaultTaskFunction(name)`
171
172`name` (mandatory) The task function name
173
174This method is available on both worker implementations and returns a boolean.