/// <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> /// Start cosmosdb 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="documents">Documents to be processed.</param> /// <param name="func">Process with business rules to be processed.</param> /// <returns>Task executed.</returns> public static async Task StartDocumentProcessAsync( IServerlessLogger serverlessLogger, ILogger logger, string patternLog, IReadOnlyList <Document> documents, Func <Document, Task> func) { try { serverlessLogger.InjectLogger(logger); if (documents.Count == 0) { serverlessLogger.LogWarning($"{patternLog} No document to process."); return; } serverlessLogger.LogInformation($"{patternLog} Start processing..."); var documentCount = documents.Count; for (int documentIndex = 0; documentIndex < documentCount; documentIndex++) { var document = documents[documentIndex]; serverlessLogger.LogInformation($"{patternLog} Processing document index: {documentIndex}, id: {document.Id} and resourceId: {document.ResourceId}!"); await func(document).ConfigureAwait(false); serverlessLogger.LogInformation($"{patternLog} Document index {documentIndex} processed!"); } serverlessLogger.LogInformation($"{patternLog} Processing completed!"); } catch (Exception ex) { serverlessLogger.LogError(ex, $"{patternLog} Processing failed!"); throw; } }
/// <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="func">Process with business rules to be processed.</param> /// <returns>Task executed.</returns> public static async Task StartEventProcessAsync( IServerlessLogger serverlessLogger, ILogger logger, string patternLog, EventData[] events, Func <EventData, Task> func) { try { serverlessLogger.InjectLogger(logger); if (events.Length == 0) { serverlessLogger.LogWarning($"{patternLog} No event to process."); return; } serverlessLogger.LogInformation($"{patternLog} Start processing..."); var eventCount = events.Length; for (int eventIndex = 0; eventIndex < eventCount; eventIndex++) { serverlessLogger.LogInformation($"{patternLog} Processing event index {eventIndex}..."); await func(events[eventIndex]).ConfigureAwait(false); serverlessLogger.LogInformation($"{patternLog} Event index {eventIndex} processed!"); } serverlessLogger.LogInformation($"{patternLog} Processing completed!"); } catch (Exception ex) { serverlessLogger.LogError(ex, $"{patternLog} Processing failed!"); throw; } }