division
Version:
Simple yet powerful wrapper over node.js cluster API. This module is inspired by impressive, but abandoned project Cluster created by TJ Holowaychuk.
58 lines (57 loc) • 2.6 kB
JavaScript
module.exports = function(options, logger) {
if (logger == null) {
logger = require('./helpers/logger');
}
options = options || {
console: true
};
this.on('error', function(error) {
return logger.error(error, options);
});
this.on('increase', function(amount) {
return logger.info("Number of workers increased by " + amount, options);
});
this.on('decrease', function(amount) {
return logger.info("Number of workers decreased by " + amount, options);
});
this.on('restart', function() {
return logger.info('Worker processes restarted', options);
});
this.on('close', function() {
return logger.info('Gracefully closing cluster process', options);
});
this.on('destroy', function() {
return logger.warn('Forcefully closing (killing) cluster process', options);
});
this.on('fork', function(worker) {
return logger.debug('New worker forked', options);
});
this.on('online', function(worker) {
return logger.debug("Worker no. " + ((worker != null ? worker.id : void 0) || "?") + " (PID: " + ((worker != null ? worker.pid : void 0) || "unknown") + ") is online", options);
});
this.on('listening', function(worker, address) {
return logger.debug("Worker no. " + ((worker != null ? worker.id : void 0) || "?") + " (PID: " + ((worker != null ? worker.pid : void 0) || "unknown") + ") is listening", options);
});
this.on('disconnect', function(worker) {
return logger.debug("Worker no. " + ((worker != null ? worker.id : void 0) || "?") + " (PID: " + ((worker != null ? worker.pid : void 0) || "unknown") + ") is disconnected", options);
});
this.on('exit', function(worker, code, signal) {
var _ref;
if (worker != null ? (_ref = worker.instance) != null ? _ref.suicide : void 0 : void 0) {
return logger.debug("Worker no. " + ((worker != null ? worker.id : void 0) || "?") + " (PID: " + ((worker != null ? worker.pid : void 0) || "unknown") + ") is exited", options);
} else {
return logger.error("Worker no. " + ((worker != null ? worker.id : void 0) || "?") + " (PID: " + ((worker != null ? worker.pid : void 0) || "unknown") + ") is exited unexpectedly (code: " + code + ", signal: " + signal + ")", options);
}
});
process.on('uncaughtException', function(error) {
logger.error(error.stack, options);
return process.exit(1);
});
process.on('exit', function() {
return logger.debug('Cluster process exited', options);
});
this.on('filechange', function(file) {
return logger.debug("" + file + " was changed", options);
});
return this;
};