我想寫一個nodejs sqs隊列處理器。Nodejs sqs隊列處理器
"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 to 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 from queue
sqs.deleteMessage({
"QueueUrl" : appConf.sqs_distribution_url,
"ReceiptHandle" :data.Messages[0].ReceiptHandle
});
}
});
}
}
});
}
readMessage();
上述代碼適用於隊列中的單個消息。我應該如何編寫此腳本,以便它可以繼續輪詢隊列中的郵件,直到處理完所有郵件?我應該使用設置超時嗎?所有的
您好,請查看我的這個要點。 https://gist.github.com/yalamber/374add88e887e688d818 – Yalamber
另外我應該擔心運行此腳本時有任何內存泄漏? – Yalamber
@askkirati更新! – zavg