
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
master-cluster
Advanced tools
Facilitate using cluster and domain modules, live reload application in development mode on code change.
master-cluster is a utility to facilitate implementing node.js core cluster module in any project. I created this module as I use the cluster module in pretty much all my projects and wanted to remove the boilerplate code. It also provides hot reloading of files on the worker in development mode so that workers are restarted when code is changed.
In boot.js
:
var MC = require('master-cluster')
, worker = require.resolve('./server.js');
MC.start({exec: worker});
In server.js
:
var MC = require('master-cluster')
, app = require('./app.js')
, http = require('http');
// set the handler that will be used for all new requests
MC.setFnHandlers(app.index, shutdown);
// create the server and pass MC run handler
// workers are restarted automatically on crash
var server = http.createServer(MC.run);
server.listen(3000, function () {
console.log("Listening on %d", 3000);
});
function shutdown () {
// optional - cleanly close db connections and other resources
}
In app.js
:
function index (req, res) {
// do something with the request and response
}
exports.index = index;
The code above can be simplified if all you need is to start a http server.
In server.js
:
var MC = require('master-cluster')
, app = require('./app.js');
MC.createHttpServer(app.index, 3000, shutdown, function () {
console.log("Listening on %d", 3000);
});
function shutdown () {
// cleanly close db connections and other resources
}
var app = require('express')();
var MC = require('master-cluster');
var responseTime = require('response-time');
// setup middlewares, mount routes, etc.
app.use(responseTime({ header: 'x-response-time' }));
app.get('/', function (req, res) {
res.end('Hello world');
});
MC.createHttpServer(app, 3000);
console.log('Server listening on %d', 3000);
Following options can be passed to the master cluster configuration:
exec
: file path to worker file (see exec
option of cluster.setupMaster)size
: the number of workers to start, default is require("os").cpus().length
reload
: boolean
, default is false
except when /^dev/.test(process.env.NODE_ENV)
logger
: optional logger for errors (must implement error
method)isCluster
: pass false
to disable cluster
and run master-only process. Setting exec
is required in this caseReloader specific options:
cooldown
: number of milliseconds to wait between file changes, default is 100ms
extensions
: file extensions to watch for and reload on change, default to js
path
: path to watch for change, default to current directory (.
)Worker options:
killTimeout
: timeout to kill the worker on uncaught exception (default 30s)logger
: optional logger for errors (must implement error
method)start (options)
: start the master with cluster optionsrun ()
: worker http handler that runs the requestsetFnHandlers (runFn, errorFn)
: set the run and error handlerssetOptions (options)
: set the options for the workers (logger and kill timeout)createHttpServer (handler, port, onShutdown, onListening)
: create the http server and setup the run and error handlerscluster
: expose node.js cluster moduleThis module is still experimental and subject to change. Please open issues for bugs and suggestions in github. Pull requests welcome.
Jerome Touffe-Blin, @jtblin, http://www.linkedin.com/in/jtblin
master-cluster is copyright 2013 Jerome Touffe-Blin and contributors. It is licensed under the BSD license. See the include LICENSE file for details.
FAQs
Facilitate using cluster and domain modules, live reload application in development mode on code change.
The npm package master-cluster receives a total of 4 weekly downloads. As such, master-cluster popularity was classified as not popular.
We found that master-cluster demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.