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

First time cron triggers twice.

Open a00andos opened this issue 12 years ago • 5 comments

First time node-cron triggers it sometimes (most of the times...) insert an extra trigger one second before it was suppost to trigger... that means it triggers twice the first time. This causes some problem.

then is trigger once every minute, like it should.

Here is my complete code...

var express = require('express'); var app = express(); var cronJob = require('cron').CronJob;

var path = require('path'); var crypto = require('crypto'); var nodemailer = require('nodemailer'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var session = require('express-session'); var MongoStore = require('connect-mongo')(session); var bodyParser = require('body-parser'); var mongo = require('mongoskin'); var Joi = require('joi'); var _ = require('underscore');

new cronJob('00 * * * * *', function(){ console.log('Cron is triggering!'); console.log('The time right now is: ', new Date().getHours(),':',new Date().getMinutes(),':',new Date().getSeconds()); }, null, true, null);

app.listen('3000', function(){ console.log('Express server listening on port 127.0.0.1:3000'); console.log('Enviroment is set to: ' + app.settings.env); });

a00andos avatar Aug 15 '14 09:08 a00andos

Node 0.10.31 solves this.

silentjohnny avatar Aug 19 '14 23:08 silentjohnny

So what was the problem with earlier versions of node and cron?

ralphtheninja avatar Aug 22 '14 16:08 ralphtheninja

This was a problem caused by a regression bug in Node 0.10.30, where a new (and faster) method for getting the current time was implemented. However, this could lead to circumstances, especially under CPU load, where timeouts would fire a couple of tens of milliseconds to soon. Not a huge problem normally, but for CronJobs, it would set a new timeout immediately, leading to the "next run" being set for, say, 25 milliseconds ahead + 1000ms that are added to make sure timeout is never 0.

This was solved in Node 0.10.31 and now timers will always be guaranteed to fire on or after the given timeout.

silentjohnny avatar Aug 22 '14 16:08 silentjohnny

Cool. Thanks for input! :)

ralphtheninja avatar Aug 22 '14 17:08 ralphtheninja

Thanks for the heads up @silentjohnny, that was just throwing me for a loop!

wavded avatar Aug 25 '14 18:08 wavded