Minimalistic multiprocess launcher for Node.js
npm install cluster-launchernpm i cluster-launcher --save
javascript
const {
launch,
stickyListenWorker,
stickyListenMaster
} = require('cluster-launcher');
const workerFunc = (listen) => {
// Use express for example.
const app = require('express')();
const http = require('http').Server(app);
app.get('*', (req, res) => {
res.send(Hello from worker pid:${process.pid});
});
listen(http);
};
const masterFunc = (listen) => {
console.log('We are doing some crazy master stuff here!');
listen();
};
launch({
worker: {
main: workerFunc,
listen: stickyListenWorker
},
master: {
main: masterFunc,
listen: stickyListenMaster
},
config: {
threads: 4,
host: {
ip: 'localhost',
port: 5000
}
}
});
`
API
$3
@param opts { object } options object
Expected that master and worker will call listen by themselves, because they can contain some async init functions.
##### Options object
- config { object }
- threads { number } count of worker processes to spawn. default: 1
- host { object } web server configuration
- ip { string } default: 'localhost'
- port { number } default: 8080
- worker
- main { function } entry point of the every worker, takes listen as a parameter
- listen { function } default: stickyListenWorker
- master
- main { function } entry point of the master
- listen { function } default: stickyListenMaster
worker/master's main will be called with specified to them listen function as a parameter.
##### Worker's listen
Expects http server object as a parameter.
##### Master's listen
Will be wrapped in a function that takes no arguments. Because of it in the master's main you should just do listen(). But inside it will be called by launch` with options object as a parameter: