[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

@@ -0,0 +1,74 @@
using System.Text.Json;
using FictionArchive.Service.ReportingService.Models.Database;
using FictionArchive.Service.Shared.MassTransit.Contracts;
using MassTransit;
namespace FictionArchive.Service.ReportingService.Services.Consumers;
public class JobStateChangedEventConsumer : IConsumer<JobStateChangedEvent>
{
private readonly ReportingServiceDbContext _dbContext;
private readonly ILogger<JobStateChangedEventConsumer> _logger;
public JobStateChangedEventConsumer(
ReportingServiceDbContext dbContext,
ILogger<JobStateChangedEventConsumer> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task Consume(ConsumeContext<JobStateChangedEvent> context)
{
var @event = context.Message;
var job = await _dbContext.Jobs.FindAsync(@event.JobId);
if (job == null)
{
job = new Job
{
Id = @event.JobId,
JobType = @event.JobType,
Status = @event.ToState,
CreatedTime = @event.Timestamp,
UpdatedTime = @event.Timestamp,
Metadata = @event.Metadata != null
? JsonSerializer.SerializeToDocument(@event.Metadata)
: null
};
_dbContext.Jobs.Add(job);
}
else
{
job.Status = @event.ToState;
job.UpdatedTime = @event.Timestamp;
if (@event.Error != null)
{
job.ErrorMessage = @event.Error;
}
if (@event.ToState is "Completed" or "Failed")
{
job.CompletedTime = @event.Timestamp;
}
}
var historyEntry = new JobHistoryEntry
{
JobId = @event.JobId,
FromState = @event.FromState,
ToState = @event.ToState,
Message = @event.Message,
Error = @event.Error,
Timestamp = @event.Timestamp
};
_dbContext.JobHistoryEntries.Add(historyEntry);
await _dbContext.SaveChangesAsync();
_logger.LogDebug("Recorded job state change: {JobId} {FromState} -> {ToState}",
@event.JobId, @event.FromState, @event.ToState);
}
}

View File

@@ -0,0 +1,36 @@
using FictionArchive.Service.ReportingService.Models.Database;
using FictionArchive.Service.Shared.Services.Database;
using Microsoft.EntityFrameworkCore;
namespace FictionArchive.Service.ReportingService.Services;
public class ReportingServiceDbContext : FictionArchiveDbContext
{
public ReportingServiceDbContext(DbContextOptions options, ILogger<ReportingServiceDbContext> logger)
: base(options, logger) { }
public DbSet<Job> Jobs => Set<Job>();
public DbSet<JobHistoryEntry> JobHistoryEntries => Set<JobHistoryEntry>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Job>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasIndex(e => e.JobType);
entity.HasIndex(e => e.Status);
entity.HasIndex(e => e.CreatedTime);
});
modelBuilder.Entity<JobHistoryEntry>(entity =>
{
entity.HasKey(e => e.Id);
entity.HasOne(e => e.Job)
.WithMany(j => j.History)
.HasForeignKey(e => e.JobId)
.OnDelete(DeleteBehavior.Cascade);
});
}
}