javascript node.js 中的 Cronjobs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5636051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cronjobs in node.js
提问by Thomas
Is there a possibility to periodically call functions at a specific time of the day in node.js? My first implementation was
是否可以在 node.js 中的特定时间定期调用函数?我的第一个实现是
setInterval(functionName(),(24*60*60*1000));
This is suboptimal, because it will restart every time I restart node. Are there better possibilities to implement this?
这是次优的,因为每次我重新启动节点时它都会重新启动。是否有更好的可能性来实现这一点?
回答by Raynos
To guard against restarts you need to create a persistent data stored job.
为了防止重启,您需要创建一个持久的数据存储作业。
Something like :
就像是 :
Job = {
_id: Number,
job: String,
dueDate: Date,
completed: Boolean
}
Then have some code as follows:
然后有一些代码如下:
var createJob = function(url, date) {
var j = db.create(Job, function(j) {
j.job = url;
j.dueDate = date;
j.save();
});
};
var runJob = function(j) {
var id = j._id;
setInterval(j.dueDate - Date.now(), function() {
db.getOne(Job, { _id : id }, function(j) {
require(j.job);
j.finished = true;
j.save();
});
});
j = null;
};
On start up you just have to do something like :
在启动时,您只需要执行以下操作:
db.get(Job, { finished: false }, function(jobs) {
jobs.forEach(runJob);
});
Replace the generic db
with MongoDB, CouchDB, Redis, etc.
db
用 MongoDB、CouchDB、Redis 等替换泛型。
回答by Talha Awan
It's an old question, but in addition to the accepted answer a library with examples is worth mentioning.
这是一个古老的问题,但除了公认的答案之外,值得一提的是带有示例的库。
cronis a lightweight package which runs the specified function at given interval, using only system's time and no db persistence.
cron是一个轻量级的包,它以给定的时间间隔运行指定的函数,只使用系统时间而不使用数据库持久性。
const CronJob = require('cron').CronJob;
const jobFiveMinutes = require("./job.five-minutes");
const jobMondayMorning = require("./job.monday-morning");
var jobs = [
new CronJob({
cronTime: "00 */5 * * * *", //every five minutes
onTick: function() {
jobFiveMinutes();
},
start: false, //don't start immediately
timeZone: 'America/Los_Angeles'
}),
new CronJob({
cronTime: "00 00 9 * * 1", //9 am Monday morning
onTick: function() {
jobMondayMorning();
},
start: false,
timeZone: 'America/Los_Angeles'
}),
];
jobs.forEach(function(job) {
job.start(); //start the jobs
});
Above, we required two files and call them inside of two cronjobs, set at different intervals. The files simply export the functions:
上面,我们需要两个文件,并在两个 cronjob 中调用它们,以不同的间隔设置。这些文件只是导出函数:
//job.five-minutes.js
module.exports = function(){
console.log("runs every five minutes")
};
//job.monday-morning.js
module.exports = function(){
console.log("runs every monday morning at 9 am, Los Angeles time")
};
Whether you run it locally or on remote server in any region, it will run according to the passed timezone (which is optional though, and does not matter in case of minutes).
无论您是在本地还是在任何地区的远程服务器上运行它,它都会根据传递的时区运行(尽管这是可选的,并且在分钟的情况下无关紧要)。
Also, restarting the server/script will have no affect on its working as it's synced with system time. A "00 */5 * * * *"
job will run on each multiple of 5 i.e. 5, 10, 15, 20 and so on. So, even if you restart the script at 24, it will run at 25 not 29.
此外,重新启动服务器/脚本不会影响其工作,因为它与系统时间同步。甲"00 */5 * * * *"
作业将上的5即5,10,15,20等的每个的多个运行。因此,即使您在 24 点重新启动脚本,它也会在 25 点而不是 29 点运行。
Lastly, the package has extended the cron syntax to include seconds on the left most. Therefore, you can even tell at what exact second of the minute you want to run the job.
最后,该软件包扩展了 cron 语法以在最左侧包含秒数。因此,您甚至可以知道要在哪一分钟的哪一秒运行作业。