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