Create npmpublish.yml
[poolifier.git] / README.MD
CommitLineData
987e0026 1# Node Thread Pool :arrow_double_up: :on:
34a572eb 2[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
103df814 3[![Dependabot](https://badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot)](https://badgen.net/dependabot/dependabot/dependabot-core/?icon=dependabot)
e7752b74 4[![Actions Status](https://github.com/pioardi/node-pool/workflows/NodeCI/badge.svg)](https://github.com/pioardi/node-pool/actions)
b4b2dc8b 5
34a572eb 6<h2>Contents </h2>
7<h3 align="center">
8 <a href="#installation">Installation</a>
9 <span> · </span>
10 <a href="#usage">Usage</a>
11 <span> · </span>
12 <a href="#api">API</a>
13 <span> · </span>
14 <a href="#contribute">Contribute</a>
15 <span> · </span>
16 <a href="#nv">Compatibility</a>
17 <span> · </span>
18 <a href="#license">License</a>
19</h3>
20
f349ea72 21<h2> Overview </h2>
1a4ec243 22Node pool contains two <a href="https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads">worker-threads </a> pool implementations , you don' t have to deal with worker-threads complexity. <br>
13031992 23The first implementation is a static thread pool , with a defined number of threads that are started at creation time .<br>
34a572eb 24The second implementation is a dynamic thread pool with a number of threads started at creation time and other threads created when the load will increase ( with an upper limit ), the new created threads will be stopped after a threshold. <br>
25You have to implement your worker extending the ThreadWorker class<br>
26<h2 id="installation">Installation</h2>
13031992 27
1a4ec243 28```
ba2be357 29npm install node-thread-pool --save
1a4ec243 30```
34a572eb 31<h2 id="usage">Usage</h2>
1a4ec243
APA
32
33You can implement a worker in a simple way , extending the class ThreadWorker :
34
35```js
36'use strict'
37const { ThreadWorker } = require('node-pool')
38
39class MyWorker extends ThreadWorker {
40 constructor () {
41 super((data) => {
34a572eb 42 // this will be executed in the worker thread,
43 // the data will be received by using the execute method
1a4ec243 44 return { ok: 1 }
34a572eb 45 }, { maxInactiveTime: 1000 * 60})
1a4ec243
APA
46 }
47}
48module.exports = new MyWorker()
49```
50
51Instantiate your pool based on your needed :
52
53```js
54'use strict'
34a572eb 55const { FixedThreadPool, DynamicThreadPool } = require('node-pool')
1a4ec243
APA
56
57// a fixed thread pool
58const pool = new FixedThreadPool(15,
59 './yourWorker.js')
60
61// or a dynamic thread pool
62const pool = new DynamicThreadPool(10, 100,
63 './yourWorker.js')
64pool.emitter.on('FullPool', () => console.log('Pool is full'))
65
66// the execute method signature is the same for both implementations,
67// so you can easy switch from one to another
68pool.execute({}).then(res => {
69 console.log(res)
70}).catch ....
71
72```
73
28b6da3e 74<strong> See examples folder for more details.</strong>
1a4ec243 75
34a572eb 76<h2 id="nv">Node versions</h2>
77
ab1526e9 78You can use node version 10.x with --experimental-worker flag, or you can use an higher version (i.e 12.x) <br>
1a4ec243 79
34a572eb 80<h2 id="api">API</h2>
81
82### `pool = new FixedThreadPool(numThreads, filePath, opts)`
83`numThreads` (mandatory) Num of threads for this worker pool <br>
84`filePath` (mandatory) Path to a file with a worker implementation <br>
85`opts` (optional) An object with these properties :
86- `errorHandler` - A function that will listen for error event on each worker thread
87- `onlineHandler` - A function that will listen for online event on each worker thread
88- `exitHandler` - A function that will listen for exit event on each worker thread
89- `maxTasks` - This is just to avoid not useful warnings message, is used to set <a href="https://nodejs.org/dist/latest-v12.x/docs/api/events.html#events_emitter_setmaxlisteners_n">maxListeners</a> on event emitters ( workers are event emitters)
90
91### `pool = new DynamicThreadPool(min, max, filePath, opts)`
92`min` (mandatory) Same as FixedThreadPool numThreads , this number of threads will be always active <br>
93`max` (mandatory) Max number of workers that this pool can contain, the new created threads will die after a threshold ( default is 1 minute , you can override it in your worker implementation). <br>
94`filePath` (mandatory) Same as FixedThreadPool <br>
95`opts` (optional) Same as FixedThreadPool <br>
96
97### `pool.execute(data)`
98Execute method is available on both pool implementations ( return type : Promise): <br>
99`data` (mandatory) An object that you want to pass to your worker implementation <br>
100
101### `pool.destroy()`
102Destroy method is available on both pool implementations.<br>
103This method will call the terminate method on each worker.
104
105
106### `class YourWorker extends ThreadWorker`
107`fn` (mandatory) The function that you want to execute on the worker thread <br>
108`opts` (optional) An object with these properties :
109- `maxInactiveTime` - Max time to wait tasks to work on ( in ms) , after this period the new worker threads will die.
110
111<h2 id="contribute">Contribute</h2>
112
113See guidelines [CONTRIBUTING](./.github/CONTRIBUTING.md)
1a4ec243 114
1a4ec243 115
34a572eb 116<h2 id="license">License</h2>
1a4ec243
APA
117
118[MIT](https://github.com/pioardi/node-pool/blob/master/LICENSE)
119