示例#1
0
        public MessageHandler(Azure.Storage.Queues.QueueClient queueClient, Azure.Storage.Queues.QueueClient deadLetterClient, Common.Configuration.IConfigurationManager config, Common.Tracking.IProcessingTracker tracker, ILogger logger)
        {
            _queueClient           = queueClient;
            _deadLetterQueueClient = deadLetterClient;

            _config  = config;
            _logger  = logger;
            _tracker = tracker;

            _factory = new RequestProcessorFactory(_config, _tracker, _logger);
        }
示例#2
0
        public async Task ProcessPendingMessagesAsync(CancellationToken token)
        {
            try
            {
                await EnsureQueueAsync();

                using (var scope = _config.ServiceProvider.CreateScope())
                {
                    Common.Tracking.IProcessingTracker tracker = scope.ServiceProvider.GetRequiredService <Common.Tracking.IProcessingTracker>();

                    RequestProcessorFactory factory = new RequestProcessorFactory(_config, tracker, _logger);

                    //currently have a permissions issue w/ accessing queue...
                    //under "Queue Service" in storage account UI, make sure to set "Authentication Method" to "Azure AD user Account" instead of "Access Key"
                    while (!token.IsCancellationRequested && (await PendingMessagesExistAsync(token)))
                    {
                        QueueMessage[] messages = null;

                        try
                        {
                            messages = await _queueClient.ReceiveMessagesAsync(maxMessages : 1, visibilityTimeout : _messageVisibilityTimeout, cancellationToken : token);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"Unable to receive messages. Account: {_queueClient.AccountName} Queue: {_queueClient.Name} Exception: {ex.Message}");
                        }

                        if (null != messages && messages.Length > 0)
                        {
                            MessageHandler <T> handler = new MessageHandler <T>(_queueClient, _deadLetterQueueClient, _config, tracker, _logger);

                            foreach (var msg in messages)
                            {
                                await handler.ProcessMessageAsync(msg, token);
                            }
                        }
                    }
                }

                _logger.LogInformation($"No additional messages found. Queue: {_queueClient.Name}.");
            }
            catch (Exception ex) //todo: filter for specific exceptions
            {
                _logger.LogError($"An error occured while monitoring queue: {_queueClient.Name}. Details: {ex.Message}");
            }
        }