使用Express.js和Kue.js的群集

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

我的设置简短的轮廓:

app.js(与节点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(); }); } 

background.js(也运行node.js节点,这不是像app一样的节点进程)

 // 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. }); 

任何人有这个想法? 每一个帮助表示赞赏。

此外,克里斯平

那么,经过几个小时的工作和testing,我自己解决了这个问题。 这是我提出的解决scheme:

app.js – 像你一样的分叉集群通常会这样做。 但是,在集群的subprocess中,运行background.js的subprocess

 // 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); }); 

background.js – 作为群集工作者的subprocess运行

 // 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); }); 

一切按预期工作,但可能不是完美的解决scheme。 但是,这明确了如何在后台作业进程和应用程序进程之间轻松发送消息,而不需要在这两者之间存储数据。 希望这将有助于未来的人。

太长。