我需要定期更新一个文件,这个文件会给我其他预定的任务。
我唯一的问题是使用cron节点
在这里,测试代码(真正的代码,缩小到cron问题,并带有一些测试行):
//Variable of the dayly update hour, with default value
var UPDATE_H=6,
UPDATE_M=30;
function start(){
console.log('start');
//test_
//Set the cron job at the next two minute following the start of the app
time=new Date();
time.setMinutes(time.getMinutes()+2);
UPDATE_H=time.getHours();
UPDATE_M=time.getMinutes();
//_test
//Start the cron job
smil_update(UPDATE_H, UPDATE_M);
//test_
//Print a dot every minutes
setInterval(function(){console.log('.');}, 60000);
//_test
player();
}
function smil_update(hour, min){
var cronJob=require('cron').CronJob,
when='',
//test_
time=new Date();
//_test
//Creating cron like date struct (cron like because seconds count too here)
when='00 '+min+' '+hour+' * * *';
console.log('Will work at:'+when);
//test_
console.log('It is '+time.getHours()+' '+time.getMinutes());
//_test
var job=new cronJob(when, timeZone='Europe/Paris', function(){
console.log('UPDATE');
player();
});
job.start();
}
function player(){
console.log('Player');
}
start();
我得到了:
start
Will work at:00 13 17 * * *
It is 17 11
Player
.
.
.
我似乎在某个地方漏掉了一点,但即使在重读了这些文档之后,我也不明白我做错了什么。
发布于 2013-09-11 07:36:16
这样啊,原来是这么回事。
错误在时间变量中。
而不是
when='00 '+...
一定是
when='0 '+...
似乎因为某种未知的原因,cron没有将00识别为0。
https://stackoverflow.com/questions/18744990
复制相似问题