2013-10-23 1 views
1

현재 자신을 위해 백그라운드에서 물건을 처리해야하는 프로젝트를 진행하고 있지만 Express와 Kue 사이에서 통신해야합니다. 하지만 현재 설정에 대해 좀 더 자세히 설명해 드리겠습니다. Express.js가 호스트 컴퓨터의 CPU 절반 이상을 포크합니다. 이것은 의도 한대로 모든 것을 작동시킵니다. 이제 백그라운드 처리를 위해 kue.js를 실행하는 다른 노드 프로세스를 실행합니다. kue가 redis를 통해 작업을 예약하기 때문에 주요 문제는 처리 된 백그라운드 작업의 데이터를 메인 node.js 앱으로 다시 보낼 수있는 방법입니다.Express.js 및 Kue.js가 포함 된 클러스터

내 설치의

짧은 개요 :이 같은 노드

app.js는 (노드 app.js 실행) 노드 background.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 (아니다 응용 프로그램과 같은 과정)

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

모든 것이 의도 한대로 작동하는 클러스터 노동자의 자식 프로세스로 실행하지만, 완벽한 해결책이 아닐 수도 있습니다. 그러나 백그라운드 작업 프로세스와 응용 프로그램 프로세스간에 메시지를 쉽게 보내고이 두 프로세스간에 데이터를 저장할 필요가없는 것을 분명히합니다. 희망이 미래에 누군가를 도울 것입니다.

너무 오래.

관련 문제