2013-10-23 50 views
1

我目前正在爲我自己的項目需要處理的東西在後臺,但是,我需要溝通Express和Kue。 但是,有關當前設置的更多信息: 我的Express.js佔用了主機內部一半以上的CPU。這按預期工作。現在我運行另一個運行kue.js進行後臺處理的節點進程。由於kue通過redis調度它的工作,我現在的主要問題是我如何能夠將數據從已處理的後臺作業發送回我的主要node.js應用程序。集羣與Express.js和Kue.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(與節點background.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. 
}); 

任何人都有這樣的想法?每一個幫助表示讚賞。

誠懇, 克里斯賓

回答

2

好了,解決了我自己的問題,工作和測試的幾個小時後。這是我想出的解決方案:

app.js - 像你一樣會分叉的羣集。然而,在集羣的子進程,運行background.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); 
    }); 

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

一切工作按預期的子進程,但可能不是完美的解決方案。但是,這清楚地表明瞭如何在後臺作業進程和應用程序進程之間輕鬆發送消息,而無需在這兩者之間存儲數據。希望這將有助於未來的人。

這麼長。