[FA-misc] Mass transit overhaul, needs testing and review

This commit is contained in:
gamer147
2026-01-21 23:16:31 -05:00
parent 055ef33666
commit f88f340d0a
97 changed files with 1150 additions and 858 deletions

View File

@@ -1,13 +0,0 @@
using FictionArchive.Service.Shared.Services.EventBus;
namespace FictionArchive.Service.UserNovelDataService.Models.IntegrationEvents;
public class ChapterCreatedEvent : IIntegrationEvent
{
public required uint ChapterId { get; init; }
public required uint NovelId { get; init; }
public required uint VolumeId { get; init; }
public required int VolumeOrder { get; init; }
public required uint ChapterOrder { get; init; }
public required string ChapterTitle { get; init; }
}

View File

@@ -1,13 +0,0 @@
using FictionArchive.Common.Enums;
using FictionArchive.Service.Shared.Services.EventBus;
namespace FictionArchive.Service.UserNovelDataService.Models.IntegrationEvents;
public class NovelCreatedEvent : IIntegrationEvent
{
public required uint NovelId { get; init; }
public required string Title { get; init; }
public required Language OriginalLanguage { get; init; }
public required string Source { get; init; }
public required string AuthorName { get; init; }
}

View File

@@ -1,15 +0,0 @@
using FictionArchive.Service.Shared.Services.EventBus;
namespace FictionArchive.Service.UserNovelDataService.Models.IntegrationEvents;
public class UserInvitedEvent : IIntegrationEvent
{
public Guid InvitedUserId { get; set; }
public required string InvitedUsername { get; set; }
public required string InvitedEmail { get; set; }
public required string InvitedOAuthProviderId { get; set; }
public Guid InviterId { get; set; }
public required string InviterUsername { get; set; }
public required string InviterOAuthProviderId { get; set; }
}

View File

@@ -1,9 +1,8 @@
using FictionArchive.Common.Extensions;
using FictionArchive.Service.Shared;
using FictionArchive.Service.Shared.Extensions;
using FictionArchive.Service.Shared.Services.EventBus.Implementations;
using FictionArchive.Service.Shared.MassTransit;
using FictionArchive.Service.UserNovelDataService.GraphQL;
using FictionArchive.Service.UserNovelDataService.Models.IntegrationEvents;
using FictionArchive.Service.UserNovelDataService.Services;
using FictionArchive.Service.UserNovelDataService.Services.EventHandlers;
@@ -22,17 +21,18 @@ public class Program
builder.Services.AddMemoryCache();
builder.Services.AddHealthChecks();
#region Event Bus
#region MassTransit
if (!isSchemaExport)
{
builder.Services.AddRabbitMQ(opt =>
{
builder.Configuration.GetSection("RabbitMQ").Bind(opt);
})
.Subscribe<NovelCreatedEvent, NovelCreatedEventHandler>()
.Subscribe<ChapterCreatedEvent, ChapterCreatedEventHandler>()
.Subscribe<UserInvitedEvent, UserInvitedEventHandler>();
builder.Services.AddFictionArchiveMassTransit<UserNovelDataServiceDbContext>(
builder.Configuration,
x =>
{
x.AddConsumer<NovelCreatedEventConsumer>();
x.AddConsumer<ChapterCreatedEventConsumer>();
x.AddConsumer<UserInvitedEventConsumer>();
});
}
#endregion

View File

