node-cron icon indicating copy to clipboard operation
node-cron copied to clipboard

Some jobs will be skipped in some cases

Open zexee opened this issue 8 years ago • 18 comments
trafficstars

In my case, if multiple jobs are scheduled at the exactly the same time, some might be skipped. By looking at the code, I see that each task has a 1000-millisecond interval callback all the times, and at each call, if the current time matches, the job runs. Sometimes, when multiple jobs are called at the same second, and plus slow CPU or heavy task or slow nodejs or the callback happens to be at the very end of that second, some of them might skipped the second 0 (if the second is not set, it will default to 0) and they will not run. In other words, this mechanism is very unreliable.

I don't see any quick fix for this. I think the best way is the node-scheduler way, which calculate the next running time for each task and set only one callback (instead of call every second which consumes CPU).

If you are using this for serious jobs like the cron replacement in a server, I highly recommend you to switch another lib. I think the author should state this in the README since it might cause serious problems and hard to find out why.

zexee avatar Nov 08 '17 06:11 zexee

It's been a while, but I believe that we had originally written the code the way you are suggesting here. There were some issues with it and so it got reworked along with a bunch of other code in the process. I'm happy to revisit this mechanism at some point because I agree that it should be more reliable than what you are describing.

I'll keep the ticket open for the rare chance I get time and can work on it or so that someone else can jump in and pr it.

ncb000gt avatar Nov 22 '17 01:11 ncb000gt

In my opinion, this should be at least documented. I was checking why some of my cron jobs don't start, and accidentally bumped into this discussion.

sshahar1 avatar Nov 26 '17 13:11 sshahar1

I have the same issue. I added more than 20 schedule in some cases it execute and others not.

vatsal2210 avatar Feb 22 '18 18:02 vatsal2210

Same here, I have 7 jobs and randomly some jobs are sometimes skipped

MarkusPint avatar Mar 15 '18 10:03 MarkusPint

Any updates on this issue? I'm comparing between cron, node-schedule, agenda and bull. Should I drop cron because of this issue?

majidhassan avatar Sep 24 '18 14:09 majidhassan

One weird behavior I noticed is that for some reason, when the I run the following pattern:

'0 00 6 * * *' - and print out the next interval that the job will be run, it skips a week:

let nextSend = jobsCollection[this._id].cronJob.cronTime.sendAt(); let expiration = nextSend - new Date()

logger.info('execute job ${this.jobType} next run will be at: ${nextSend.toString()}); jobsBroker.executeJob(this, expiration)

Output: [job: 5ba8d193603baf0a9c8ba899 type: dashboardSubscription scheduleString: 0 00 6 * * * next run will be at: **Mon Sep 24 2018 06:00:00 GMT-0600]**

[job: 5ba8d193603baf0a9c8ba899 type: dashboardSubscription scheduleString: 0 00 6 * * * next run will be at: **Mon Oct 01 2018 06:00:00 GMT-0600]**

kbbgl avatar Oct 02 '18 14:10 kbbgl

@kbbgl are you using a specific timezone with that cron string?

jodevsa avatar Oct 06 '18 17:10 jodevsa

@jodevsa it's variable, dependent on the timezone of the client.

kbbgl avatar Oct 06 '18 20:10 kbbgl

@kbbgl It would really help in figuring out what exactly is happening.

I'm getting the following output from running this :

const CronTime = require('./cron').CronTime;
var cronTime = new CronTime('0 00 6 * * *');
var nextDate = cronTime.sendAt();

output: moment("2018-10-07T06:00:00.000")

jodevsa avatar Oct 06 '18 23:10 jodevsa

Is anyone still seeing this issue? We've since pushed a couple changes up to v1.5.0. Please review on that version. This sounds similar to GH-385, but I'm not certain at the moment and am having a hard time reproducing the code in there.

Let us know, thanks.

ncb000gt avatar Oct 29 '18 21:10 ncb000gt

I'm still having this issue.

codypace68 avatar Feb 16 '19 09:02 codypace68

@codypace68 which version of the module, node, and so forth? Also, can you share the cron syntax and a example of how you're using it?

ncb000gt avatar Feb 16 '19 16:02 ncb000gt

We have the same issue with v8.15.0 of nodejs and v1.5.0 of the lib. It is hard to reproduce I think. It seems to skip launch when the date is over (for example because an IO use the process at the time when the cron have to valid).

cedric-vinexplore avatar Feb 20 '19 09:02 cedric-vinexplore

I am missing a job once in a while as well.

MichaelLiss avatar Mar 11 '19 14:03 MichaelLiss

Had the same problem. There seems to be an issue if you start several jobs "simultaneously" (in a short period of time). In my case, I had to switch to node-cron, who works well in that scenario.

DigitalLeaves avatar Jun 12 '19 04:06 DigitalLeaves

Same issue, I going to node-schedule :(

VitorBrangioni avatar Jul 01 '19 17:07 VitorBrangioni

Any updates on that? It's been a while..

1valdis avatar Jul 24 '20 07:07 1valdis

the callback intervals should be longer than 1 second, I believe they are as long as the full time between executions unless the execution time is more than a month away. if anyone is still having this issue could you make a test case for it following the conventions here? https://github.com/kelektiv/node-cron/blob/master/tests/cron.test.js

intcreator avatar Feb 23 '23 17:02 intcreator

just did a test running 50 cron jobs that each ran every second, no cron jobs were skipped as far as I could tell. going to close this since I could not duplicate it. I'm guessing our general core algorithm hardening has fixed this issue. if anyone has a test case that can duplicate this we can reopen

intcreator avatar Dec 14 '24 03:12 intcreator