【问题标题】:bull task in sailsjs not working?Sailsjs 中的公牛任务不起作用?
【发布时间】: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


【解决方案1】:

因此,首先,您必须确保在您的服务器上安装了 Redis。 创建队列时,您可以在下面的示例中传递 Redis 配置,这是默认设置。

然后在 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}); 

【讨论】:

    【解决方案2】:

    首先你必须连接到一个 Redis 服务器

    var testQueue = new Queue('test', {
      redis: {
        port: 6379,
        host: '127.0.0.1',
        password: 'secret'
      }
    });
    

    根据the doc

    如果队列为空,则直接执行作业,否则将被放入队列并尽快执行。

    要访问作业中的数据,请使用 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 中的新队列。每次实例化同一个队列时,它都会尝试处理之前未完成会话中可能存在的所有旧作业。

    因此,如果服务器重新启动,您不会失去工作。

    【讨论】:

    • 使用bul需要Redis服务器。您认为公牛在哪里存储队列数据?此外,自行安装也非常简单。
    • 好吧,我希望它只是将数据保存在内存中。主要是因为如果服务器重新启动/发生故障,无论如何都需要清空队列列表 - 队列也应该是“几乎总是空的”,除非在很短的时间内突然添加了很多东西(通常是 9 点钟在早晨)。所以设置一个额外的 redis 服务器感觉有点矫枉过正。
    • 为什么要“额外”的服务器?你至少需要一个。并且在服务器重新启动时队列不为空。我为此编辑了答案
    【解决方案3】:

    只需在 for 循环中使用 job.data.value

    for(let i = 0; i<job.data.value; i+=1) {
      console.log(i);
    }

    【讨论】:

      猜你喜欢
      • 2018-05-13
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 2015-10-20
      • 2016-04-20
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多