[英]Node.js: Worker with differents code in cluster?
I have a application in node.js. 我在node.js中有一个应用程序。
This application is divided in 3 parts: 本申请分为3部分:
launcher.js, which start the two other part, and restart them on crash/update after handling cleaning. launcher.js,启动另外两个部分,并在处理清理后在崩溃/更新时重新启动它们。
app.js, which work on the computer himself. app.js,自己在电脑上工作。
server.js which is used to access log and different command. server.js,用于访问日志和不同的命令。
The simplified code for launcher is: 启动器的简化代码是:
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();
}
The good thing with cluster is that they are in the same process as the launcher, so I can handle some error without having to restart the app (just calling the right function inside the app for a "soft reboot" of itself), and keep everything in the same process. 集群的好处在于它们与启动器处于同一个进程中,因此我可以处理一些错误,而无需重新启动应用程序(只需在应用程序中调用正确的函数以进行“软重启”),并保持一切都在同一个过程中。
While with exec, I m stuck with restarting the server, sometime without knowing what went wrong, and it mean having a subshell, which I dislike. 虽然有了exec,我仍然坚持重新启动服务器,有时不知道出了什么问题,这意味着有一个子shell,我不喜欢。
Is there a way to fork the cluster, but start a different code? 有没有办法分叉群集,但启动不同的代码?
My solution to this: 我对此的解决方案:
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
}
}
A more dynamic example: 一个更动态的例子:
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;
}
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();
});
}
It work for starting two different cluster, but I m stuck at restarting the app. 它适用于启动两个不同的集群,但我坚持重新启动应用程序。
EDIT: Final code, with working restart: 编辑:最终代码,重启工作:
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);
}
}
You ll need to store cluster's code in different files and start this code with the paths to the files as arguments. 您需要将群集的代码存储在不同的文件中,并使用文件路径作为参数启动此代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.