Javascript 帆船上的公牛任务不起作用?
我(天真地)尝试让bull在sails应用程序中工作:最终我希望有一个队列,我可以根据传入的路由向其中添加/删除/检查任务 现在我了解了如何创建一个全局工作的排队系统,我必须在bootstrap.js中添加这个设置Javascript 帆船上的公牛任务不起作用?,javascript,sails.js,scheduled-tasks,bull.js,Javascript,Sails.js,Scheduled Tasks,Bull.js,我(天真地)尝试让bull在sails应用程序中工作:最终我希望有一个队列,我可以根据传入的路由向其中添加/删除/检查任务 现在我了解了如何创建一个全局工作的排队系统,我必须在bootstrap.js中添加这个设置 /** * Bootstrap * (sails.config.bootstrap) * * An asynchronous bootstrap function that runs before your Sails app gets lifted. * This giv
/**
* Bootstrap
* (sails.config.bootstrap)
*
* An asynchronous bootstrap function that runs before your Sails app gets lifted.
* This gives you an opportunity to set up your data model, run jobs, or perform some special logic.
*
* For more information on bootstrapping your app, check out:
* https://sailsjs.com/config/bootstrap
*/
module.exports.bootstrap = function(done) {
// It's very important to trigger this callback method when you are finished
// with the bootstrap! (otherwise your server will never lift, since it's waiting on the bootstrap)
let Queue = require('bull');
let q = new Queue('test queue');
q.process(function(job, done){
console.log("starting job");
for(let i = 0; i<job.value; i+=1) {
console.log(i);
}
done();
});
q.add({'value':10});
global.DirectUpdateQueue = q;
return done();
};
/**
*引导
*(sails.config.bootstrap)
*
*一个异步引导函数,在你的Sails应用程序被提升之前运行。
*这使您有机会设置数据模型、运行作业或执行某些特殊逻辑。
*
*有关启动应用程序的更多信息,请查看:
* https://sailsjs.com/config/bootstrap
*/
module.exports.bootstrap=函数(完成){
//完成后触发此回调方法非常重要
//使用引导!(否则您的服务器将永远无法提升,因为它正在等待引导)
let Queue=require('bull');
设q=新队列(“测试队列”);
q、 过程(功能(工作、完成){
日志(“启动作业”);
对于(设i=0;i首先必须连接到Redis服务器)
var testQueue = new Queue('test', {
redis: {
port: 6379,
host: '127.0.0.1',
password: 'secret'
}
});
根据:
如果队列为空,作业将直接执行,否则将被放入队列并尽快执行
要访问作业中的数据,请使用job.data
对象:
testQueue.process((job) => {
console.log("job with data 'foo' :", job.data.foo);
// example with Promise
return asynchTreatment()
.then(() => { console.log('treatment ok'); })
.catch((err) => { console.log('treatment ko :', err); }
}).on('completed', (job, result) => {
// Job completed with output result!
console.log('result :', result);
});
testQueue.add({ foo : 'bar' });
编辑1:
医生说:
它创建了一个新队列,该队列在Redis中持久化。每次实例化同一队列时,它都会尝试处理以前未完成会话中可能存在的所有旧作业
因此,如果服务器重新启动,您不会丢失作业。只需在for循环中使用job.data.value即可
for(设i=0;i因此,首先,您必须确保您的服务器上安装了Redis。
创建队列时,可以传递我下面示例中的Redis config,这是默认设置
然后在bootsrap.js中:
var Queue = require('bull');
var testQueue = new Queue('Website Queue', 'redis://127.0.0.1:6379');
testQueue.process(function(job, done){
console.log('job started');
setTimeout(function () {
console.log('10 seconds later');
console.log(job.data);
}, 10000)
done();
});
global.testQueue = testQueue;
然后从“操作/控制器”可以执行以下操作:
testQueue.add({'value':10});
您在sails.js之外使用bull.js成功了吗?我对bull.js一点都不熟悉,但他们的文档似乎表明它依赖于redis(“要求:bull需要大于或等于2.8.18的redis版本”)@davepreston使用的是什么服务器软件?我需要像express/sails传入消息之类的东西,对吗?我不能只是“运行节点”连接到它?需要一个Redis服务器来使用bull。你认为bull在哪里存储队列数据?此外,自己安装一个队列数据非常简单。我希望它只是将数据保存在内存中。主要是因为如果服务器重新启动/出现故障,则需要清空队列列表-队列也应该是空的“几乎总是空的”,除了在很短的时间内突然添加了很多东西(通常是早上9点)。因此,额外的redis服务器的设置感觉有些过分。为什么要设置“额外”服务器?您至少需要一个。服务器重新启动时,队列不是空的。我编辑了我的答案