private static Polly.Retry.AsyncRetryPolicy GetWaitAndRetry(EventReliabilitySettings eventReliabilitySettings) { return(Policy .Handle <Exception>() .WaitAndRetryAsync( (int)(eventReliabilitySettings?.RetryAttempts).GetValueOrDefault(), retryAttempt => eventReliabilitySettings?.RetrySleepDuration ?? TimeSpan.FromSeconds(0))); }
/// <summary> /// Add EventHub publisher with IoC. /// </summary> /// <exception cref="ArgumentException">When <paramref name="eventName"/> or <paramref name="connectionString"/> is null, empty or invalid.</exception> /// <exception cref="ArgumentNullException"><paramref name="connectionString"/> is invalid.</exception> /// <param name="services"><see cref="IServiceCollection"/> injection.</param> /// <param name="eventName">EventHub name.</param> /// <param name="connectionString">EventHub connection string with format: Endpoint=sb://namespace_DNS_Name;SharedAccessKeyName=SHARED_ACCESS_KEY_NAME;SharedAccessKey=SHARED_ACCESS_KEY;EntityPath=.</param> /// <param name="reliabilitySettings">Event reliability settings.</param> /// <returns><see cref="IServiceCollection"/> with EventHub plugin injected.</returns> public static IServiceCollection AddEventHubPlugin( this IServiceCollection services, string eventName, string connectionString, EventReliabilitySettings reliabilitySettings = null) { services.AddEventPlugin(eventName, new AzureEventHub(connectionString, eventName, reliabilitySettings)); return(services); }
/// <summary> /// Start event hub process. /// </summary> /// <param name="serverlessLogger">Logger to save log.</param> /// <param name="logger">Logger to be injected in <paramref name="serverlessLogger"/>.</param> /// <param name="patternLog">Pattern log.</param> /// <param name="events">Events to be processed.</param> /// <param name="processEventAsync">Process with business rules to be processed.</param> /// <param name="publishPoisonedEventAsync">Function receiving error parameter to publish poisoned event data.</param> /// <param name="eventReliabilitySettings">Event reliability settings.</param> /// <returns>Task executed.</returns> public static async Task StartEventProcessAsync( IServerlessLogger serverlessLogger, ILogger logger, string patternLog, EventData[] events, Func <EventData, Task> processEventAsync, Func <EventData, Exception, Task> publishPoisonedEventAsync, EventReliabilitySettings eventReliabilitySettings) { try { serverlessLogger.InjectLogger(logger); if (events.Length == 0) { serverlessLogger.LogWarning($"{patternLog} No event to process."); return; } serverlessLogger.LogInformation($"{patternLog} Start processing..."); var waitAndRetryAsync = GetWaitAndRetry(eventReliabilitySettings); var eventCount = events.Length; for (int eventIndex = 0; eventIndex < eventCount; eventIndex++) { serverlessLogger.LogInformation($"{patternLog} Processing event index {eventIndex}..."); var eventData = events[eventIndex]; var fallbackAsync = GetFallback(serverlessLogger, patternLog, eventData, publishPoisonedEventAsync); await Policy .WrapAsync(fallbackAsync, waitAndRetryAsync) .ExecuteAsync(() => processEventAsync(eventData)) .ConfigureAwait(false); serverlessLogger.LogInformation($"{patternLog} Event index {eventIndex} processed!"); } serverlessLogger.LogInformation($"{patternLog} Processing completed!"); } catch (Exception ex) { serverlessLogger.LogError(ex, $"{patternLog} Processing failed!"); throw; } }
/// <summary> /// Initializes a new instance of the <see cref="AzureEventHub"/> class. /// </summary> /// <exception cref="ArgumentException">When <paramref name="connectionString"/> is null, empty or invalid.</exception> /// <exception cref="ArgumentException">When <paramref name="eventName"/> is null, empty or invalid.</exception> /// <param name="connectionString">EventHub connection string with format: Endpoint=sb://namespace_DNS_Name;EntityPath=EVENT_HUB_NAME;SharedAccessKeyName=SHARED_ACCESS_KEY_NAME;SharedAccessKey=SHARED_ACCESS_KEY.</param> /// <param name="eventName">EventHub name to set EntityPath.</param> /// <param name="reliabilitySettings">Event reliability settings.</param> public AzureEventHub(string connectionString, string eventName, EventReliabilitySettings reliabilitySettings = null) { if (string.IsNullOrWhiteSpace(connectionString)) { throw new ArgumentException(Messages.REQUIRED_PARAMETER, nameof(connectionString)); } if (string.IsNullOrWhiteSpace(eventName)) { throw new ArgumentException(Messages.REQUIRED_PARAMETER, nameof(eventName)); } var connectionStringBuilder = new EventHubsConnectionStringBuilder(connectionString) { TransportType = TransportType.AmqpWebSockets, EntityPath = eventName }; eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString()); ReliabilitySettings = reliabilitySettings; }