https://github.com/noviel/cluster-launcher
Minimalistic multiprocess launcher for Node.js
https://github.com/noviel/cluster-launcher
Last synced: about 1 year ago
JSON representation
Minimalistic multiprocess launcher for Node.js
- Host: GitHub
- URL: https://github.com/noviel/cluster-launcher
- Owner: Noviel
- License: mit
- Created: 2017-02-09T17:26:36.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-15T18:26:39.000Z (over 9 years ago)
- Last Synced: 2025-02-05T02:17:14.068Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cluster multiprocess launcher
## bI?
Minimalistic configurable multiprocess launcher for Node.js based on cluster. Includes built-in sticky listeners for Socket.IO.
## Installation
`npm i cluster-launcher --save`
## Usage
```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
### .launch(opts)
@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:
- **ip** { string }
- **port** { number }
- **workers** { array }
### .stickyListenWorker, .stickyListenMaster
Default built-in listen function for worker, that provide sticky connection for the client to
specific worker based on the client's ip address. Usefull for Socket.IO.