node.js - How deal with the multiple messages from SQS? -
the following function receive multiple messages sqs
. each message has processed , database updated accordingly.
i can deal single message, calling pull
function worker
module. how deal multiple messages? cannot keep calling pull
method of worker
module in loop, since block thread. best way possible here?
function checkmessage(){ var params = { queueurl : constant.queue_url, visibilitytimeout: 0, waittimeseconds: 20, maxnumberofmessages: 10 } sqs.receivemessage(params,(err,data) => { if(data){ var workerid = uuidv4(); // worker pull message processing // worker response returned in callback function worker.pull(data,workerid,(err,respdata) => { if(respdata){ // if message processed // set final job status complete , // progress 100% }else{ // if processing failed set final // job status error } }); } }); }
pull
method worker
module:
function pull(messageobject,workerid,cb){ if(messageobject){ var messageprocessed = true; /* * process message required. before starting processing * set job status processing. */ /** * after message has been processed, call callback function * inside monitor module. */ var callbackobject = {jobid : jobid, serverid : workerid}; if(messageprocessed){ return cb(null,callbackobject); }else { return cb(new error('failed process messgae'),callbackobject); } } }
none of code shown synchronous or cpu intensive. test if have problem. if code not shown is synchronous or cpu instensive, may have problem whether there loop or not. use separate thread webworker-threads
or process. if need process in order search npms.io 'queue'.
Comments
Post a Comment