/// <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))); }
/// <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); }
/// <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); } }
/// <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)); }
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); }
/// <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); }
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); }
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() } }); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <returns></returns> protected abstract Task MoveToDeadLetterAsync(EventExecutionErrorContext context);
/// <summary> /// /// </summary> /// <param name="context"></param> /// <returns></returns> protected abstract Task RetryAsync(EventExecutionErrorContext context);
protected override Task MoveToDeadLetterAsync(EventExecutionErrorContext context) { ThrowOriginalExceptions(context); return(Task.CompletedTask); }