D
D
drowzeenico2016-11-28 13:28:57
Node.js
drowzeenico, 2016-11-28 13:28:57

How to stop a process in node.js?

Good day to all.
There is a master node process that runs several children. Each of the children is a parser that receives data from the master. Everything works fine, but I noticed one feature - when the parsing is finished, the child process is not closed. I struggled with the problem for a day and found out that the node does not close if there are unfinished handlers on the stack. I began to dig what I have hanging in the stack so far. And I found that this is the handler for incoming messages from the master process:

if (cluster.isMaster) {

  for (var i = 0; i < numCPUs; i++) {
    let worker = cluster.fork();
    worker.send(i);
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} has finished his job.`);

    numberOfProcesses--;
    if(numberOfProcesses <= 0) {
      console.log(`Parsing was completed.`);
      process.exit(0);
    }
  });


} else {
  process.once('message', msg => {
    // именно вот из-за этого обработчика процесс продолжает работать
    console.log(process.pid, msg)
  })
}

Even when "once" is specified, that is, get the message and reset the handler. Still doesn't finish the job.
And now the question is why is it not closing and how to kill the child process?
ps: just don't suggest process.exit() because the process has to wait for all tasks to complete.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2016-11-28
@drowzeenico

We will still have to offer process.exit ()
Handlers are hung on the process, so it is not going to end itself.
You need to call after the completion of all tasks - Promise to help.
It is unlikely to succeed otherwise.

P
pomeo, 2016-11-28
@pomeo

Is child_process.exec good for you?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question