package
run multi-threaded node.js network applications using the native cluster module
npm install cluster-magicGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
run multi-threaded node.js network applications using the native cluster module
sigtermsighup$ npm install cluster-magic --save
$ yarn add cluster-magicA working snippet is available in the examples directory. Just run node examples/startup.js
File: startup.js
Initializes the cluster application
const _cluster = require('cluster-magic');
const _app = require('./application.js');
// start the clustered app (8 workers)
_cluster.init(_app, {
numWorkers: 8
});File: application.js
Your socket based application which should be multiplexed
const net = require('net');
function startup(){
const server = net.createServer((socket) => {
// connections never end
});
server.listen(8000);
}
module.exports = {
// init hook
init: startup
};To hot-restart (zero downtime) an application, just send a SIGHUP to the master process. This spawns new workers and disconnects all current workers from the cluster-proxy
Example
# send SIGHUP to process 12345
kill -HUP 12345cluster-magic comes with as simple delayed-restart policy which suppresses infinite restart loops.
counter*200ms to avoid infinite restart loops on internal application errors.
Well..this is may not what you expect from a "clustered application" but such an error requires that a manual fix will take place by the operations team (_logger.alert event is triggerd which SHOULD be observed)To specify the number of workers you can easily pass the environment variable NUM_WORKERS to the nodejs process. Default is set to num_cpus*2
cluster-magic is OpenSource and licensed under the Terms of The MIT License (X11) - your're welcome to contribute
Estimated unpacked size across 3 packages.
66.0 kB
Includes dependencies.