我使用node
+ express
作为我的网络服务器,cluster
为4 workers
。
我尝试了几种方法来故意杀死worker
:
process.exit()
在控制器中,并使用浏览器操作触发。认为这仅适用于单个工人流程,但结果发现所有工人都被杀死了。
再次在控制器中,我让worker
向master
发送自杀公告:
process.send('suicide');
这是我的主要过程:
if (cluster.isMaster) {
console.log(`Master cluster setting up ${numWorkers} workers...`);
for (let i = 0; i < numWorkers; i++) {
const worker = cluster.fork();
worker.on('message', msg => {
console.log(worker.process.pid + ' wants to suicide');
worker.kill();
process.kill(worker.process.pid);
});
}
}
事实证明,worker.kill()
根本没有影响,process.kill(worker.process.pid);
再次杀死了所有4名工人。此外,console.log
出现了4次,我不明白。我使用浏览器触发一些因此触发自杀公告的行动,这不应该是单个工人的行为吗?
我也在项目中使用WebSockets
并保持联系,不知道这是否重要。任何帮助表示赞赏!
修改
感谢@Mia我找到了原因:当我将process.exit()
放入else
语句(当cluster.isWorker
时)时它工作正常,但当放入特定控制器时,它会转出去影响所有工人。不知道怎么解决。控制器不应该只影响一个特定的工作人员吗?
答案 0 :(得分:3)
我很抱歉写在这里,由于我的声誉,我无法发表评论。 我写的如下,它工作正常。 每当集群获得请求app.get(&#34; /&#34;)时,就会创建第一个集群并打开服务器,2秒后它会被process.exit方法退出。 您可以通过打印剩余的工人来确认这种方法很有效。
'use strict';
const cluster = require('cluster');
const os = require('os');
if (cluster.isMaster) {
const cpus = os.cpus().length;
for (let i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code) => {
console.log(`${worker.process.pid} is killed`);
console.log("remaining wokers");
const workers = Object.keys(cluster.workers);
for(let worker of workers){
console.log(cluster.workers[worker].process.pid);
}
});
} else {
console.log("process id ",process.pid);
const express = require("express");
const app = express();
const path = require("path");
const fs = require("fs");
const http = require('http').Server(app);
const io = require('socket.io')(http);
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static('public'));
app.get('/',function(req,res){
console.log("connected via" , process.pid);
setTimeout(()=>{
process.exit();
},2000)
res.sendFile(path.join(__dirname+"/public/view/index.html"));
});
http.listen(3000);
}
----------------------------代码分成模块---------------
Root(server.js)
'use strict';
const cluster = require('cluster');
const os = require('os');
if (cluster.isMaster) {
const cpus = os.cpus().length;
for (let i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code) => {
console.log(`${worker.process.pid} is killed`);
console.log("remaining wokers");
const workers = Object.keys(cluster.workers);
for(let worker of workers){
console.log(cluster.workers[worker].process.pid);
}
});
} else {
console.log("process id ",process.pid);
const express = require('./modules/express');
const http = require('http').Server(express);
http.listen(3000);
}
快速
const express = require("express");
const app = express();
const bodyParser = require('body-parser');
const route = require('../route');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static('public'));
route(app);
module.exports = app;
路线(index.js)
const HomeController = require('../controllers/HomeController');
module.exports = function(app){
app.get('/',HomeController.renderIndex);
app.get('/killProcess',HomeController.killProcess);
}
的HomeController
const path = require('path');
exports.renderIndex = function(req,res){
console.log("connected via" , process.pid);
res.sendFile(path.join(__dirname+"/../public/view/index.html"));
}
exports.killProcess = function(req,res){
res.write(`current process ${process.pid} is killed`);
res.end();
process.kill(process.pid);
}
客户端(浏览器)
$("#kill").on("click",function(e){
$.ajax({
url:"/killProcess"
}).success(function(data){
console.log(data);
})
})