Comparing version 3.0.0 to 4.0.0
@@ -14,7 +14,15 @@ 'use strict'; | ||
const NOOP = () => {}; | ||
module.exports = function throng(options, startFunction) { | ||
let startFn = typeof options === 'function' ? | ||
options : startFunction; | ||
options = options || {}; | ||
let startFn = options.start || startFunction || options; | ||
let masterFn = options.master || NOOP; | ||
if (cluster.isWorker) return startFn(cluster.worker.id); | ||
if (typeof startFn !== 'function') { | ||
throw new Error('Start function required'); | ||
} | ||
if (cluster.isWorker) { | ||
return startFn(cluster.worker.id); | ||
} | ||
@@ -28,2 +36,3 @@ let opts = isNaN(options) ? | ||
listen(); | ||
masterFn(); | ||
fork(); | ||
@@ -30,0 +39,0 @@ |
{ | ||
"name": "throng", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "A simple worker-manager for clustered apps", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -34,2 +34,4 @@ # Throng | ||
Simplest; automatically fork 1 worker per CPU core: | ||
```js | ||
@@ -39,3 +41,3 @@ throng(startFunction); | ||
Provide a worker count: | ||
Specify a number of workers: | ||
@@ -49,3 +51,8 @@ ```js | ||
```js | ||
throng({ workers: 16, grace: 1000 }, startFunction); | ||
throng({ | ||
workers: 16, | ||
grace: 1000, | ||
master: masterFunction, | ||
start: startFunction | ||
}); | ||
``` | ||
@@ -77,2 +84,50 @@ | ||
## A Complex example | ||
```js | ||
const throng = require('./lib/throng'); | ||
throng({ | ||
workers: 4, | ||
master: startMaster, | ||
start: startWorker | ||
}); | ||
// This will only be called once | ||
function startMaster() { | ||
console.log(`Started master`); | ||
} | ||
// This will be called four times | ||
function startWorker(id) { | ||
console.log(`Started worker ${ id }`); | ||
process.on('SIGTERM', () => { | ||
console.log(`Worker ${ id } exiting...`); | ||
console.log('(cleanup would happen here)'); | ||
process.exit(); | ||
}); | ||
} | ||
``` | ||
``` | ||
$ node example-complex.js | ||
Started master | ||
Started worker 1 | ||
Started worker 2 | ||
Started worker 3 | ||
Started worker 4 | ||
$ killall node | ||
Worker 3 exiting... | ||
Worker 4 exiting... | ||
(cleanup would happen here) | ||
(cleanup would happen here) | ||
Worker 2 exiting... | ||
(cleanup would happen here) | ||
Worker 1 exiting... | ||
(cleanup would happen here) | ||
``` | ||
## Tests | ||
@@ -79,0 +134,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4449
61
134