コード例 #1
0
        public async Task CompleteMessage(ProcessingCommand processingCommand, CommandResult commandResult)
        {
            using (await _asyncLock.LockAsync())
            {
                LastActiveTime = DateTime.Now;
                try
                {
                    if (processingCommand.Sequence == _consumedSequence + 1)
                    {
                        _messageDict.Remove(processingCommand.Sequence);
                        await CompleteCommand(processingCommand, commandResult);

                        _consumedSequence = ProcessNextCompletedCommands(processingCommand.Sequence);
                    }
                    else if (processingCommand.Sequence > _consumedSequence + 1)
                    {
                        _requestToCompleteCommandDict[processingCommand.Sequence] = commandResult;
                    }
                    else if (processingCommand.Sequence < _consumedSequence + 1)
                    {
                        _messageDict.Remove(processingCommand.Sequence);
                        await CompleteCommand(processingCommand, commandResult);

                        _requestToCompleteCommandDict.Remove(processingCommand.Sequence);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Command mailbox complete command failed, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId), ex);
                }
            }
        }
コード例 #2
0
 public void PublishDomainEventAsync(ProcessingCommand processingCommand, DomainEventStream eventStream)
 {
     if (eventStream.Items == null || eventStream.Items.Count == 0)
     {
         eventStream.Items = processingCommand.Items;
     }
     var eventStreamMessage = new DomainEventStreamMessage(processingCommand.Message.Id, eventStream.AggregateRootId, eventStream.Version, eventStream.AggregateRootTypeName, eventStream.Events, eventStream.Items);
     PublishDomainEventAsync(processingCommand, eventStreamMessage, 0);
 }
コード例 #3
0
 public void EnqueueMessage(ProcessingCommand message)
 {
     lock (_lockObj)
     {
         message.Sequence = _maxOffset;
         message.Mailbox = this;
         _messageDict.TryAdd(message.Sequence, message);
         _maxOffset++;
     }
     RegisterForExecution();
 }
コード例 #4
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();
                    }
                }
            }
        }
