Cluster with Express.js and Kue.js



我目前正在为自己做一个项目,需要在后台处理东西,但是,我需要在 Express 和 Kue 之间进行通信。但更多关于当前设置的信息:My Express.js 在主机内分叉了一半以上的 CPU。这将按预期工作。现在我运行另一个节点进程,它运行 kue.js 用于后台处理。由于 kue 通过 redis 调度其作业,我现在的主要问题是如何将数据从处理后的后台作业发送回我的主节点.js应用程序。

我的设置的简短概述:

app.js(与 node app.js一起运行)

var cluster = require('cluster'), Datasets = require('./models/datasets'), kue = require('kue'), jobs = cue.createQueue();
if(cluster.isMaster) {
  // Forking going on here    
} else {
  // Express.js app runs here
  app.get('/', function(req, res) {
    // Now here is where i schedule the job and i would like to retrieve an answer from the job when its done, like with on('complete', ...) but that doesn't work at all
    jobs.create('stuff', {lorem: 'ipsum', dolor: ''}).save();
  });
}

背景.js(也以节点背景运行.js,这与应用程序不同的节点进程)

// omiting libraries right now, its simply kue and datasets like in app.'s
jobs.process('stuff', function(job, done){
  //processing some background stuff here
  // Now here i would like to pass back an array of objects to the app.js process after the job is completed. 
}); 

有人对此有想法吗?感谢每一个帮助。

真诚地,克里斯平

好吧,经过几个小时的工作和测试,我自己解决了这个问题。这是我提出的解决方案:

app.js - 像您通常所做的那样分叉集群。但是,在群集的子进程中,运行后台的子进程.js

// FOrking and stuff
} else {
  var background = child_process.fork(__dirname + '/background.js');
  app.get('/', function(req, res) {
    var job = {//job stuff here, like type of job and data and stuff};
    background.send(job);
  });

后台.js - 作为群集工作线程的子进程运行

// Pretty much the same like before, but now queueing jobs and processing them too
process.on('message', function(object) {
  jobs.create(//accessing your job data here like type of job priority and data);
});

一切都按预期工作,但可能不是完美的解决方案。但是,这清楚地表明了如何在后台作业进程和应用进程之间轻松发送消息,而无需在这两个进程之间存储数据。希望这将在未来对某人有所帮助。

再见。

最新更新