First time cron triggers twice.
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); });
Node 0.10.31 solves this.
So what was the problem with earlier versions of node and cron?
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.
Cool. Thanks for input! :)
Thanks for the heads up @silentjohnny, that was just throwing me for a loop!