Files
iblog2/daemon.js

28 lines
889 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
var worker;
//遍历CPU核心数
for (var i = 0; i < numCPUs; i++) {
//生成新的工作进程运行主模块
worker = cluster.fork();
console.log('worker%d 正在运行...', worker.process.pid);
}
} else {
//运行主模块
require('./bin/www');
}
//监听退出事件
cluster.on('exit', function (worker, code, signal) {
if (code !== 0) {
console.error('worker%d 异常退出(%s30s后尝试重启...', worker.process.pid, signal || code);
setTimeout(function () {
var new_worker = cluster.fork();
console.log('worker%d 正在运行...', new_worker.process.pid);
},30000);
} else {
console.log('worker%d 正常退出!', worker.process.pid);
}
});