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

timeout set twice on first run in Node 0.10.30

Open silentjohnny opened this issue 12 years ago • 18 comments

Since upgrading to Node 0.10.30, which now uses the uv_now instead of Date.now, I notice that CronJobs run twice the first run, 1 second apart. Before this, never saw this behaviour.

I use the following:

this.job = new cronJob({ cronTime: this.options.pattern, onTick: this.run.bind(this), start: false }); this.job.start();

Where pattern is a valid cron pattern, for testing set to 0 * * * * *, so runs every minute

When I put some logging in the Cron module, I see this:

start called timeout set to 18996 callbackwrapper called start called timeout set to 1000 <---- where does this come from? callbackwrapper called start called timeout set to 58994 callbackwrapper called start called timeout set to 59995

so, after the first two runs, job runs every minute.

If I run the same in Node 0.10.29, I get:

start called timeout set to 26996 callbackwrapper called start called timeout set to 59994

So, right away the next run is set correctly.

Any ideas?

silentjohnny avatar Aug 08 '14 14:08 silentjohnny

I'm seeing this too!

Pita avatar Aug 11 '14 18:08 Pita

Sorry I didn't respond, I was on vacation with limited access.

I'll be able to take a look at this later tonight.

ncb000gt avatar Aug 11 '14 18:08 ncb000gt

Thank you. Also btw, it seems like the version thats on npm is quite outdated

Pita avatar Aug 11 '14 21:08 Pita

Do the tests pass for you?

ncb000gt avatar Aug 12 '14 01:08 ncb000gt

what OS are you on?

with node v0.10.29 i get

job1 Mon Aug 11 2014 22:03:00 GMT-0400 (EDT)
job1 Mon Aug 11 2014 22:04:00 GMT-0400 (EDT)
job1 Mon Aug 11 2014 22:05:00 GMT-0400 (EDT)

with node v0.10.30 i get

job1 Mon Aug 11 2014 21:48:00 GMT-0400 (EDT)
job1 Mon Aug 11 2014 21:49:00 GMT-0400 (EDT)
job1 Mon Aug 11 2014 21:50:00 GMT-0400 (EDT)

ncb000gt avatar Aug 12 '14 02:08 ncb000gt

Also, the version on npm is up to date at v1.0.4.

https://www.npmjs.org/package/cron

