private void ProcessCommand(ProcessingCommand processingCommand, ICommandAsyncHandlerProxy commandAsyncHandler, int retryTimes)
        {
            var command = processingCommand.Message;

            _ioHelper.TryAsyncActionRecursively("GetCommandAsync",
                                                () => _commandStore.GetAsync(command.Id),
                                                currentRetryTimes => ProcessCommand(processingCommand, commandAsyncHandler, currentRetryTimes),
                                                result =>
            {
                var existingHandledCommand = result.Data;
                if (existingHandledCommand != null)
                {
                    if (existingHandledCommand.Message != null)
                    {
                        PublishMessageAsync(processingCommand, existingHandledCommand.Message, 0);
                    }
                    else
                    {
                        CompleteCommand(processingCommand, CommandStatus.Success, null, null);
                    }
                    return;
                }
                HandleCommandAsync(processingCommand, commandAsyncHandler, 0);
            },
                                                () => string.Format("[commandId:{0},commandType:{1}]", command.Id, command.GetType().Name),
                                                errorMessage =>
            {
                _logger.Fatal(string.Format("Get command by id has unknown exception, the code should not be run to here, errorMessage: {0}", errorMessage));
            },
                                                retryTimes, true);
        }
        private void HandleAggregateDuplicatedCommandAsync(ProcessingCommand processingCommand, IAggregateRoot dirtyAggregateRoot, DomainEventStream eventStream, int retryTimes)
        {
            var command = processingCommand.Message;

            _ioHelper.TryAsyncActionRecursively <AsyncTaskResult <HandledCommand> >("GetCommandAsync",
                                                                                    () => _commandStore.GetAsync(command.Id),
                                                                                    currentRetryTimes => HandleAggregateDuplicatedCommandAsync(processingCommand, dirtyAggregateRoot, eventStream, currentRetryTimes),
                                                                                    result =>
            {
                var existingHandledCommand = result.Data;
                if (existingHandledCommand != null)
                {
                    HandleExistingHandledAggregateAsync(processingCommand, dirtyAggregateRoot, eventStream, existingHandledCommand, 0);
                }
                else
                {
                    //到这里,说明当前command想添加到commandStore中时,提示command重复,但是尝试从commandStore中取出该command时却找不到该command。
                    //出现这种情况,我们就无法再做后续处理了,这种错误理论上不会出现,除非commandStore的Add接口和Get接口出现读写不一致的情况;
                    //我们记录错误日志,然后认为当前command已被处理为失败。
                    var errorMessage = string.Format("Command exist in the command store, but we cannot get it from the command store. commandType:{0}, commandId:{1}",
                                                     command.GetType().Name,
                                                     command.Id);
                    NotifyCommandExecuted(processingCommand, CommandStatus.Failed, null, errorMessage);
                }
            },
                                                                                    () => string.Format("[commandId:{0}]", command.Id),
                                                                                    () => NotifyCommandExecuted(processingCommand, CommandStatus.Failed, null, "Get command async failed."),
                                                                                    retryTimes);
        }
Пример #3
0
        private void ProcessCommand(ProcessingCommand processingCommand, ICommandAsyncHandlerProxy commandAsyncHandler, int retryTimes)
        {
            var command = processingCommand.Message;

            _ioHelper.TryAsyncActionRecursively <AsyncTaskResult <HandledCommand> >("GetCommandAsync",
                                                                                    () => _commandStore.GetAsync(command.Id),
                                                                                    currentRetryTimes => ProcessCommand(processingCommand, commandAsyncHandler, currentRetryTimes),
                                                                                    result =>
            {
                var existingHandledCommand = result.Data;
                if (existingHandledCommand != null)
                {
                    NotifyCommandExecuted(processingCommand, CommandStatus.NothingChanged, null, null);
                    return;
                }
                if (_logger.IsDebugEnabled)
                {
                    _logger.DebugFormat("Duplicate command execution, commandId:{0},commandType:{1}", command.Id, command.GetType().Name);
                }
                HandleCommandAsync(processingCommand, commandAsyncHandler, 0);
            },
                                                                                    () => string.Format("[commandId:{0},commandType:{1}]", command.Id, command.GetType().Name),
                                                                                    errorMessage => NotifyCommandExecuted(processingCommand, CommandStatus.Failed, typeof(string).FullName, errorMessage ?? "Get command async failed."),
                                                                                    retryTimes);
        }