Node Kue and Child Process - get error from spawned process

James Bund

I try to spawn a child process performing cpu intensive calculations through a job queue with Kue. My code at the moment looks like this:

consumer.js

var kue = require('kue');
var util  = require('util');
var spawn = require('child_process').spawn;

var jobs = kue.createQueue();

jobs.process('calc', 2, function(job, done){
  var work = spawn('Rscript', ['opti2.R', job.data.file]);

  work.stderr.on('data', function (data) {
    job.log('stderr: ' + data);
  });

  work.stdout.on('data', function (data) {
    job.log('stdout: ' + data);
  });

  work.on('exit', function (code, signal) {
    console.log('child process exited with code ' + code + ' with singal ' + signal);
    if(code != 0){
      done(****How to get the stderr of the child process as an error here***);
    } else {
      done(Error());
    }
  });
});

The code somewhat do what i would like it to do, but is there a better way to report the job as failed (to Kue) and get the stderr from the spawned process?

Leonid Beschastny

You can use job.log method to send data directly to Kue.

I would also recommend you to switch from .spawn to .exec, because it returns stdout and stderr as strings in its final callback along with a good error, which suits your needs well:

var exec = require('child_process').exec;

jobs.process('calc', 2, function(job, done){
  exec('Rscript opti2.R ' + job.data.file, function (error, stdout, stderr) {
    if (stdout.length > 0) job.log('stdout: ' + stdout);
    if (stderr.length > 0) job.log('stderr: ' + stderr);
    done(error);
  });
});

Though solution should work with .spawn as well: simply replace each console.log call in your code with job.log.

Though, you may want to bufferize your stderr in order to send it to Kue in one chunk:

jobs.process('calc', 2, function(job, done){
  var work = spawn('Rscript', ['opti2.R', job.data.file]);
  var stderr = '';

  work.stderr.on('data', function (data) {
    stderr += data;
  });

  work.stdout.on('data', function (data) {
    job.log(data); // sending arriving `stdout` chunks as normal log events
  });

  work.on('close', function (code, signal) {
    console.log('child process exited with code ' + code + ' with singal ' + signal);
    if(code != 0){
      done(stderr); // sending all collected stderr as an explanation
    } else {
      done();
    }
  });
});

I would also recommend using close event instead of exit, because it waits for child's stdio streams.

For more information see Event: 'exit' docs:

This event is emitted after the child process ends.

Note that the child process stdio streams might still be open.

and Event: 'close' docs:

This event is emitted when the stdio streams of a child process have all terminated.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Supervisord error "child process was not spawned"

From Dev

Unable to get output from infinitely running Ruby application spawned from Node child process

From Dev

How to get the output of a spawned child_process in Node.JS?

From Dev

How to replace node.js process with spawned child?

From Dev

Sending an Error Object from a spawned child-process over an IPC-channel

From Dev

How to get list of all child process spawned by a script

From Dev

Unable to pipe to or from spawned child process more than once

From Dev

Closing pipe does not interrupt read() in child process spawned from thread

From Dev

Node.js - spawned process is generating error "execvp(): No such file or directory"

From Dev

node child process jasmine error

From Dev

Node.js can't get output of spawned process

From Dev

mount fails from spawned process

From Java

How to get child process from parent process

From Dev

Using a timer changes the error message received from an exiting spawned process

From Dev

Get process ids of all spawned processes from xargs

From Dev

Expect: How to get the exit code from spawned process

From Dev

spawned process in node.js exiting immediately

From Dev

How to detect if a Node spawned process is still running?

From Dev

Pass a packed JS file within an asar package to a spawned node child process

From Dev

Pass a packed JS file within an asar package to a spawned node child process

From Dev

Run node as background process from script running as child process

From Dev

Run node as background process from script running as child process

From Dev

How do I preserve colors in the console when running a script from a spawned child process?

From Dev

How to kill all child processes spawned by a process started from a script on kill or kill -9

From Dev

Testing captured IO from a spawned process

From Dev

Not receiving stdout from nodejs spawned process

From Dev

What is a node child process?

From Dev

Get child process operation result from parent process

From Dev

Nodejs how to get child process from process Id

Related Related

  1. 1

    Supervisord error "child process was not spawned"

  2. 2

    Unable to get output from infinitely running Ruby application spawned from Node child process

  3. 3

    How to get the output of a spawned child_process in Node.JS?

  4. 4

    How to replace node.js process with spawned child?

  5. 5

    Sending an Error Object from a spawned child-process over an IPC-channel

  6. 6

    How to get list of all child process spawned by a script

  7. 7

    Unable to pipe to or from spawned child process more than once

  8. 8

    Closing pipe does not interrupt read() in child process spawned from thread

  9. 9

    Node.js - spawned process is generating error "execvp(): No such file or directory"

  10. 10

    node child process jasmine error

  11. 11

    Node.js can't get output of spawned process

  12. 12

    mount fails from spawned process

  13. 13

    How to get child process from parent process

  14. 14

    Using a timer changes the error message received from an exiting spawned process

  15. 15

    Get process ids of all spawned processes from xargs

  16. 16

    Expect: How to get the exit code from spawned process

  17. 17

    spawned process in node.js exiting immediately

  18. 18

    How to detect if a Node spawned process is still running?

  19. 19

    Pass a packed JS file within an asar package to a spawned node child process

  20. 20

    Pass a packed JS file within an asar package to a spawned node child process

  21. 21

    Run node as background process from script running as child process

  22. 22

    Run node as background process from script running as child process

  23. 23

    How do I preserve colors in the console when running a script from a spawned child process?

  24. 24

    How to kill all child processes spawned by a process started from a script on kill or kill -9

  25. 25

    Testing captured IO from a spawned process

  26. 26

    Not receiving stdout from nodejs spawned process

  27. 27

    What is a node child process?

  28. 28

    Get child process operation result from parent process

  29. 29

    Nodejs how to get child process from process Id

HotTag

Archive