1

Traditionally I use:

if (cluster.isMaster) {
  cluster.fork();
  cluster.on('exit', function(worker, code, signal) {
    cluster.fork();
  });
}
if (cluster.isWorker) {
  ...
}

to setup workers but say if I want two servers running at different port without one server shutting down another when an error occur, what might be the method to allow such setup using cluster only.

Aero Wang
  • 8,382
  • 14
  • 63
  • 99

1 Answers1

2

For workers with different jobs to do, don't use clustering at all. Clustering is for multiple processes that share a port and incoming work is load balanced equally among all in the cluster.

If you want some other workers that do different jobs, then just use the child_process module to start your workers with something like child_process.fork(), give them different code and use any number of communication methods available to you to communicate with the workers. For example, each worker can start its own http server on its port and then you can communicate with it from the main process on that port.

Here's a simple .fork() example that illustrates using process message for communicating between the two. You can use many other communication mechanisms too.

Parent code:

const { fork } = require('child_process');

const forked = fork('child.js');

forked.on('message', (msg) => {
  console.log('Message from child', msg);
});

forked.send({ hello: 'world' });

Child code:

process.on('message', (msg) => {
  console.log('Message from parent:', msg);
});

let counter = 0;

setInterval(() => {
  process.send({ counter: counter++ });
}, 1000);
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks, I haven't use `child_process` before. Can you write an example to explain how it works? – Aero Wang Apr 17 '18 at 02:16
  • @AeroWang - If you can [read the doc](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options) and ask a more specific question, I'd know better what to help with without just copying the doc. There are also lots of examples available on Google. Here are a couple: [Everything you need to know about child_process](https://medium.freecodecamp.org/node-js-child-processes-everything-you-need-to-know-e69498fe970a) and [Understanding execfile, spawn, exec and fork in node](https://dzone.com/articles/understanding-execfile-spawn-exec-and-fork-in-node). – jfriend00 Apr 17 '18 at 02:22
  • Hi I am in China so I can't access Google. Nor can I open either of these two articles. – Aero Wang Apr 17 '18 at 02:40
  • @AeroWang - OK, I added an example, but I'd suggest you use whatever search engine you have access to and find your own articles. This is a commonly written about topic. – jfriend00 Apr 17 '18 at 02:46
  • Thanks! :) In cluster, once the process exits, for example when the master receives `process.exit(1)`, it will be forked (restarted) automatically. How can I achieve this with `child_process`? – Aero Wang Apr 17 '18 at 03:29
  • @AeroWang - Can you access [the doc](https://nodejs.org/api/child_process.html#child_process_event_exit)? The parent gets the `exit` event. That's what the cluster module does. – jfriend00 Apr 17 '18 at 03:34