示例#1
0
        private void CommitAggregate(AggregateRoot dirtyAggregate, ICommand command, IMessageQueue <ICommand> queue)
        {
            var eventStream = BuildEvents(dirtyAggregate, command);

            if (_retryService.TryAction("TrySendEvent", () => TrySendEvent(eventStream), 3))
            {
                FinishExecution(command, queue);
            }
            else
            {
                _retryService.RetryInQueue(
                    new ActionInfo(
                        "TrySendEvent",
                        (obj) => TrySendEvent(obj as EventStream),
                        eventStream,
                        new ActionInfo(
                            "SendEventSuccessAction",
                            (obj) =>
                {
                    var data           = obj as dynamic;
                    var currentCommand = data.Command as ICommand;
                    var currentQueue   = data.Queue as IMessageQueue <ICommand>;
                    FinishExecution(currentCommand, currentQueue);
                    return(true);
                },
                            new { Command = command, Queue = queue },
                            null)));
            }
        }
        public override void Execute(EventStream message, IMessageQueue <EventStream> queue)
        {
            var eventStreamContext = new EventStreamContext {
                EventStream = message, Queue = queue
            };

            Func <EventStreamContext, bool> tryCommitEventsAction = (context) => {
                try {
                    return(CommitEvents(context));
                }
                catch (Exception ex) {
                    _logger.Error(string.Format("Exception raised when committing events:{0}.", context.EventStream.GetStreamInformation()), ex);
                    return(false);
                }
            };

            if (!_retryService.TryAction("TryCommitEvents", () => tryCommitEventsAction(eventStreamContext), 3))
            {
                _retryService.RetryInQueue(new ActionInfo("TryCommitEvents", (obj) => tryCommitEventsAction(obj as EventStreamContext), eventStreamContext, null));
            }
        }
        private void TryDispatchEventsToEventHandlers(EventStreamContext context)
        {
            Func <EventStreamContext, bool> tryDispatchEventsAction = (streamContext) => {
                if (streamContext.EventStream.Version == 1)
                {
                    return(DispatchEventsToHandlers(streamContext.EventStream));
                }
                else
                {
                    var lastPublishedVersion = _eventPublishInfoStore.GetEventPublishedVersion(streamContext.EventStream.AggregateRootId);

                    if (lastPublishedVersion + 1 == streamContext.EventStream.Version)
                    {
                        return(DispatchEventsToHandlers(streamContext.EventStream));
                    }
                    else if (lastPublishedVersion + 1 > streamContext.EventStream.Version)
                    {
                        return(true);
                    }

                    return(false);
                }
            };

            try {
                if (_retryService.TryAction("TryDispatchEvents", () => tryDispatchEventsAction(context), 3))
                {
                    Clear(context);
                }
                else
                {
                    _retryService.RetryInQueue(
                        new ActionInfo(
                            "TryDispatchEvents",
                            (obj) => tryDispatchEventsAction(obj as EventStreamContext),
                            context,
                            new ActionInfo(
                                "DispatchEventsSuccessAction",
                                (data) => { Clear(data as EventStreamContext); return(true); },
                                context, null)
                            )
                        );
                }
            }
            catch (Exception ex) {
                _logger.Error(string.Format("Exception raised when dispatching events:{0}", context.EventStream.GetStreamInformation()), ex);
            }
        }
        /// <summary>Retry the given command.
        /// </summary>
        /// <param name="commandInfo"></param>
        /// <param name="eventStream"></param>
        /// <param name="errorInfo"></param>
        /// <param name="retrySuccessCallbackAction"></param>
        public void RetryCommand(CommandInfo commandInfo, EventStream eventStream, ErrorInfo errorInfo, ActionInfo retrySuccessCallbackAction)
        {
            if (_retryCommandQueue == null)
            {
                _retryCommandQueue = Configuration.Instance.GetRetryCommandQueue();
            }
            var command = commandInfo.Command;

            Action <CommandInfo, ActionInfo> actionAfterCommandRetried = (currentCommandInfo, callbackActionInfo) =>
            {
                currentCommandInfo.IncreaseRetriedCount();
                _logger.InfoFormat("Sent {0} to command retry queue for {1} time.", currentCommandInfo.Command.GetType().Name, currentCommandInfo.RetriedCount);
                callbackActionInfo.Action(callbackActionInfo.Data);
            };

            if (commandInfo.RetriedCount < command.RetryCount)
            {
                if (_retryService.TryAction("TryEnqueueCommand", () => TryEnqueueCommand(command), 2))
                {
                    actionAfterCommandRetried(commandInfo, retrySuccessCallbackAction);
                }
                else
                {
                    _retryService.RetryInQueue(
                        new ActionInfo(
                            "TryEnqueueCommand",
                            (obj) => TryEnqueueCommand(obj as ICommand),
                            command,
                            new ActionInfo(
                                "TryEnqueueCommandFinishedAction",
                                (obj) =>
                    {
                        var data = obj as dynamic;
                        var currentCommandInfo = data.CommandInfo as CommandInfo;
                        var callbackActionInfo = data.Callback as ActionInfo;
                        actionAfterCommandRetried(currentCommandInfo, callbackActionInfo);
                        return(true);
                    },
                                new { CommandInfo = commandInfo, Callback = retrySuccessCallbackAction },
                                null)));
                }
            }
            else
            {
                _commandAsyncResultManager.TryComplete(commandInfo.Command.Id, eventStream.AggregateRootId, errorInfo.ErrorMessage, errorInfo.Exception);
            }
        }