1
以下功能將接收來自sqs
的多條消息。必須處理每條消息並相應地更新數據庫。如何處理來自SQS的多個消息?
我可以通過調用worker
模塊中的pull
函數來處理單個消息。但如何處理多個消息?我無法繼續調用worker
模塊的pull
方法,因爲它會阻塞該線程。這裏可能的最佳方式是什麼?從Worker
模塊
function checkMessage(){
var params = {
QueueUrl : Constant.QUEUE_URL,
VisibilityTimeout: 0,
WaitTimeSeconds: 20,
MaxNumberOfMessages: 10
}
sqs.receiveMessage(params,(err,data) => {
if(data){
var workerId = uuidV4();
// Now worker will pull the message for processing
// The worker response is returned in the callback function
Worker.pull(data,workerId,(err,respData) => {
if(respData){
// If the message was successfully processed
// set the final job status to complete and
// progress to 100%
}else{
// If the processing failed set the final
// job status to error
}
});
}
});
}
Pull
方法:
function pull(messageObject,workerId,cb){
if(messageObject){
var messageProcessed = true;
/*
* Process the message as required. Before starting the processing
* set the job status to processing.
*/
/**
* After the message has been processed, call the callback function
* inside monitor module.
*/
var callbackObject = {jobid : jobId, serverid : workerId};
if(messageProcessed){
return cb(null,callbackObject);
}else {
return cb(new Error('Failed to process messgae'),callbackObject);
}
}
}
當你拉進入睡眠/等待某個時間,如果有零消息,是什麼問題? – mootmoot