【发布时间】:2019-02-24 12:31:00
【问题描述】:
好吧,我(天真地)试图让公牛在一个sails应用程序中工作:最终我希望有一个队列,我可以根据传入的路线添加/删除/检查任务。
现在,据我了解,要创建一个在全球范围内工作的排队系统,我必须在 bootstrap.js 中添加此设置。
/**
* 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 可以正常启动,并且在路线中我可以看到 global.DirectUpdateQueue 存在。
但不起作用的是排队的任务被执行。 - 我在控制台中看不到任何日志(至少应为“开始工作”)。当我在处理函数中设置断点时,代码也不会中断。
那么这里发生了什么?
编辑:这可能是因为我没有设置(本地)redis 服务器吗? - 我没有找到有关此主题的任何信息,但我希望/希望 Bull.js 能够在内部实际处理此服务器,并且(更重要的是)不限于特定(OS)环境。
【问题讨论】:
-
您在sails.js 之外使用过bul.js 成功了吗?我根本不熟悉bul.js,但他们的文档似乎表明它依赖于redis(“要求:Bull 需要大于或等于2.8.18 的Redis 版本。”)
-
@davepreston 那么使用什么服务器软件呢?我需要诸如快递/航行传入消息之类的东西,对吗?我不能只是“运行节点”并连接到它吗?
标签: javascript sails.js scheduled-tasks bull.js