using MassTransit; using Microsoft.Extensions.DependencyInjection; using LCU.Api.Internal.MaidBotLib.Models; using LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices; using System.Collections.Generic; using LCU.Library.Internal.Logging.Performance; namespace LCU.Api.Internal.Dispatcher.Consumers; public class ExecutionConsumer : IConsumer { private readonly IServiceScopeFactory _scopeFactory; public ExecutionConsumer(IServiceScopeFactory scopeFactory) { _scopeFactory = scopeFactory; } public async Task Consume(ConsumeContext context) { try { using var scope = _scopeFactory.CreateAsyncScope(); var performanceMonitor = new PerformanceMonitor("DispatcherExecutionConsumerMonitor", Guid.NewGuid()); await scope.ServiceProvider.GetRequiredService().HandleExecutionRequest(context.Message); performanceMonitor.LogPerformance((new Dictionary(){ {"WorkItemId", context.Message.WorkItemId.ToString()}, {"WorkItemExecutionId", context.Message.WorkItemExecutionId.ToString()} })); } catch (ServiceException serviceException) { Log.ForContext(Enum.GetName(typeof(PropertyEnum), PropertyEnum.Group), Enum.GetName(typeof(GroupEnum), GroupEnum.ITDevelopment)) .Error(serviceException, $"Dispatcher Execution Failure. " + $"The Dispatcher Execution Request is placed on LCU.Dispatcher.Execution_error."); throw; } catch (Exception exception) { Log.ForContext(Enum.GetName(typeof(PropertyEnum), PropertyEnum.Group), Enum.GetName(typeof(GroupEnum), GroupEnum.ITDevelopment)) .Fatal(exception, $"Unexpected Dispatcher Execution Failure. " + $"The Dispatcher Execution Request is placed on LCU.Dispatcher.Execution_error."); throw; } } }