I'm using node for server side processing - going through big data files, and inserting into the db. This will be run nightly on a cron job. In the program have several tasks that need to be accomplished. I have set up child_process
so that it does these distinct tasks correctly. However, I would like to have one error handling section for the whole thing. If any step goes wrong, I would like to have an email sent to me so that I know. When I saw this I thought that it would catch errors in the child processes and allow me to send an email with the problem.
process.on('error', function (err) {
console.log("Error running Scripts")
sendErr(err);
});
However I quickly found out that this only catches errors in spawning the process, not in the actual execution. But then I found the uncaughtException
event, and replaced error
in the code above with uncaughtException
. Unfortunately, it did not work as I would have hoped, and the code is not run, and the email is not sent. Does anybody know how to do this, or see what i am doing wrong. Here is my current code
var process = childProcess.fork(scriptName, {cwd:scriptWorkingDirectory});
process.on('uncaughtException', function (err) {
console.log("Error running Scripts")
sendErr(err);
});
You must do process.on('uncaughtException', ...)
in the child processes and not in the parent.
Parent can't listen to errors in child process.
You can listen in the parent to 'end'
event of the child to see if the child ended but you can't tell if it ended ok, only based on exit code.
So in child listen to the uncaughtException
and than you can send a message from the child process to the parent process via subprocess.send
the parent process can than send the mail as you like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With