コード例 #5
0
 private void CompleteMessageWithResult(ProcessingCommand processingCommand, CommandResult commandResult)
 {
     try
     {
         processingCommand.Complete(commandResult);
     }
     catch (Exception ex)
     {
         _logger.Error(string.Format("Failed to complete command, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId), ex);
     }
 }
コード例 #6
0
 public void EnqueueMessage(ProcessingCommand message)
 {
     lock (_lockObj)
     {
         message.Sequence = _maxOffset;
         message.Mailbox  = this;
         _messageDict.TryAdd(message.Sequence, message);
         _maxOffset++;
     }
     RegisterForExecution();
 }
コード例 #7
0
 private Task CompleteCommand(ProcessingCommand processingCommand, CommandResult commandResult)
 {
     try
     {
         return(processingCommand.CompleteAsync(commandResult));
     }
     catch (Exception ex)
     {
         _logger.Error(string.Format("Failed to complete command, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId), ex);
         return(Task.CompletedTask);
     }
 }
コード例 #8
0
 public void EnqueueMessage(ProcessingCommand message)
 {
     lock (_lockObj)
     {
         message.Sequence = _nextSequence;
         message.Mailbox  = this;
         if (_messageDict.TryAdd(message.Sequence, message))
         {
             _nextSequence++;
         }
     }
     RegisterForExecution();
 }
コード例 #9
0
 public void EnqueueMessage(ProcessingCommand message)
 {
     lock (_lockObj)
     {
         message.Sequence = _nextSequence;
         message.Mailbox  = this;
         if (_messageDict.TryAdd(message.Sequence, message))
         {
             _nextSequence++;
         }
     }
     LastActiveTime = DateTime.Now;
     TryRun();
 }
コード例 #10
0
 public async Task CompleteMessage(ProcessingCommand message, CommandResult result)
 {
     try
     {
         if (_messageDict.TryRemove(message.Sequence, out ProcessingCommand removed))
         {
             LastActiveTime = DateTime.Now;
             await message.CompleteAsync(result).ConfigureAwait(false);
         }
     }
     catch (Exception ex)
     {
         _logger.Error(string.Format("{0} complete message with result failed, aggregateRootId: {1}, messageId: {2}, messageSequence: {3}, result: {4}", GetType().Name, AggregateRootId, message.Message.Id, message.Sequence, result), ex);
     }
 }
コード例 #11
0
 public void EnqueueMessage(ProcessingCommand message)
 {
     lock (_lockObj)
     {
         message.Sequence = NextSequence;
         message.MailBox  = this;
         if (_messageDict.TryAdd(message.Sequence, message))
         {
             NextSequence++;
             _logger.DebugFormat("{0} enqueued new message, aggregateRootId: {1}, messageId: {2}, messageSequence: {3}", GetType().Name, AggregateRootId, message.Message.Id, message.Sequence);
             LastActiveTime = DateTime.Now;
             TryRun();
         }
     }
 }
コード例 #12
0
 public void EnqueueMessage(ProcessingCommand message)
 {
     lock (_lockObj)
     {
         message.Sequence = _nextSequence;
         message.MailBox  = this;
         if (_messageDict.TryAdd(message.Sequence, message))
         {
             _nextSequence++;
             _logger.InfoFormat("{0} enqueued new message, message: {1}, sequence: {2}", GetType().Name, _jsonSerializer.Serialize(message), message.Sequence);
             LastActiveTime = DateTime.Now;
             TryRun();
         }
         else
         {
             _logger.ErrorFormat("{0} enqueue message failed, message: {1}, sequence: {2}", GetType().Name, _jsonSerializer.Serialize(message), message.Sequence);
         }
     }
 }
コード例 #13
0
        public async void Run()
        {
            LastActiveTime = DateTime.Now;
            while (_isPaused)
            {
                _logger.InfoFormat("Command mailbox is pausing and we should wait for a while, aggregateRootId: {0}", AggregateRootId);
                _pauseWaitHandle.WaitOne(1000);
            }
            ProcessingCommand processingCommand = null;

            try
            {
                _processingWaitHandle.Reset();
                _isProcessingCommand = true;
                var count = 0;
                while (_consumingSequence < _nextSequence && count < _batchSize)
                {
                    processingCommand = GetProcessingCommand(_consumingSequence);
                    if (processingCommand != null)
                    {
                        await _messageHandler.Handle(processingCommand);
                    }
                    _consumingSequence++;
                    count++;
                }
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Command mailbox run has unknown exception, aggregateRootId: {0}, commandId: {1}", AggregateRootId, processingCommand != null ? processingCommand.Message.Id : string.Empty), ex);
                Thread.Sleep(1);
            }
            finally
            {
                _isProcessingCommand = false;
                _processingWaitHandle.Set();
                Exit();
                if (_consumingSequence < _nextSequence)
                {
                    TryRun();
                }
            }
        }
コード例 #14
0
 public void CompleteMessage(ProcessingCommand message, CommandResult commandResult)
 {
     lock (_lockObj2)
     {
         if (message.Sequence == _consumedOffset + 1)
         {
             _messageDict.Remove(message.Sequence);
             _consumedOffset = message.Sequence;
             CompleteMessageWithResult(message, commandResult);
             ProcessRequestToCompleteOffsets();
         }
         else if (message.Sequence > _consumedOffset + 1)
         {
             _requestToCompleteOffsetDict[message.Sequence] = commandResult;
         }
         else if (message.Sequence < _consumedOffset + 1)
         {
             _messageDict.Remove(message.Sequence);
             _requestToCompleteOffsetDict.Remove(message.Sequence);
         }
     }
 }
コード例 #15
0
 public void CompleteMessage(ProcessingCommand message, CommandResult commandResult)
 {
     lock (_lockObj2)
     {
         if (message.Sequence == _consumedOffset + 1)
         {
             _messageDict.Remove(message.Sequence);
             _consumedOffset = message.Sequence;
             CompleteMessageWithResult(message, commandResult);
             ProcessRequestToCompleteOffsets();
         }
         else if (message.Sequence > _consumedOffset + 1)
         {
             _requestToCompleteOffsetDict[message.Sequence] = commandResult;
         }
         else if (message.Sequence < _consumedOffset + 1)
         {
             _messageDict.Remove(message.Sequence);
             _requestToCompleteOffsetDict.Remove(message.Sequence);
         }
     }
 }
コード例 #16
0
 public void CompleteMessage(ProcessingCommand processingCommand, CommandResult commandResult)
 {
     lock (_lockObj2)
     {
         try
         {
             if (processingCommand.Sequence == _consumedSequence + 1)
             {
                 _messageDict.Remove(processingCommand.Sequence);
                 CompleteCommandWithResult(processingCommand, commandResult);
                 _consumedSequence = ProcessNextCompletedCommands(processingCommand.Sequence);
             }
             else if (processingCommand.Sequence > _consumedSequence + 1)
             {
                 _requestToCompleteSequenceDict[processingCommand.Sequence] = commandResult;
             }
             else if (processingCommand.Sequence < _consumedSequence + 1)
             {
                 _messageDict.Remove(processingCommand.Sequence);
                 CompleteCommandWithResult(processingCommand, commandResult);
                 _requestToCompleteSequenceDict.Remove(processingCommand.Sequence);
             }
             if (_logger.IsDebugEnabled)
             {
                 _logger.DebugFormat("Command mailbox complete command success, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId);
             }
         }
         catch (Exception ex)
         {
             _logger.Error(string.Format("Command mailbox complete command failed, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId), ex);
         }
         finally
         {
             RegisterForExecution();
         }
     }
 }
コード例 #17
0
ファイル: DefaultEventService.cs プロジェクト: andyshao/enode
 private void PublishDomainEventAsync(ProcessingCommand processingCommand, DomainEventStreamMessage eventStream, int retryTimes)
 {
     _ioHelper.TryAsyncActionRecursively<AsyncTaskResult>("PublishDomainEventAsync",
     () => _domainEventPublisher.PublishAsync(eventStream),
     currentRetryTimes => PublishDomainEventAsync(processingCommand, eventStream, currentRetryTimes),
     result =>
     {
         _logger.DebugFormat("Publish domain events success, {0}", eventStream);
         processingCommand.Complete(new CommandResult(CommandStatus.Success, processingCommand.Message.Id, eventStream.AggregateRootId, null, null));
     },
     () => string.Format("[eventStream:{0}]", eventStream),
     errorMessage => processingCommand.Complete(new CommandResult(CommandStatus.Failed, processingCommand.Message.Id, eventStream.AggregateRootId, null, errorMessage ?? "Publish domain event async failed.")),
     retryTimes);
 }
コード例 #18
0
 public EventProcessingContext(IAggregateRoot aggregateRoot, EventStream eventStream, ProcessingCommand processingCommand)
 {
     AggregateRoot = aggregateRoot;
     EventStream = eventStream;
     ProcessingCommand = processingCommand;
 }
コード例 #19
0
        private void PublishEvents(IDictionary<string, string> contextItems, ProcessingCommand processingCommand, EventStream eventStream)
        {
            var publishEvents = new Func<bool>(() =>
            {
                try
                {
                    _eventPublisher.PublishEvent(contextItems, eventStream);
                    return true;
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Exception raised when publishing events:{0}", eventStream), ex);
                    return false;
                }
            });

            var publishEventsCallback = new Func<object, bool>(obj =>
            {
                var currentProcessingCommand = obj as ProcessingCommand;
                currentProcessingCommand.CommandExecuteContext.OnCommandExecuted(currentProcessingCommand.Command, CommandStatus.Success, 0, null);
                return true;
            });

            _actionExecutionService.TryAction("PublishEvents", publishEvents, 3, new ActionInfo("PublishEventsCallback", publishEventsCallback, processingCommand, null));
        }
コード例 #20
0
ファイル: DefaultEventService.cs プロジェクト: ulswww/enode
 private void PublishDomainEventAsync(ProcessingCommand processingCommand, DomainEventStreamMessage eventStream, int retryTimes)
 {
     _ioHelper.TryAsyncActionRecursively("PublishEventAsync",
     () => _domainEventPublisher.PublishAsync(eventStream),
     currentRetryTimes => PublishDomainEventAsync(processingCommand, eventStream, currentRetryTimes),
     result =>
     {
         if (_logger.IsDebugEnabled)
         {
             _logger.DebugFormat("Publish event success, {0}", eventStream);
         }
         var commandHandleResult = processingCommand.CommandExecuteContext.GetResult();
         CompleteCommand(processingCommand, new CommandResult(CommandStatus.Success, processingCommand.Message.Id, eventStream.AggregateRootId, commandHandleResult, typeof(string).FullName));
     },
     () => string.Format("[eventStream:{0}]", eventStream),
     errorMessage =>
     {
         _logger.Fatal(string.Format("Publish event has unknown exception, the code should not be run to here, errorMessage: {0}", errorMessage));
     },
     retryTimes, true);
 }
コード例 #21
0
ファイル: DefaultEventService.cs プロジェクト: ulswww/enode
 private void CompleteCommand(ProcessingCommand processingCommand, CommandResult commandResult)
 {
     processingCommand.Mailbox.CompleteMessage(processingCommand, commandResult);
     _logger.InfoFormat("Complete command, aggregateId: {0}", processingCommand.Message.AggregateRootId);
 }
コード例 #22
0
 private void PublishDomainEventAsync(ProcessingCommand processingCommand, DomainEventStreamMessage eventStream, int retryTimes)
 {
     _ioHelper.TryAsyncActionRecursively<AsyncTaskResult>("PublishDomainEventAsync",
     () => _domainEventPublisher.PublishAsync(eventStream),
     currentRetryTimes => PublishDomainEventAsync(processingCommand, eventStream, currentRetryTimes),
     result =>
     {
         if (_logger.IsDebugEnabled)
         {
             _logger.DebugFormat("Publish domain events success, {0}", eventStream);
         }
         var commandHandleResult = processingCommand.CommandExecuteContext.GetResult();
         processingCommand.Complete(new CommandResult(CommandStatus.Success, processingCommand.Message.Id, eventStream.AggregateRootId, commandHandleResult, typeof(string).FullName));
     },
     () => string.Format("[eventStream:{0}]", eventStream),
     errorMessage => processingCommand.Complete(new CommandResult(CommandStatus.Failed, processingCommand.Message.Id, eventStream.AggregateRootId, errorMessage ?? "Publish domain event async failed.", typeof(string).FullName)),
     retryTimes);
 }
コード例 #23
0
 private void CompleteMessageWithResult(ProcessingCommand processingCommand, CommandResult commandResult)
 {
     try
     {
         processingCommand.Complete(commandResult);
     }
     catch (Exception ex)
     {
         _logger.Error(string.Format("Failed to complete command, commandId: {0}, aggregateRootId: {1}", processingCommand.Message.Id, processingCommand.Message.AggregateRootId), ex);
     }
 }