I do need to release a new version at some point of this module though (not sure if that's what you meant).

ncb000gt avatar Aug 12 '14 02:08 ncb000gt

I saw this on heroku, can't reproduce it locally either right now.

Pita avatar Aug 12 '14 09:08 Pita

Interesting. I wonder if there is something up with Heroku then... I'll try to check it out tonight.

  • Nick Campbell

On Tue, Aug 12, 2014 at 5:30 AM, Pita [email protected] wrote:

I saw this on heroku, can't reproduce it locally either right now.

— Reply to this email directly or view it on GitHub https://github.com/ncb000gt/node-cron/issues/104#issuecomment-51892365.

ncb000gt avatar Aug 12 '14 13:08 ncb000gt

Nope, I also see it happening locally (OS X 10.9.4), but only with Node 0.10.30.

silentjohnny avatar Aug 12 '14 14:08 silentjohnny

@silentjohnny you have a working code example to reproduce the bug?

Pita avatar Aug 12 '14 14:08 Pita

var cron = require('cron');
var pattern = '*/10 * * * * *';

function run() {
    console.log('run at ' + new Date());
};

var job = new cron.CronJob(pattern, run);
job.start();

Then I also put a console log in CronTime.getTimeout():

getTimeout: function() {
    var sendAt = this.sendAt().getTime();
    var now = CronDate.now();
    var timeout = Math.max(-1, sendAt - now);
    console.log("time is " + now + ", sending at " + sendAt + ", timeout from now is " + timeout);
    return timeout;
    //return Math.max(-1, this.sendAt().getTime() - CronDate.now());
}

This gives me:

time is 1407928412120, sending at 1407928420106, timeout from now is 7986 time is 1407928419870, sending at 1407928420870, timeout from now is 1000 run at Wed Aug 13 2014 13:13:39 GMT+0200 (CEST) time is 1407928420870, sending at 1407928430870, timeout from now is 10000 run at Wed Aug 13 2014 13:13:40 GMT+0200 (CEST)

As you can see, the timeout is firing before the sendAt time, so there is a second fire right away.

It doesn't happen all the time, but it doesn't happen at all on 0.10.29

silentjohnny avatar Aug 13 '14 11:08 silentjohnny

Interesting. IIRC the change to uv_now happened in v0.10.29.

Can you pull master and try it to see if it happens for you with the latest in github? Thanks.

  • Nick Campbell

On Wed, Aug 13, 2014 at 7:24 AM, Joris Verbogt [email protected] wrote:

var cron = require('cron');var pattern = '*/10 * * * * *'; function run() { console.log('run at ' + new Date());}; var job = new cron.CronJob(pattern, run);job.start();

Then I also put a console log in CronTime.getTimeout():

getTimeout: function() { var sendAt = this.sendAt().getTime(); var now = CronDate.now(); var timeout = Math.max(-1, sendAt - now); console.log("time is " + now + ", sending at " + sendAt + ", timeout from now is " + timeout); return timeout; //return Math.max(-1, this.sendAt().getTime() - CronDate.now());}

This gives me:

time is 1407928412120, sending at 1407928420106, timeout from now is 7986 time is 1407928419870, sending at 1407928420870, timeout from now is 1000 run at Wed Aug 13 2014 13:13:39 GMT+0200 (CEST) time is 1407928420870, sending at 1407928430870, timeout from now is 10000 run at Wed Aug 13 2014 13:13:40 GMT+0200 (CEST)

As you can see, the timeout is firing before the sendAt time, so there is a second fire right away.

It doesn't happen all the time, but it doesn't happen at all on 0.10.29

— Reply to this email directly or view it on GitHub https://github.com/ncb000gt/node-cron/issues/104#issuecomment-52036404.

ncb000gt avatar Aug 13 '14 13:08 ncb000gt

I should note that I'm also testing a few things locally. We'd setup some code to allow stopping of jobs before the timeouts change. That is why getTimeout is called before the actual callback is fired. But, I'm not seeing any issues regarding duplication (as in this is by design and should happen every time). If you want the job to stop you can call job.stop() or just specify an exact date/time.

I'll see what I can do about changing the behavior, but I'm not sure this is going to change.

ncb000gt avatar Aug 13 '14 14:08 ncb000gt

The uv_now was introduced in 0.10.30. There are regression issues (and a fix) with more timer related problems, so this might be solved in 0.10.31.

I used the latest npm which seems to be the latest master, but I also tried with master clone, same results.

What I see is that it only happens when a job fires in the second before the actual timeout, so in my example:

sending at 1407928420106

leads to an actual run at 1407928419870, which is still in the 39th second of the minute.

And my guess is that's where the extra fire 1000ms later comes from.

Maybe it's better to wait for 0.10.31 today and hope this resolves the problem.

silentjohnny avatar Aug 13 '14 14:08 silentjohnny

Ahh right, yea. I see what you mean. Agreed- let's see what 0.10.31 brings.

ncb000gt avatar Aug 13 '14 14:08 ncb000gt

I'm experiencing this behaviour aswell... I opened a issue for my problem, only to find this issue has already been opened here.

I can add that my first double triggers seems to happen randomly, a larger file, or more requirement seems to affect. A lighter file with no requirements seems to be free from this problem almost always.

a00andos avatar Aug 15 '14 12:08 a00andos

What actually happens is the regression issue in Node 0.10.30:

In certain circumstances (especially under CPU load) timers (e.g., set with setTimeout) fire before the actual timeout. This might just be 100ms, which shouldn't be a problem in most situations, but in the case of CronJob, it is. Cron runs assume the timeout has already passed (at least 0 ms), otherwise it will set another timeout right away 1 second (+ the ms it was fired too soon) later. This is the behaviour we see.

So, either go back to 0.10.29 or wait for the fix in 0.10.31

silentjohnny avatar Aug 15 '14 12:08 silentjohnny

Node 0.10.31 solves this bug.

silentjohnny avatar Aug 19 '14 23:08 silentjohnny