简体   繁体   English

使用node.js集群时,如何在工作者死亡时访问它?

[英]When using node.js cluster, how to access a worker's environment when it dies?

I'm using node.js cluster module to create worker processes. 我正在使用node.js集群模块来创建工作进程。 And I set a custom variable in each worker's environment as I fork it. 我在fork它时在每个worker的环境中设置一个自定义变量。

I need to read that custom variable when a worker dies, but when a worker dies, I can't access its environment object anymore. 我需要在工作者死亡时读取该自定义变量,但是当工作者死亡时,我无法再访问其环境对象。

This is what I tried so far: 这是我到目前为止所尝试的:

var cluster = require('cluster'),
    os = require('os');

if (cluster.isMaster) {

    cluster.on('exit', function (worker, code, signal) {

        console.log('worker ' + worker.process.pid + ' died');

        var x = {
            workerId: worker.process.env.workerId // This is undefined.
        };
        cluster.fork(x);
    });

    for (var i = 0; i < os.cpus().length; i++) {
        var x = {
            workerId: i
        };
        cluster.fork(x);
    }

}
else {
    console.log("workerId: ", process.env.workerId);

    // simulate an exeption:
    throw "fakeError";

}

I know that's not gonna work, my question is: how to access to latest state of a worker's envoronment right before its death? 我知道这不会起作用,我的问题是:如何在死亡之前获得工人的最新状态?

It seems that env only set in worker's process and is not accessible in master. 似乎env只设置在worker的进程中,并且无法在master中访问。 Master only have primitive information about workers process. 大师只有关于工人过程的原始信息。 You can do what you want like: 你可以做你想做的事:

// Fork workers.
for (var i = 0; i < numCPUs; i++) {
    var env = {workerId: i},
        newWorker = cluster.fork(env);
    newWorker.process.env = env;
}

cluster.on('exit', function (worker, code, signal) {
    console.log('worker ', worker.process.env.workerId, ' died');
    var env = worker.process.env,
        newWorker = cluster.fork(env);
    newWorker.process.env = env;
});

I hope it helps you. 我希望它对你有所帮助。 Whenever workers change their env, they should send message to master and inform it about those changes, so, master can update its information. 每当工作人员更改他们的环境时,他们应该向主人发送消息并告知其有关这些更改,因此,主人可以更新其信息。

You can subscribe to the exit event of the cluster and you will be notified when any worker in the cluster dies. 您可以订阅群集的exit事件,当群集中的任何工作者死亡时,您将收到通知。 At that point you can do whatever you want. 那时你可以做任何你想做的事。

Code Snippet picked directly from Documentation 代码片段直接从文档中选取

cluster.on('exit', function(worker, code, signal) {
      console.log('worker %d died (%s). restarting...',
        worker.process.pid, signal || code);
      cluster.fork();
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从master(Node.js集群)访问工作线程环境 - Access worker environment from master (Node.js Cluster) 将 node.js 作为作业工作者运行时是否需要集群? - is cluster for node.js required when running it as a job worker? 在node.js中,如何请求或访问群集中的特定工作程序(具有进程ID或工作程序号)? - In node.js how to request or access specific worker in cluster (with either process ID or worker number)? Node.JS 子进程在父进程死亡时被杀死 - Node.JS child processes being killed when parent dies 'exit' - 在node.js集群中从master中杀死的工作进程中的事件 - 'exit'-Event in worker process when killed from master within a node.js cluster Node.js集群架构:如何扩展主工作者 - Node.js Cluster architecture: how to scale master worker “集群”和“worker_threads”在 Node.js 中如何工作? - How do 'cluster' and 'worker_threads' work in Node.js? 使用 PM2 在集群模式下运行 node.js 应用程序时,如何通过命令行加载环境变量? - How can I load environment variables via command line when running a node.js app in cluster mode with PM2? Node.js集群-检测工作人员卡住了吗? - Node.js Cluster - detect worker stuck? Node.js:在集群中具有不同代码的工作者? - Node.js: Worker with differents code in cluster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM