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

            _ioHelper.TryAsyncActionRecursively <AsyncTaskResult <HandledCommand> >("GetCommandAsync",
                                                                                    () => _commandStore.GetAsync(command.Id),
                                                                                    currentRetryTimes => HandleFaildCommandAsync(processingCommand, commandHandler, currentRetryTimes),
                                                                                    result =>
            {
                var existingHandledCommand = result.Data;
                if (existingHandledCommand != null)
                {
                    if (existingHandledCommand.Message != null)
                    {
                        PublishMessageAsync(processingCommand, existingHandledCommand.Message, 0);
                    }
                    else
                    {
                        NotifyCommandExecuted(processingCommand, CommandStatus.Success, null, null);
                    }
                }
                else
                {
                    NotifyCommandExecuted(processingCommand, CommandStatus.Failed, null, "Handle command failed.");
                }
            },
                                                                                    () => string.Format("[command:[id:{0},type:{1}]", command.Id, command.GetType().Name),
                                                                                    () => NotifyCommandExecuted(processingCommand, CommandStatus.Failed, null, "Get command async failed."),
                                                                                    retryTimes);
        }
Пример #2
0
        private void HandleCommandAsync(ProcessingCommand processingCommand, ICommandAsyncHandlerProxy commandHandler, int retryTimes)
        {
            var command = processingCommand.Message;

            _ioHelper.TryAsyncActionRecursively <AsyncTaskResult <IApplicationMessage> >("HandleCommandAsync",
                                                                                         () => commandHandler.HandleAsync(command),
                                                                                         currentRetryTimes => HandleCommandAsync(processingCommand, commandHandler, currentRetryTimes),
                                                                                         result =>
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.DebugFormat("Handle command async success. handlerType:{0}, commandType:{1}, commandId:{2}, aggregateRootId:{3}",
                                        commandHandler.GetInnerObject().GetType().Name,
                                        command.GetType().Name,
                                        command.Id,
                                        command.AggregateRootId);
                }
                if (result.Data == null)
                {
                    NotifyCommandExecuted(processingCommand, CommandStatus.Success, null, null);
                }
                else
                {
                    CommitChangesAsync(processingCommand, result.Data, 0);
                }
            },
                                                                                         () => string.Format("[command:[id:{0},type:{1}],handlerType:{2}]", command.Id, command.GetType().Name, commandHandler.GetInnerObject().GetType().Name),
                                                                                         errorMessage => HandleFaildCommandAsync(processingCommand, commandHandler, 0),
                                                                                         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);
        }
        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 Task HandleCommandAsync(ProcessingCommand processingCommand, ICommandAsyncHandlerProxy commandHandler, int retryTimes)
        {
            var command = processingCommand.Message;

            _ioHelper.TryAsyncActionRecursively("HandleCommandAsync",
                                                () =>
            {
                try
                {
                    var asyncResult = commandHandler.HandleAsync(command);
                    if (_logger.IsDebugEnabled)
                    {
                        _logger.DebugFormat("Handle command async success. handlerType:{0}, commandType:{1}, commandId:{2}, aggregateRootId:{3}",
                                            commandHandler.GetInnerObject().GetType().Name,
                                            command.GetType().Name,
                                            command.Id,
                                            command.AggregateRootId);
                    }
                    return(asyncResult);
                }
                catch (IOException ex)
                {
                    _logger.Error(string.Format("Handle command async has io exception. handlerType:{0}, commandType:{1}, commandId:{2}, aggregateRootId:{3}",
                                                commandHandler.GetInnerObject().GetType().Name,
                                                command.GetType().Name,
                                                command.Id,
                                                command.AggregateRootId), ex);
                    return(Task.FromResult(new AsyncTaskResult <IApplicationMessage>(AsyncTaskStatus.IOException, ex.Message)));
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Handle command async has unknown exception. handlerType:{0}, commandType:{1}, commandId:{2}, aggregateRootId:{3}",
                                                commandHandler.GetInnerObject().GetType().Name,
                                                command.GetType().Name,
                                                command.Id,
                                                command.AggregateRootId), ex);
                    return(Task.FromResult(new AsyncTaskResult <IApplicationMessage>(AsyncTaskStatus.Failed, ex.Message)));
                }
            },
                                                currentRetryTimes => HandleCommandAsync(processingCommand, commandHandler, currentRetryTimes),
                                                result =>
            {
                CommitChangesAsync(processingCommand, true, result.Data, null);
            },
                                                () => string.Format("[command:[id:{0},type:{1}],handlerType:{2}]", command.Id, command.GetType().Name, commandHandler.GetInnerObject().GetType().Name),
                                                errorMessage =>
            {
                CommitChangesAsync(processingCommand, false, null, errorMessage);
            },
                                                retryTimes);

            return(Task.CompletedTask);
        }
        private void HandleCommand(ProcessingCommand processingCommand, ICommandAsyncHandlerProxy commandHandler)
        {
            var realHandler = commandHandler.GetInnerObject() as ICommandAsyncHandler;

            if (realHandler.CheckCommandHandledFirst)
            {
                ProcessCommand(processingCommand, commandHandler, 0);
            }
            else
            {
                HandleCommandAsync(processingCommand, commandHandler, 0);
            }
        }
 private Task HandleCommand(ProcessingCommand processingCommand, ICommandAsyncHandlerProxy commandHandler)
 {
     return(HandleCommandAsync(processingCommand, commandHandler, 0));
 }
 private void HandleCommand(ProcessingCommand processingCommand, ICommandAsyncHandlerProxy commandHandler)
 {
     HandleCommandAsync(processingCommand, commandHandler, 0);
 }