【问题标题】:Access worker environment from master (Node.js Cluster)从 master 访问 worker 环境(Node.js 集群)
【发布时间】:2014-02-19 12:04:01
【问题描述】:

我通过集群模块在我的 Node.js 应用程序中派生工作人员,并将自定义 ID 传递给我所有工作人员的环境。这很有效。

但是,当发出“在线”或“退出”事件时,我不知道如何在我的主服务器中访问此 ID。

文档不是很有帮助。你能指点我正确的方式吗?

var cluster = require('cluster'); 

if (cluster.isMaster) {
  //MASTER

  function fork() {
    var worker_env = {worker_id:'my_custom_id'};
    cluster.fork(worker_env);
  }                 

  cluster.on('online', function(worker) {
    console.log(worker.process.env.worker_id); // undefined
    //
    // How can I access my custom worker id here?
    //
  });

  cluster.on('exit', function(worker, code, signal) {
    //
    // And here...?
    //
    fork();
  });

} else {
  // WORKER

  console.log(process.env.worker_id); // my_custom_id
}

【问题讨论】:

    标签: javascript node.js cluster-computing env


    【解决方案1】:

    没办法,worker进程env没有暴露给master。

    一种方法可以是我们集群的地图(包含所需信息的对象)。

    类似这样的:

    var cluster = require('cluster');
    
    if (true === cluster.isMaster) {
      //CODE EXECUTED BY MASTER
      var cluster_map = {}; // Here we store the workers info in a object   
      var restart_Limit = 10; // max global worker restart (10)
    
      function fork_worker(myWorkerId) {
        // these makes worker_id available in the worker
        var worker = cluster.fork({
          worker_id: myWorkerId 
        });
        // max restarts limit (global)
        if (worker.id >= restart_Limit) { 
          console.log('Restart limit reached, bye!');
          process.kill();
    
        }
        // here we add the key "myWorkerId"  to the cluster map
        cluster_map[worker.id] = myWorkerId;
    
        // WORKER AUTO-KILL
        setTimeout(function() {
          console.log('stoping...' + myWorkerId);
          worker.kill();
        }, 3000);
      }
    
      cluster.on('online', function(worker) {
        var online_proc = cluster_map[worker.id];
    
        console.log('worker online: ' + online_proc + '\n Restarts: ' + worker.id);
      });
    
      cluster.on('exit', function(worker, code, signal) {
    
        var exited_proc = cluster_map[worker.id];
    
        // delete the process from the cluster map
        delete cluster_map[worker.id];
        console.log("worker offline: " + exited_proc);
    
        // WORKER AUTO-RESTART
        setTimeout(function() {
          console.log('Restarting... ' + exited_proc);
          fork_worker(exited_proc);
        }, 3000);
    
      });
    
      // start the magic ( 3 workers )
      (function() {
        fork_worker('id_1');
        fork_worker('id_2');
        fork_worker('id_3');
      })();
    
    } else {
      //CODE EXECUTED BY EACH WORKER (process env is present here).
      console.log('hi from the worker,  process.env: ' + process.env.worker_id);
      // all the hard work for the workers here.
    }
    

    【讨论】:

    • 谢谢你,也为例子。会这样做。
    猜你喜欢
    • 2020-05-25
    • 2020-04-05
    • 1970-01-01
    • 2020-12-21
    • 2021-08-24
    • 2018-03-07
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多