Kue.JS手册

集群并行处理

下面的例子演示了如何使用集群来夸CPU分发作业进程负载.请参阅集群模块文档,使用它更详细的例子。

When cluster .isMaster the file is being executed in context of the master process, in which case you may perform tasks that you only want once, such as starting the web app bundled with Kue. The logic in the else block is executed per worker.

var kue = require('kue')
  , cluster = require('cluster')
  , jobs = kue.createQueue();

var clusterWorkerSize = require('os').cpus().length;

if (cluster.isMaster) {
  kue.app.listen(3000);
  for (var i = 0; i < clusterWorkerSize; i++) {
    cluster.fork();
  }
} else {
  jobs.process('email', 10, function(job, done){
    var pending = 5
      , total = pending;

    var interval = setInterval(function(){
      job.log('sending!');
      job.progress(total - pending, total);
      --pending || done();
      pending || clearInterval(interval);
    }, 1000);
  });
}

This will create an email job processor (worker) per each of your machine CPU cores, with each you can handle 10 concurrent email jobs, leading to total 10 * N concurrent email jobs processed in your N core machine.

Now when you visit Kue's UI in the browser you'll see that jobs are being processed roughly N times faster! (if you have N cores).