예제 #1
0
        public async Task <bool> HandleRecievedItemAsync <TCommand, TResult>(QueueItem <TCommand> item, int maxDequeueCount) where TCommand : class, ICommand <TResult>
        {
            try
            {
                _logger.LogInfo($"Recieved command {item.GetType().Name} from queue");
                bool shouldDequeue = true;
                IQueueableCommand queueableCommand = item.Item as IQueueableCommand;
                if (queueableCommand != null)
                {
                    queueableCommand.DequeueCount = item.DequeueCount;
                }
                Task commandTask = _commandExecuter.ExecuteAsync(item.Item);
                while (!commandTask.Wait(TimeSpan.FromSeconds(10)))
                {
                    await item.ExtendLeaseAsync();
                }

                if (queueableCommand != null)
                {
                    shouldDequeue = queueableCommand.ShouldDequeue;
                }
                _logger.LogInfo($"Completed processing command {item.GetType().Name} and returning a shouldDequeue status of {shouldDequeue}");
                return(shouldDequeue);
            }
            catch (Exception ex)
            {
                if (item.DequeueCount > maxDequeueCount)
                {
                    _logger.LogError($"Error during processing command of type {item.GetType().Name} with exception of type {ex.GetType().AssemblyQualifiedName}. Dequeue count is {item.DequeueCount}, will not try again.", item.Item, ex);
                    return(true);
                }
                _logger.LogWarning($"Error during processing command of type {item.GetType().Name} with exception of type {ex.GetType().AssemblyQualifiedName}. Dequeue count is {item.DequeueCount}, will try again.", item.Item, ex);
                return(false);
            }
        }
        private async Task ProcessMessageAsync(Message message, CancellationToken cancellationToken)
        {
            TCommand command = default(TCommand);

            try
            {
                command = _serializer.Deserialize <TCommand>(message.Body);
                _logger.LogInfo($"Recieved command {command.GetType().Name} from queue");
                bool shouldDequeue = true;

                // ReSharper disable once SuspiciousTypeConversion.Global
                IQueueableCommand queueableCommand = command as IQueueableCommand;
                if (queueableCommand != null)
                {
                    queueableCommand.DequeueCount = message.SystemProperties.DeliveryCount;
                }

                await ExecuteCommandAsync(command, cancellationToken);

                if (queueableCommand != null)
                {
                    shouldDequeue = queueableCommand.ShouldDequeue;
                }

                _logger.LogInfo($"Completed processing command {command.GetType().Name} and returning a shouldDequeue status of {shouldDequeue}");
                if (shouldDequeue)
                {
                    await _queueClient.CompleteAsync(message.SystemProperties.LockToken);
                }
            }
            catch (Exception ex)
            {
                _logger.LogWarning($"Error during processing command of type {typeof(TCommand).Name} with exception of type {ex.GetType().AssemblyQualifiedName}. Dequeue count is {message.SystemProperties.DeliveryCount}, will try again.",
                                   command, ex);
            }
        }