kelektiv / node-cron

Cron for NodeJS.
MIT License
8.43k stars 621 forks source link

error handling #426

Open ks-s-a opened 5 years ago

ks-s-a commented 5 years ago

Hello.

Is there a way to catch an error during the cronjob execution? As I see cron.start() doesn't return promise, so we can't just hang a .catch() there.

I guess it's common need so some people found a good approach to solve it.

Thank you a lot!

Feature specification: https://github.com/kelektiv/node-cron/issues/426#issuecomment-1741837777

magician11 commented 4 years ago

This is important.

If an error is thrown by a async function inside the cronjob callback.. then even with a try.. catch I will get

(node:8637) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
ncb000gt commented 4 years ago

Currently there isn't a way. But, I do see value in this. If I get some time I'll implement this, otherwise I'd happily review a PR for this.

Thanks for bringing it up.

tbasim commented 4 years ago

Any updates on this?

sathisa commented 4 years ago

any update on this?

chrisbrocklesby commented 4 years ago

This would be VERY handy.... +1

thiagodpf commented 4 years ago

The palliative solution I found was to retrieve the CronJob callback list (job._callbacks) and for each spawn object (child_process) within it, add an handler to the spawn 'error' event, so I can at least be notified that something has gone wrong in the child_process ...

var job = new CronJob(/* parameters for the constructor */);

/* In practice you will only have one spawn to execute at each tick */
job._callbacks.forEach((spawn) => {
    spawn.on('error', (err) => {
        console.error('Failed to start subprocess.');
    });
});

You can do the same gambling on job.onComplete, as it turns into a spawn after all.

I am not proud to share this tip, but as long as there is no other solution, it goes anyway.

NiekBeijloos commented 3 years ago

What is the status of this feature? I noticed throwing an exception from the callback function given to cron.schedule will not terminate the overall process, but it just keeps executing. Does the node cron module apply a try catch under the hood or how can this behavior be explained?

sheerlox commented 1 year ago

this feature's scope seems simple and well-defined enough:

JosephVoid commented 7 months ago

Hi @sheerlox @ncb000gt, I would like to add this feature.

sheerlox commented 4 months ago

related issue: