using LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices; using LCU.Api.Internal.MaidBotLib.Models; using LCU.Library.Internal.DispatcherModels.Models.Requests; using LCU.Library.Internal.Logging.Performance; using MassTransit; using Microsoft.Extensions.DependencyInjection; using System.Collections.Generic; namespace LCU.Api.Internal.Dispatcher.Consumers { public class SendErroredJobAlertConsumer : IConsumer> { private readonly IServiceScopeFactory _scopeFactory; public SendErroredJobAlertConsumer(IServiceScopeFactory scopeFactory) { _scopeFactory = scopeFactory; } public async Task Consume(ConsumeContext> context) { try { using var scope = _scopeFactory.CreateAsyncScope(); var performanceMonitor = new PerformanceMonitor("DispatcherBatchErroredJobsConsumerMonitor", Guid.NewGuid()); var batchedErroredJobAlertRequestlist = new List(); for (int index = 0; index < context.Message.Length; index++) { ConsumeContext messageContext = context.Message[index]; batchedErroredJobAlertRequestlist.Add(messageContext.Message); } await scope.ServiceProvider.GetRequiredService().HandleSendingErroredJobAlerts(batchedErroredJobAlertRequestlist); performanceMonitor.LogPerformance((new Dictionary(){ {"NumberOfBatchedErrors", batchedErroredJobAlertRequestlist.Count().ToString()} })); } catch (ServiceException serviceException) { Log.ForContext(Enum.GetName(typeof(PropertyEnum), PropertyEnum.Group), Enum.GetName(typeof(GroupEnum), GroupEnum.ITDevelopment)) .Error(serviceException, $"Dispatcher Send Errored Job Alert. " + $"The Dispatcher Send Errored Job Alert Request is placed on LCU.Dispatcher.SendErroredJobAlert_error."); throw; } catch (Exception exception) { Log.ForContext(Enum.GetName(typeof(PropertyEnum), PropertyEnum.Group), Enum.GetName(typeof(GroupEnum), GroupEnum.ITDevelopment)) .Fatal(exception, $"Unexpected Dispatcher Send Errored Job Alert. " + $"The Dispatcher Send Errored Job Alert Request is placed on LCU.Dispatcher.SendErroredJobAlert_error."); throw; } } } }