Files
monorepo/LCU.API.Internal.Dispatcher/LCU.Api.Internal.Dispatcher/Services/QueueServices/QueueCompletionService.cs
T
2026-03-24 15:26:03 -05:00

45 lines
2.0 KiB
C#

using System.Collections.Generic;
using System.Threading;
using MassTransit;
using Newtonsoft.Json;
using ApiResponse = LCU.Library.Internal.ApiResponse.Models;
using LCU.Api.Internal.MaidBotLib.Models;
using LCU.Api.Internal.Dispatcher.Configurations;
namespace LCU.Api.Internal.Dispatcher.Services.QueueServices;
public class QueueCompletionService : IQueueCompletionService
{
private readonly ISendEndpoint _sendEndpoint;
private readonly ReceiveEndpointConfigurations _queueReceiveEndpointsConfiguration;
public QueueCompletionService(IBusControl busControl,
ReceiveEndpointConfigurations queueReceiveEndpointsConfiguration)
{
_queueReceiveEndpointsConfiguration = queueReceiveEndpointsConfiguration;
_sendEndpoint = busControl.GetSendEndpoint(_queueReceiveEndpointsConfiguration.CompletionReceiveEndpoint.ReceiveEndpointUri).Result;
}
public async Task QueueCompletion(DispatcherReturnResponse request, List<string> messages = null, bool successful = true)
{
try
{
ArgumentNullException.ThrowIfNull(request);
var response = new ApiResponse.Response()
{
Successful = successful,
ResponseStack = messages == null ? new List<string>() : messages,
ResponsePayload = JsonConvert.SerializeObject(request)
};
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
await _sendEndpoint.Send(response, cancellationTokenSource.Token);
}
catch (Exception exception)
{
Log.ForContext(Enum.GetName(typeof(PropertyEnum), PropertyEnum.Group),
Enum.GetName(typeof(GroupEnum), GroupEnum.ITDevelopment))
.Fatal(exception, $"Failed to queue completion workitem request. WorkItemId: {request?.WorkItemId}");
throw new ServiceException($"Failed to queue completion workitem request. WorkItemId: {request?.WorkItemId}");
}
}
}