gpt4 book ai didi

node.js - 如何使用 Kue 每周四安排一次工作?

转载 作者:搜寻专家 更新时间:2023-10-31 22:17:51 24 4
gpt4 key购买 nike

使用 Kue ,如何安排每周四执行一次的工作? Kue readme 提到我可以延迟一个 Job,但是在特定时间重复执行 Job 呢?

我可以通过 cron 作业做我想做的事,但我喜欢 Kue 的功能。

我想要的是在星期四的任何时候处理一次作业,但只有一次。

最佳答案

我有一个类似的问题,我基本上想出了以下问题。如果其他人有不同的解决方案,我很乐意看到其他一些想法。

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文档很少,但源代码有很好的注释且易于阅读

关于node.js - 如何使用 Kue 每周四安排一次工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16867001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com