[FA-misc] Initial MassTransit implementation seems to work
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
using FictionArchive.Service.TranslationService.Contracts;
|
||||
using FictionArchive.Service.TranslationService.Models.Enums;
|
||||
using FictionArchive.Service.TranslationService.Services;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Consumers;
|
||||
|
||||
public class TranslationRequestCreatedConsumer : IConsumer<ITranslationRequestCreated>
|
||||
{
|
||||
private readonly ILogger<TranslationRequestCreatedConsumer> _logger;
|
||||
private readonly TranslationEngineService _translationEngineService;
|
||||
private readonly IPublishEndpoint _publishEndpoint;
|
||||
|
||||
public TranslationRequestCreatedConsumer(
|
||||
ILogger<TranslationRequestCreatedConsumer> logger,
|
||||
TranslationEngineService translationEngineService,
|
||||
IPublishEndpoint publishEndpoint)
|
||||
{
|
||||
_logger = logger;
|
||||
_translationEngineService = translationEngineService;
|
||||
_publishEndpoint = publishEndpoint;
|
||||
}
|
||||
|
||||
public async Task Consume(ConsumeContext<ITranslationRequestCreated> context)
|
||||
{
|
||||
var message = context.Message;
|
||||
|
||||
_logger.LogInformation("Processing translation request {TranslationRequestId}", message.TranslationRequestId);
|
||||
|
||||
var result = await _translationEngineService.Translate(
|
||||
message.From,
|
||||
message.To,
|
||||
message.Body,
|
||||
message.TranslationEngineKey);
|
||||
|
||||
if (result.Status == TranslationRequestStatus.Success)
|
||||
{
|
||||
await _publishEndpoint.Publish<ITranslationRequestCompleted>(
|
||||
new TranslationRequestCompleted(
|
||||
TranslationRequestId: message.TranslationRequestId,
|
||||
TranslatedText: result.TranslatedText));
|
||||
|
||||
_logger.LogInformation("Translation completed for request {TranslationRequestId}", message.TranslationRequestId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError("Translation failed for request {TranslationRequestId}", message.TranslationRequestId);
|
||||
throw new InvalidOperationException($"Translation failed for request {message.TranslationRequestId}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using FictionArchive.Service.Shared.Contracts.Events;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Contracts;
|
||||
|
||||
public record TranslationRequestCompleted(
|
||||
Guid? TranslationRequestId,
|
||||
string? TranslatedText) : ITranslationRequestCompleted;
|
||||
@@ -1,18 +0,0 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.TranslationService.Models.Enums;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Models.IntegrationEvents;
|
||||
|
||||
public class TranslationRequestCompletedEvent : IIntegrationEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps this event back to a triggering request.
|
||||
/// </summary>
|
||||
public Guid? TranslationRequestId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The resulting text.
|
||||
/// </summary>
|
||||
public string? TranslatedText { get; set; }
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Models.IntegrationEvents;
|
||||
|
||||
public class TranslationRequestCreatedEvent : IIntegrationEvent
|
||||
{
|
||||
public Guid TranslationRequestId { get; set; }
|
||||
public Language From { get; set; }
|
||||
public Language To { get; set; }
|
||||
public string Body { get; set; }
|
||||
public string TranslationEngineKey { get; set; }
|
||||
}
|
||||
@@ -2,16 +2,13 @@ using DeepL;
|
||||
using FictionArchive.Common.Extensions;
|
||||
using FictionArchive.Service.Shared;
|
||||
using FictionArchive.Service.Shared.Extensions;
|
||||
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
using FictionArchive.Service.Shared.Services.GraphQL;
|
||||
using FictionArchive.Service.TranslationService.Consumers;
|
||||
using FictionArchive.Service.TranslationService.GraphQL;
|
||||
using FictionArchive.Service.TranslationService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.TranslationService.Services;
|
||||
using FictionArchive.Service.TranslationService.Services.Database;
|
||||
using FictionArchive.Service.TranslationService.Services.EventHandlers;
|
||||
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
|
||||
using FictionArchive.Service.TranslationService.Services.TranslationEngines.DeepLTranslate;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService;
|
||||
|
||||
@@ -26,15 +23,16 @@ public class Program
|
||||
|
||||
builder.Services.AddHealthChecks();
|
||||
|
||||
#region Event Bus
|
||||
#region MassTransit
|
||||
|
||||
if (!isSchemaExport)
|
||||
{
|
||||
builder.Services.AddRabbitMQ(opt =>
|
||||
{
|
||||
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
|
||||
})
|
||||
.Subscribe<TranslationRequestCreatedEvent, TranslationRequestCreatedEventHandler>();
|
||||
builder.Services.AddFictionArchiveMassTransit(
|
||||
builder.Configuration,
|
||||
x =>
|
||||
{
|
||||
x.AddConsumer<TranslationRequestCreatedConsumer>();
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.TranslationService.Models.Enums;
|
||||
using FictionArchive.Service.TranslationService.Models.IntegrationEvents;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Services.EventHandlers;
|
||||
|
||||
public class TranslationRequestCreatedEventHandler : IIntegrationEventHandler<TranslationRequestCreatedEvent>
|
||||
{
|
||||
private readonly ILogger<TranslationRequestCreatedEventHandler> _logger;
|
||||
private readonly TranslationEngineService _translationEngineService;
|
||||
private readonly IEventBus _eventBus;
|
||||
|
||||
public TranslationRequestCreatedEventHandler(ILogger<TranslationRequestCreatedEventHandler> logger, TranslationEngineService translationEngineService)
|
||||
{
|
||||
_logger = logger;
|
||||
_translationEngineService = translationEngineService;
|
||||
}
|
||||
|
||||
public async Task Handle(TranslationRequestCreatedEvent @event)
|
||||
{
|
||||
var result = await _translationEngineService.Translate(@event.From, @event.To, @event.Body, @event.TranslationEngineKey);
|
||||
if (result.Status == TranslationRequestStatus.Success)
|
||||
{
|
||||
await _eventBus.Publish(new TranslationRequestCompletedEvent()
|
||||
{
|
||||
TranslatedText = result.TranslatedText,
|
||||
TranslationRequestId = @event.TranslationRequestId,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,21 @@
|
||||
using System.Text;
|
||||
using FictionArchive.Common.Enums;
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
|
||||
using FictionArchive.Service.TranslationService.Models;
|
||||
using FictionArchive.Service.TranslationService.Models.Database;
|
||||
using FictionArchive.Service.TranslationService.Models.Enums;
|
||||
using FictionArchive.Service.TranslationService.Models.IntegrationEvents;
|
||||
using FictionArchive.Service.TranslationService.Services.Database;
|
||||
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace FictionArchive.Service.TranslationService.Services;
|
||||
|
||||
public class TranslationEngineService
|
||||
{
|
||||
private readonly IEnumerable<ITranslationEngine> _translationEngines;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly TranslationServiceDbContext _dbContext;
|
||||
|
||||
public TranslationEngineService(IEnumerable<ITranslationEngine> translationEngines, TranslationServiceDbContext dbContext, IEventBus eventBus)
|
||||
public TranslationEngineService(IEnumerable<ITranslationEngine> translationEngines, TranslationServiceDbContext dbContext)
|
||||
{
|
||||
_translationEngines = translationEngines;
|
||||
_dbContext = dbContext;
|
||||
_eventBus = eventBus;
|
||||
}
|
||||
|
||||
public async Task<TranslationResult> Translate(Language from, Language to, string text, string translationEngineKey)
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning"
|
||||
}
|
||||
},
|
||||
"DeepL": {
|
||||
|
||||
Reference in New Issue
Block a user