@@ -1,25 +1,27 @@
using FictionArchive.Service.Shared.Services.EventBus;
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using FictionArchive.Service.UserNovelDataService.Models.Database;
using FictionArchive.Service.UserNovelDataService.Models.IntegrationEvents;
using MassTransit;
using Microsoft.EntityFrameworkCore;
namespace FictionArchive.Service.UserNovelDataService.Services.EventHandlers;
public class ChapterCreatedEventHandler : IIntegrationEventHandler<ChapterCreatedEvent>
public class ChapterCreatedEventConsumer : IConsumer<ChapterCreatedEvent>
{
private readonly UserNovelDataServiceDbContext _dbContext;
private readonly ILogger<ChapterCreatedEventHandler> _logger;
private readonly ILogger<ChapterCreatedEventConsumer> _logger;
public ChapterCreatedEventHandler(
public ChapterCreatedEventConsumer(
UserNovelDataServiceDbContext dbContext,
ILogger<ChapterCreatedEventHandler> logger)
ILogger<ChapterCreatedEventConsumer> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task Handle(ChapterCreatedEvent @event)
public async Task Consume(ConsumeContext<ChapterCreatedEvent> context)
{
var @event = context.Message;
// Ensure novel exists
var novelExists = await _dbContext.Novels.AnyAsync(n => n.Id == @event.NovelId);
if (!novelExists)

View File

@@ -1,25 +1,27 @@
using FictionArchive.Service.Shared.Services.EventBus;
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using FictionArchive.Service.UserNovelDataService.Models.Database;
using FictionArchive.Service.UserNovelDataService.Models.IntegrationEvents;
using MassTransit;
using Microsoft.EntityFrameworkCore;
namespace FictionArchive.Service.UserNovelDataService.Services.EventHandlers;
public class NovelCreatedEventHandler : IIntegrationEventHandler<NovelCreatedEvent>
public class NovelCreatedEventConsumer : IConsumer<NovelCreatedEvent>
{
private readonly UserNovelDataServiceDbContext _dbContext;
private readonly ILogger<NovelCreatedEventHandler> _logger;
private readonly ILogger<NovelCreatedEventConsumer> _logger;
public NovelCreatedEventHandler(
public NovelCreatedEventConsumer(
UserNovelDataServiceDbContext dbContext,
ILogger<NovelCreatedEventHandler> logger)
ILogger<NovelCreatedEventConsumer> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task Handle(NovelCreatedEvent @event)
public async Task Consume(ConsumeContext<NovelCreatedEvent> context)
{
var @event = context.Message;
var exists = await _dbContext.Novels.AnyAsync(n => n.Id == @event.NovelId);
if (exists)
{

View File

@@ -1,25 +1,27 @@
using FictionArchive.Service.Shared.Services.EventBus;
using FictionArchive.Service.Shared.MassTransit.Contracts.Events;
using FictionArchive.Service.UserNovelDataService.Models.Database;
using FictionArchive.Service.UserNovelDataService.Models.IntegrationEvents;
using MassTransit;
using Microsoft.EntityFrameworkCore;
namespace FictionArchive.Service.UserNovelDataService.Services.EventHandlers;
public class UserInvitedEventHandler : IIntegrationEventHandler<UserInvitedEvent>
public class UserInvitedEventConsumer : IConsumer<UserInvitedEvent>
{
private readonly UserNovelDataServiceDbContext _dbContext;
private readonly ILogger<UserInvitedEventHandler> _logger;
private readonly ILogger<UserInvitedEventConsumer> _logger;
public UserInvitedEventHandler(
public UserInvitedEventConsumer(
UserNovelDataServiceDbContext dbContext,
ILogger<UserInvitedEventHandler> logger)
ILogger<UserInvitedEventConsumer> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task Handle(UserInvitedEvent @event)
public async Task Consume(ConsumeContext<UserInvitedEvent> context)
{
var @event = context.Message;
var exists = await _dbContext.Users.AnyAsync(u => u.Id == @event.InvitedUserId);
if (exists)
{

View File

@@ -9,8 +9,10 @@
"DefaultConnection": "Host=localhost;Database=FictionArchive_UserNovelDataService;Username=postgres;password=postgres"
},
"RabbitMQ": {
"ConnectionString": "amqp://localhost",
"ClientIdentifier": "UserNovelDataService"
"Host": "localhost",
"VirtualHost": "/",
"Username": "guest",
"Password": "guest"
},
"OIDC": {
"Authority": "https://auth.orfl.xyz/application/o/fiction-archive/",