简体   繁体   English

从master(Node.js集群)访问工作线程环境

[英]Access worker environment from master (Node.js Cluster)

I fork workers in my Node.js application via the Cluster module and pass a custom ID to the environment of all my workers. 我通过Cluster模块在我的Node.js应用程序中分工,并将自定义ID传递给我所有工作人员的环境。 That works well for far. 到目前为止效果很好。

However, I do not know how I can access this id in my master when an 'online' or 'exit' event is emitted. 但是,当发出“在线”或“退出”事件时,我不知道如何在我的主人中访问此ID。

The documentation is not very helpful. 文档不是很有帮助。 Could you please point me the right way? 你能指点我正确的方法吗?

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
}

theres no way, the worker process env is not exposed to the master. 没有办法,工作进程env不会暴露给主人。

One aproach can be a map of our cluster (a object containig the needed info). 一个aproach可以是我们的集群的映射(一个对象包含所需的信息)。

Something like these: 像这样的东西:

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.
}

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

相关问题 Node.js集群架构:如何扩展主工作者 - Node.js Cluster architecture: how to scale master worker 'exit' - 在node.js集群中从master中杀死的工作进程中的事件 - 'exit'-Event in worker process when killed from master within a node.js cluster Node.js:在集群中具有不同代码的工作者? - Node.js: Worker with differents code in cluster? node.js集群:让每个工作人员呈现不同的页面 - node.js cluster: have each worker render different page 如何使用 Node.js 集群将消息(对象)从 worker 发送到 Master(主) - How to send a message (object) from workers to Master (primary) with Node.js Cluster Node.js |工作进程:如何验证主进程和工作进程之间的双向通信 - Node.js|Worker Processes: How to verify two-way communication between a master and worker process Node.js集群 - Node.js Cluster Node.js - cluster.fork() - 如何检查工作人员中的工作人员数量? - Node.js - cluster.fork() - how to check number of worker within the worker? 在node.js中,如何声明一个可以被master进程初始化并被worker进程访问的共享变量? - In node.js, how to declare a shared variable that can be initialized by master process and accessed by worker processes? 为什么在Nginx中增加worker_connections会使node.js集群中的应用程序变慢? - Why increasing worker_connections in Nginx makes the application slower in node.js cluster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM