Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected virtual Task <bool> ShouldHandleAsync(EventExecutionErrorContext context)
        {
            if (!Options.EnabledErrorHandle)
            {
                return(Task.FromResult(false));
            }

            return(Task.FromResult(Options.ErrorHandleSelector == null || Options.ErrorHandleSelector.Invoke(context.EventType)));
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        protected virtual void ThrowOriginalExceptions(EventExecutionErrorContext context)
        {
            if (context.Exceptions.Count == 1)
            {
                context.Exceptions[0].ReThrow();
            }

            throw new AggregateException(
                      "More than one error has occurred while triggering the event: " + context.EventType,
                      context.Exceptions);
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="eventData"></param>
        /// <param name="onErrorAction"></param>
        /// <returns></returns>
        public virtual async Task TriggerHandlersAsync(Type eventType, object eventData, Action <EventExecutionErrorContext> onErrorAction = null)
        {
            var exceptions = new List <Exception>();

            await TriggerHandlersAsync(eventType, eventData, exceptions);

            if (exceptions.Any())
            {
                var context = new EventExecutionErrorContext(exceptions, eventType, this);
                onErrorAction?.Invoke(context);
                await ErrorHandler.HandleAsync(context);
            }
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected virtual Task <bool> ShouldRetryAsync(EventExecutionErrorContext context)
        {
            if (Options.RetryStrategyOptions == null)
            {
                return(Task.FromResult(false));
            }

            if (!context.TryGetRetryAttempt(out var retryAttempt))
            {
                return(Task.FromResult(false));
            }

            return(Task.FromResult(Options.RetryStrategyOptions.MaxRetryAttempts > retryAttempt));
        }
Esempio n. 5
0
        protected override async Task <bool> ShouldRetryAsync(EventExecutionErrorContext context)
        {
            var messageId = context.GetProperty <Guid>(nameof(LocalEventMessage.MessageId));

            context.SetProperty(RetryAttemptKey, RetryTracking.GetOrDefault(messageId));

            if (await base.ShouldRetryAsync(context))
            {
                return(true);
            }

            RetryTracking.Remove(messageId);
            return(false);
        }
Esempio n. 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual async Task HandleAsync(EventExecutionErrorContext context)
        {
            if (!await ShouldHandleAsync(context))
            {
                ThrowOriginalExceptions(context);
            }

            if (await ShouldRetryAsync(context))
            {
                await RetryAsync(context);

                return;
            }

            await MoveToDeadLetterAsync(context);
        }
Esempio n. 7
0
        protected override async Task RetryAsync(EventExecutionErrorContext context)
        {
            if (Options.RetryStrategyOptions.IntervalMillisecond > 0)
            {
                await Task.Delay(Options.RetryStrategyOptions.IntervalMillisecond);
            }

            var messageId = context.GetProperty <Guid>(nameof(LocalEventMessage.MessageId));

            context.TryGetRetryAttempt(out var retryAttempt);
            RetryTracking[messageId] = ++retryAttempt;

            await context.EventBus.As <LocalEventBus>().PublishAsync(new LocalEventMessage(messageId, context.EventData, context.EventType));

            RetryTracking.Remove(messageId);
        }
Esempio n. 8
0
        protected override async Task RetryAsync(EventExecutionErrorContext context)
        {
            if (Options.RetryStrategyOptions.IntervalMillisecond > 0)
            {
                await Task.Delay(Options.RetryStrategyOptions.IntervalMillisecond);
            }

            context.TryGetRetryAttempt(out var retryAttempt);

            await context.EventBus.As <RabbitEventBus>().PublishAsync(
                context.EventType,
                context.EventData,
                context.GetProperty(HeadersKey).As <MessageProperties>(),
                new Dictionary <string, object>
            {
                { RetryAttemptKey, ++retryAttempt },
                { "exceptions", context.Exceptions.Select(x => x.ToString()).ToList() }
            });
        }
Esempio n. 9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 protected abstract Task MoveToDeadLetterAsync(EventExecutionErrorContext context);
Esempio n. 10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 protected abstract Task RetryAsync(EventExecutionErrorContext context);
Esempio n. 11
0
        protected override Task MoveToDeadLetterAsync(EventExecutionErrorContext context)
        {
            ThrowOriginalExceptions(context);

            return(Task.CompletedTask);
        }