如何使用Kue每个星期四安排一次工作? [英] How to schedule a job once every Thursday using Kue?
问题描述
使用 Kue ,我该如何安排每个星期四执行一次作业? Kue自述文件提到我可以延迟Job,但是在特定时间重复执行Job怎么办?
Using Kue, how do I schedule a job to be executed once every Thursday? The Kue readme mentions that I can delay a Job, but what about repeatedly executing the Job at a specific time?
我可以通过Cron来做我想做的事,但是我喜欢Kue的功能.
I can do what I want with a cron job, but I like Kue's features.
我想要的是在星期四的任何时候处理一次作业,但是只能处理一次.
What I want is to process a Job once anytime on Thursday, but only once.
推荐答案
我有一个类似的问题,基本上我提出了以下问题.如果其他人有不同的解决方案,我很乐意看到其他一些想法.
I had a similar question and I basically came up with the following. If anyone else has a different solution I would love to see some other ideas.
var jobQueue = kue.createQueue();
// Define job processor
jobQueue.process('thursday-jobs', function (job, done) {
var milisecondsTillThurs = // TODO: Get the time until next thursday. For this I used moment.js
// do this job again next Thursday
jobQueue.create('thursday-jobs').delay(milisecondsTillThurs).save();
// For Example purpose this job waits then calls done
setTimeout(function () {
done();
}, 10000);
});
// Use some initialization code to check if the job exists yet, and create it otherwise
kue.Job.rangeByType('thursday-jobs','delayed', 0, 10, '', function (err, jobs) {
if (err) {return handleErr(err);}
if (!jobs.length) {
jobQueue.create('thursday-jobs').save();
}
// Start checking for delayed jobs. This defaults to checking every 5 seconds
jobQueue.promote();
});
Kue 的文档很少,但是源代码的注释很好并且易于阅读
Kue has minimal documentation, but the source is well commented and easy to read
这篇关于如何使用Kue每个星期四安排一次工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!