我在node.js中有一个应用程序。
本申请分为3部分:
launcher.js,启动另外两个部分,并在处理清理后在崩溃/更新时重启它们。
app.js,自己在电脑上工作。
server.js,用于访问日志和不同的命令。
启动器的简化代码是:
var cluster = require('cluster'),
exec = require('child_process').exec,
server;
if (cluster.isMaster) {
cluster.fork();
server = exec('server.js');
cluster.on('exit', function(worker, code, signal) {
//Clean corrupted data, log crash if neccessary, reload source code for update ...
cluster.fork();
});
server.on('exit', function () {
//Same as for app, with a different handling of signal...
server = exec('node server.js');
});
} else {
var self = require('app.js');
self.start();
}
群集的好处在于它们与启动器处于同一个进程中,因此我可以处理一些错误而无需重新启动应用程序(只需在应用程序内调用正确的功能以进行“软重启”) ,并将所有内容保持在同一个过程中。
虽然有了exec,我仍然坚持重新启动服务器,有时候不知道出了什么问题,这意味着有一个子shell,我不喜欢。
有没有办法分叉群集,但是启动一个不同的代码?
答案 0 :(得分:17)
我的解决方案:
var cluster = require("cluster");
if(cluster.isMaster){
// Forking Worker1 and Worker2
var worker1 = cluster.fork({WorkerName: "worker1"});
var worker2 = cluster.fork({WorkerName: "worker2"});
// Respawn if one of both exits
cluster.on("exit", function(worker, code, signal){
if(worker==worker1) worker1 = cluster.fork({WorkerName: "worker1"});
if(worker==worker2) worker2 = cluster.fork({WorkerName: "worker2"});
});
} else {
if(process.env.WorkerName=="worker1"){
// Code of Worker1
}
if(process.env.WorkerName=="worker2"){
// Code of Worker2
}
}
一个更动态的例子:
var cluster = require("cluster");
if(cluster.isMaster){
// Forking Workers based on args
if(process.argv.length < 3){
console.log("Usage: "+process.argv[1]+" module [module]");
}
process.argv.forEach(function (val, index, array) {
// Don't use this script as worker (index 1 = self)
if(index>1){
// Resolve the module before spawning to prevent loop.
try { require.resolve(val); spawn(val); }
catch(e) { console.error("Module '"+val+"' not found"); }
}
});
cluster.on("exit", function(worker, code, signal){
respawn(worker);
});
} else {
var self = require(process.env.WorkerScript);
self.start();
}
function spawn(script){
cluster.fork({WorkerScript: script}).env = {WorkerScript: script};
}
function respawn(worker){
console.log("Respawning: "+worker.env.WorkerScript)
cluster.fork(worker.env).env = worker.env;
}
答案 1 :(得分:2)
var cluster = require('cluster');
if (cluster.isMaster) {
var app = cluster.fork(),
server = cluster.fork();
app.on('message', function () {
app.send('app');
});
server.on('message', function () {
server.send('server');
});
} else {
process.send('');
process.on('message', function (code) {
var self=require('/path/to/' + code + '.js');
self.start();
});
}
它适用于启动两个不同的群集,但我坚持重新启动应用程序。
编辑:最终代码,重启工作:
var VERSION = 0.3,
util = require('util'),
cluster = require('cluster'),
PATH = process.argv[1].substr(0, process.argv[1].lastIndexOf('/') + 1),
lib = [],
childs = [];
function listen(child, i) {
child.on('message', function(m) {
if (m.type === 'REBOOT')
{
reboot();
} else if (m.type === 'CODE1') {
child.send({type: 'START', c: lib[i]});
} else {
log('ERROR', '');
}
});
child.on('exit', function(worker, code, signal) {
delete require.cache[require.resolve(PATH + lib[i])];
childs[i]=cluster.fork();
listen(childs[i], i);
});
}
function reboot() {
i = 0;
do
{
childs[i].kill();
i = i + 1;
}while (i < childs.length);
}
if (!cluster.isMaster) {
var self;
process.send({type:'START'});
process.on('message', function(m) {
if (m.type === 'START'){
self = require(PATH + m.c);
self.start();
}
});
} else {
var i = 3;
if (process.argv.length < 4)
{
log('ERROR', 'Not enought argument');
log('USAGE', 'node launcher.js x ...');
log('USAGE', '...: Apps to start (at least one)');
process.exit(-1);
} else {
do
{
lib.push(process.argv[i]);
i = i + 1;
}while (i < process.argv.length);
i = 0;
do
{
childs.push(cluster.fork());
i = i + 1;
}while(i < lib.length);
i = 0;
do
{
listen(childs[i], i);
i = i + 1;
}while(i < lib.length);
}
}
您需要将群集的代码存储在不同的文件中,并以文件的路径作为参数启动此代码。