feat: add queued tasks end timeout support to worker node termination
[poolifier.git] / docs / api.md
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)
8 - [`pool.execute(data, name, transferList)`](#poolexecutedata-name-transferlist)
9 - [`pool.start()`](#poolstart)
10 - [`pool.destroy()`](#pooldestroy)
11 - [`pool.hasTaskFunction(name)`](#poolhastaskfunctionname)
12 - [`pool.addTaskFunction(name, fn)`](#pooladdtaskfunctionname-fn)
13 - [`pool.removeTaskFunction(name)`](#poolremovetaskfunctionname)
14 - [`pool.listTaskFunctionNames()`](#poollisttaskfunctionnames)
15 - [`pool.setDefaultTaskFunction(name)`](#poolsetdefaulttaskfunctionname)
16 - [`PoolOptions`](#pooloptions)
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)
22 - [`YourWorker.listTaskFunctionNames()`](#yourworkerlisttaskfunctionnames)
23 - [`YourWorker.setDefaultTaskFunction(name)`](#yourworkersetdefaulttaskfunctionname)
24
25 ## Pool
26
27 ### `pool = new FixedThreadPool/FixedClusterPool(numberOfThreads/numberOfWorkers, filePath, opts)`
28
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.
32
33 ### `pool = new DynamicThreadPool/DynamicClusterPool(min, max, filePath, opts)`
34
35 `min` (mandatory) Same as _FixedThreadPool_/_FixedClusterPool_ numberOfThreads/numberOfWorkers, this number of workers will be always active.
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).
37 `filePath` (mandatory) Path to a file with a worker implementation.
38 `opts` (optional) An object with the pool options properties described below.
39
40 ### `pool.execute(data, name, transferList)`
41
42 `data` (optional) An object that you want to pass to your worker task function implementation.
43 `name` (optional) A string with the task function name that you want to execute on the worker. Default: `'default'`
44 `transferList` (optional) An array of transferable objects that you want to transfer to your [`ThreadWorker`](#class-yourworker-extends-threadworkerclusterworker) worker implementation.
45
46 This method is available on both pool implementations and returns a promise with the task function execution response.
47
48 ### `pool.start()`
49
50 This method is available on both pool implementations and will start the minimum number of workers.
51
52 ### `pool.destroy()`
53
54 This method is available on both pool implementations and will call the terminate method on each worker.
55
56 ### `pool.hasTaskFunction(name)`
57
58 `name` (mandatory) The task function name.
59
60 This method is available on both pool implementations and returns a boolean.
61
62 ### `pool.addTaskFunction(name, fn)`
63
64 `name` (mandatory) The task function name.
65 `fn` (mandatory) The task function.
66
67 This method is available on both pool implementations and returns a boolean promise.
68
69 ### `pool.removeTaskFunction(name)`
70
71 `name` (mandatory) The task function name.
72
73 This method is available on both pool implementations and returns a boolean promise.
74
75 ### `pool.listTaskFunctionNames()`
76
77 This method is available on both pool implementations and returns an array of the task function names.
78
79 ### `pool.setDefaultTaskFunction(name)`
80
81 `name` (mandatory) The task function name.
82
83 This method is available on both pool implementations and returns a boolean promise.
84
85 ### `PoolOptions`
86
87 An object with these properties:
88
89 - `onlineHandler` (optional) - A function that will listen for online event on each worker.
90 Default: `() => {}`
91 - `messageHandler` (optional) - A function that will listen for message event on each worker.
92 Default: `() => {}`
93 - `errorHandler` (optional) - A function that will listen for error event on each worker.
94 Default: `() => {}`
95 - `exitHandler` (optional) - A function that will listen for exit event on each worker.
96 Default: `() => {}`
97
98 - `workerChoiceStrategy` (optional) - The worker choice strategy to use in this pool:
99
100 - `WorkerChoiceStrategies.ROUND_ROBIN`: Submit tasks to worker in a round robin fashion
101 - `WorkerChoiceStrategies.LEAST_USED`: Submit tasks to the worker with the minimum number of executed, executing and queued tasks
102 - `WorkerChoiceStrategies.LEAST_BUSY`: Submit tasks to the worker with the minimum tasks total execution and wait time
103 - `WorkerChoiceStrategies.LEAST_ELU`: Submit tasks to the worker with the minimum event loop utilization (ELU)
104 - `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
105 - `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)
106 - `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
107
108 `WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN`, `WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN` and `WorkerChoiceStrategies.FAIR_SHARE` strategies are targeted to heavy and long tasks.
109 Default: `WorkerChoiceStrategies.ROUND_ROBIN`
110
111 - `workerChoiceStrategyOptions` (optional) - The worker choice strategy options object to use in this pool.
112 Properties:
113
114 - `retries` (optional) - The number of retries to perform if no worker is eligible.
115 - `measurement` (optional) - The measurement to use in worker choice strategies: `runTime`, `waitTime` or `elu`.
116 - `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.
117 - `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.
118 - `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.
119 - `weights` (optional) - The worker weights to use in weighted round robin worker choice strategies: `{ 0: 200, 1: 300, ..., n: 100 }`.
120
121 Default: `{ retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }`
122
123 - `startWorkers` (optional) - Start the minimum number of workers at pool initialization.
124 Default: `true`
125 - `restartWorkerOnError` (optional) - Restart worker on uncaught error in this pool.
126 Default: `true`
127 - `enableEvents` (optional) - Events integrated with async resource emission enablement in this pool.
128 Default: `true`
129 - `enableTasksQueue` (optional) - Tasks queue per worker enablement in this pool.
130 Default: `false`
131
132 - `tasksQueueOptions` (optional) - The worker tasks queue options object to use in this pool.
133 Properties:
134
135 - `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.
136 - `concurrency` (optional) - The maximum number of tasks that can be executed concurrently on a worker. It must be a positive integer.
137 - `taskStealing` (optional) - Task stealing enablement on idle.
138 - `tasksStealingOnBackPressure` (optional) - Tasks stealing enablement under back pressure.
139 - `tasksFinishedTimeout` (optional) - Queued tasks finished timeout in milliseconds at worker termination.
140
141 Default: `{ size: (pool maximum size)^2, concurrency: 1, taskStealing: true, tasksStealingOnBackPressure: true, tasksFinishedTimeout: 1000 }`
142
143 - `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.
144
145 - `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.
146
147 - `settings` (optional) - An object with the cluster settings. See [cluster](https://nodejs.org/api/cluster.html#cluster_cluster_settings) for more details.
148
149 ## Worker
150
151 ### `class YourWorker extends ThreadWorker/ClusterWorker`
152
153 `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.
154 `opts` (optional) An object with these properties:
155
156 - `killBehavior` (optional) - Dictates if your worker will be deleted in case a task is active on it.
157 **KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **won't** be deleted.
158 **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted.
159 This option only apply to the newly created workers.
160 Default: `KillBehaviors.SOFT`
161
162 - `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.
163 The last active time of your worker will be updated when it terminates a task.
164 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.
165 If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
166 Default: `60000`
167
168 - `killHandler` (optional) - A function that will be called when a worker is killed.
169 Default: `() => {}`
170
171 #### `YourWorker.hasTaskFunction(name)`
172
173 `name` (mandatory) The task function name.
174
175 This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`.
176
177 #### `YourWorker.addTaskFunction(name, fn)`
178
179 `name` (mandatory) The task function name.
180 `fn` (mandatory) The task function.
181
182 This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`.
183
184 #### `YourWorker.removeTaskFunction(name)`
185
186 `name` (mandatory) The task function name.
187
188 This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`.
189
190 #### `YourWorker.listTaskFunctionNames()`
191
192 This method is available on both worker implementations and returns an array of the task function names.
193
194 #### `YourWorker.setDefaultTaskFunction(name)`
195
196 `name` (mandatory) The task function name.
197
198 This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`.