我正在尝试编写一个 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();
上面的代码适用于队列中的单个消息。我应该如何编写此脚本,以便在处理完所有消息之前一直轮询队列中的消息?我应该使用设置超时吗?