Azure毒物队列:nodejs - 如何创建出列阈值

时间:2015-09-24 08:44:23

标签: node.js azure poison-queue

我已经创建了一个存储队列,在队列中插入了一条消息。我一直在用节点写的webjob阅读消息,我可以看到dequeue计数正在增加。但是,它从来没有被列入毒药队列。

这是用天蓝色还是我必须在我的nodeja webjob中写的东西?

在C#中创建队列

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference("successemailqueue");
queue.CreateIfNotExists();

var messageData = JsonConvert.SerializeObject(data, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
queue.AddMessage(new CloudQueueMessage(messageData));

从队列中读取消息:

var retryOperations = new azure.ExponentialRetryPolicyFilter();
var queueService = azure.createQueueService(config.storageName, config.storageKey)
                        .withFilter(retryOperations);
var singleMessageDefaults = { numofmessages: 1, visibilitytimeout: 2 * 60 };

var getSingleMessage = function() {
    var deferred = Q.defer();
    queueService.getMessages(config.queueName, singleMessageDefaults,
                             getSingleMessageComplete(deferred));
    return deferred.promise;
};

处理消息:

var processMessage = function(message){
    if (message) {
        if (message.dequeuecount < 5) {
            sendEmail(message, queue.deleteMessage);
        }else{
            // move to poison queue
        }
    }
};

由于

2 个答案:

答案 0 :(得分:1)

因为您已经使用了this example,所以可以在queue.js文件中添加一个方法,用于将新消息插入队列,并且可以像这样实现进入毒性队列:

插入新消息(queue.js):

function addMessage(queueName, message){
    var deferred = Q.defer();
    queueService.createMessage(queueName, message,
        function (error, result, response) {
            if (error) {
                deferred.reject({ error: error, result: result, response: response });
            } else {
                deferred.resolve(result);
            }
        });

    return deferred.promise;
}

转向毒药(run.js):

var processMessage = function(message){
    var deferred = Q.defer();
        if (message) {
            if (message.dequeuecount < 5) {
                sendEmail(message, queue.deleteMessage);
            } else {
                moveToPoison(message);
            };
    }
}

function moveToPoison(message){
    var deferred = Q.defer();

    var poisonQueueConf = {
        storageName: config.storageName, 
        storageKey: config.storageKey,
        queueName: config.queueName + '-poison'
    };

    var poisonQueue = require("./queue")(poisonQueueConf);

    queue.deleteMessage(message)
        .then(function(){
            return poisonQueue.addMessage(.messagetext)
                .then(function () {
                    deferred.resolve();
                }); 
        }).catch(function(error){
            deferred.reject(error);
        });

    return deferred.promise;        
}

答案 1 :(得分:0)

我尝试成功复制您的问题。当从队列中读取消息并且不删除它时,dequeuecount属性将会增加。

因此,您的问题的解决方案是在readed消息之后及时删除队列存储中的消息。请参阅https://azure.microsoft.com/en-us/documentation/articles/storage-nodejs-how-to-use-queues/#how-to-dequeue-the-next-message

这是我下面的示例代码,修改代码:

var azure = require('azure');
var config = {storageName: "<storage_name>", storageKey: "<storage_key>", queueName: "<queue_name>"};
var retryOperations = new azure.ExponentialRetryPolicyFilter();
var queueService = azure.createQueueService(config.storageName, config.storageKey)
                        .withFilter(retryOperations);
var singleMessageDefaults = { numofmessages: 1, visibilitytimeout: 2 * 60 };
var getSingleMessage = function() {
    var message;
    queueService.getMessages(config.queueName, singleMessageDefaults, function(error, result, response) {
        if(!error) {
            message = result[0];
            if(message != undefined) {
                queueService.deleteMessage(config.queueName, message.messageid, message.popreceipt, function(error, response){
                    if(!error){
                            console.log('message deleted from queue');
                    }
                });
            }
        }
    });
    return message;
};

最诚挚的问候。