Kue:连续 processing/polling 队列的最佳实践

Kue: best practice for continuously processing/polling the queue

持续处理添加到队列中的项目的最佳方法是什么?我看到下面的方法

queue.process

https://github.com/Automattic/kue#processing-jobs

但这将处理 return。此调用后添加的项目显然未处理。

我想做的:

queue.on('job enqueue', function(id, type){
            queue.process('email', function (job, done) {
                console.log('processing one: ' + job.id);
                done(null);
            });
        });

但不确定这是否会触发忽略当前队列状态的多个处理方法?

编辑:

我已经创建了一个处理程序来监听 'email' 类型,但在下面的场景中它只被调用一次。除非我遗漏了什么,否则我希望这里的过程 运行 恰好是 10 次?

const queue = kue.createQueue();

        queue.process('email', function (job, done) {
            email(job.id, job.data, done);
        });

        var email = function(id, email, done) {
          console.log('job: %s, sent to: %s number: %s', id, email.to, email.number);
          done(null, {result: 'OK'});
        };

        queue
            .on('job enqueue', function (id, type) {
                console.log('job %s got queued of type %s with id %s', id, type);
            })
            .on('job complete', function (id, result) {
                console.log('job complete: ' + id);
            });

        for (var i = 0; i < 10; i++) {
            queue
                .create('email', {
                    title: 'welcome email for tj',
                    number: i,
                    to: 'tj@learnboost.com',
                    template: 'welcome-email'
                })
                .removeOnComplete(true)
                .save();
        }

传递给 queue.process 的函数将为每个排队的作业调用。

将其视为事件处理程序:它 "listens"(在本例中)"email" events/jobs,以及每个处理函数将被调用。所以 "items added after this call are obviously not processed".

是不正确的

根据the fine manual"by default a call to queue.process() will only accept one job at a time for processing",但您可以增加并发:

queue.process('email', 20, function(job, done){
  // ...
});

显示为每个新作业调用处理程序的一些示例代码:

const kue   = require('kue');
const queue = kue.createQueue();

queue.process('email', function(job, done){
  console.log('got job', job.data);
  done();
});

setInterval(() => {
  queue.create('email', {
    timestamp : new Date()
  }).save();
}, 1000);