从Resque队列中提取时如何实现同步行为?

时间:2017-08-03 06:59:36

标签: node.js redis resque

我有一个案例,我使用'queue'将一些消息排入队列。现在我想连续提取这些消息。我正在使用'worker',但它以异步方式触发所有消息,同时为每条消息调用我的回调函数。有没有办法可以控制工作者只提取一条消息或串行调用一些去队列消息?

// Code enqueuing 3 messages...
var queue = new NR.queue({connection: connectionDetails});

queue.enqueue(config.Redis.resquequeuename, config.Redis.resquequeuejob, {
message_id: 1,
message_payload: "this is a test body of my message"
});
queue.enqueue(config.Redis.resquequeuename, config.Redis.resquequeuejob, {
message_id: 2,
message_payload: "this is a test body of my message"
});
queue.enqueue(config.Redis.resquequeuename, config.Redis.resquequeuejob, {
message_id: 3,
message_payload: "this is a test body of my message"
});

// Code using worker to dequeue messages
var GetQueueMessage = function(appcallback){

var job = {};
job[config.Redis.resquequeuejob] = {
plugins: [],
pluginOptions: {},
perform: function(compositemessage, callback){
callback(null, appcallback(null, compositemessage));
}
};

var workername = os.hostname() + ":" + process.pid;
var worker = new NR.worker({connection: connectionDetails, queues: [config.Redis.resquequeuename], 'name': workername}, job);
worker.connect(function(){
worker.workerCleanup();

console.log("Worker starting, Resque worker. Worker name: "+ workername);

worker.start();
});
// different even handlers for worker here...
};

workerGetQueueMessage(function(err, pCompositeMessage){
console.log("Composite Message returned from Worker: " + JSON.stringify(pCompositeMessage));
console.log(new Date(dt.now()));
});

0 个答案:

没有答案