Ejemplo n.º 1
0
 private async Task ProcessMessages()
 {
     using (await _asyncLock.LockAsync().ConfigureAwait(false))
     {
         LastActiveTime = DateTime.Now;
         try
         {
             var scannedCount = 0;
             while (TotalUnHandledMessageCount > 0 && scannedCount < _batchSize && !IsPauseRequested)
             {
                 var message = GetMessage(ConsumingSequence);
                 if (message != null)
                 {
                     await _messageHandler.HandleAsync(message).ConfigureAwait(false);
                 }
                 scannedCount++;
                 ConsumingSequence++;
             }
         }
         catch (Exception ex)
         {
             _logger.Error(string.Format("{0} run has unknown exception, aggregateRootId: {1}", GetType().Name, AggregateRootId), ex);
             Thread.Sleep(1);
         }
         finally
         {
             CompleteRun();
         }
     }
 }
Ejemplo n.º 2
0
        public void Run()
        {
            if (_stopHandling == 1)
            {
                return;
            }
            var hasException = false;
            ProcessingCommand processingMessage = null;

            try
            {
                if (HasRemainningMessage())
                {
                    processingMessage = GetNextMessage();
                    IncreaseConsumingOffset();

                    if (processingMessage != null)
                    {
                        _messageHandler.HandleAsync(processingMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                hasException = true;

                if (ex is IOException)
                {
                    //We need to retry the command.
                    DecreaseConsumingOffset();
                }

                if (processingMessage != null)
                {
                    var command = processingMessage.Message;
                    _logger.Error(string.Format("Failed to handle command [id: {0}, type: {1}]", command.Id, command.GetType().Name), ex);
                }
                else
                {
                    _logger.Error("Failed to run command mailbox.", ex);
                }
            }
            finally
            {
                if (hasException || processingMessage == null)
                {
                    ExitHandlingMessage();
                    if (HasRemainningMessage())
                    {
                        RegisterForExecution();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        async Task StartAsync()
        {
            lastActiveOn = DateTime.UtcNow;

            while (isPausing)
            {
                logger.LogDebug($"队列当前状态为暂停,等待并重新启动。 [AggregateRootId = {aggregateRootId}]");
                pausingHandle.WaitOne(timeout);
            }

            var processingCommand = default(ProcessingCommand);

            try
            {
                processingHandle.Reset();
                isProcessing = true;

                while (currentSequence < nextSequence)
                {
                    if (queue.TryGetValue(currentSequence, out processingCommand))
                    {
                        await handler.HandleAsync(processingCommand);
                    }

                    currentSequence++;
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"处理队列启动失败。 [AggregateRootId = {aggregateRootId}, CommandType = {processingCommand?.Command?.GetType()}, CommandId = {processingCommand?.Command?.Id}]");
                Thread.Sleep(1);
            }
            finally
            {
                isProcessing = false;
                processingHandle.Set();
                Stop();

                if (currentSequence < nextSequence)
                {
                    TryStart();
                }
            }
        }