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