node.js - Nodejs sqs queue processor -
i trying write nodejs sqs queue processor.
"use strict"; var appconf = require('./config/appconf'); var aws = require('aws-sdk'); aws.config.loadfrompath('./config/aws_config.json'); var sqs = new aws.sqs(); var exec = require('child_process').exec; function readmessage() { sqs.receivemessage({ "queueurl": appconf.sqs_distribution_url, "maxnumberofmessages": 1, "visibilitytimeout": 30, "waittimeseconds": 20 }, function (err, data) { var sqs_message_body; if (data.messages) { if (typeof data.messages[0] !== 'undefined' && typeof data.messages[0].body !== 'undefined') { //sqs msg body sqs_message_body = json.parse(data.messages[0].body); //make call nodejs handler in codeigniter exec('php '+ appconf.ci_fc_path +'/index.php nodejs_handler make_contentq_call "'+ sqs_message_body.contentq_cat_id+'" "'+sqs_message_body.cnhq_cat_id+'" "'+sqs_message_body.network_id+'"', function (error, stdout, stderr) { if (error) { throw error; } console.log('stdout: ' + stdout); if(stdout == 'success'){ //delete message queue sqs.deletemessage({ "queueurl" : appconf.sqs_distribution_url, "receipthandle" :data.messages[0].receipthandle }); } }); } } }); } readmessage();
the above code works fine single message in queue. how should write script keeps polling messages in queue untill messages processed? should use set timeout?
first of should definetely use long polling technique provided amazon, , understand using because have "waittimeseconds": 20
argument in sqs.receivemessage
call. hope didn't forget configure in aws web interface.
about polling messages - may use different techniques including timers, think simple call readmessage()
function @ end of receivemessage
's (or exec
's) callback function. processing of (or waiting for) next message in queue start after end of processing of previous message in queue.
update:
as me in new version of code there many readmessage()
calls. think better minimize keep code more clear , easy maintain. if leave, example, 1 call @ end of main receivemessage
callback recieve lot of php worker scripts running in parallel - , maybe not bad point of view of performance - have add complicated script control amount of parallel workers. think can cut calls in exec
callback, try join if
s , join calls in main callback.
"use strict"; var appconf = require('./config/appconf'); var aws = require('aws-sdk'); aws.config.loadfrompath('./config/aws_config.json'); var delay = 20 * 1000; var sqs = new aws.sqs(); var exec = require('child_process').exec; function readmessage() { sqs.receivemessage({ "queueurl": appconf.sqs_distribution_url, "maxnumberofmessages": 1, "visibilitytimeout": 30, "waittimeseconds": 20 }, function (err, data) { var sqs_message_body; if (data.messages) && (typeof data.messages[0] !== 'undefined' && typeof data.messages[0].body !== 'undefined')) { //sqs msg body sqs_message_body = json.parse(data.messages[0].body); //make call nodejs handler in codeigniter exec('php '+ appconf.ci_fc_path +'/index.php nodejs_handler make_contentq_call "'+ sqs_message_body.contentq_cat_id+'" "'+sqs_message_body.cnhq_cat_id+'" "'+sqs_message_body.network_id+'"', function (error, stdout, stderr) { if (error) { // error handling } if(stdout == 'success'){ //delete message queue sqs.deletemessage({ "queueurl" : appconf.sqs_distribution_url, "receipthandle" :data.messages[0].receipthandle }, function(err, data){ }); } readmessage(); }); } } readmessage(); }); } readmessage();
about memory leaks: think should not worry because next call of readmessage()
happens in callback function - not recursively, , recursively called function returns value parent function after calling receivemessage()
function.
Comments
Post a Comment