Add EmailAccountSyncWorker and configure EmailAccount entity

Introduced the `EmailAccountSyncWorker` background service to initialize and synchronize email accounts using RabbitMQ for event-driven processing. The service resolves email account configurations from application settings and upserts them into the repository.

Updated `MessagingServiceDbContext` to configure the `EmailAccount` entity, setting the `Username` property to use the `SQL_Latin1_General_CP1_CI_AS` collation for case-insensitive comparisons.

Added necessary `using` directives in `DependencyInjection.cs` to integrate new dependencies for AutoMapper, repositories, services, and background processing.
This commit is contained in:
2026-08-13 09:37:39 +02:00
parent 893addb45d
commit 8b4d1e48f5
3 changed files with 34 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
using AutoMapper;
using DigitalData.MessagingService.Application.Common.Interfaces;
using DigitalData.MessagingService.Application.Common.Interfaces.Repositories;
using DigitalData.MessagingService.Infrastructure.Mappings;
using DigitalData.MessagingService.Infrastructure.Persistence;
using DigitalData.MessagingService.Infrastructure.Queue;
using DigitalData.MessagingService.Infrastructure.Repositories;

View File

@@ -16,6 +16,12 @@ public class MessagingServiceDbContext(DbContextOptions<MessagingServiceDbContex
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<EmailAccount>(entity =>
{
entity.Property(e => e.Username)
.UseCollation("SQL_Latin1_General_CP1_CI_AS");
});
modelBuilder.Entity<ReceivedEmail>(entity =>
{
entity.HasMany(e => e.Attachments)

View File

@@ -0,0 +1,26 @@
using DigitalData.MessagingService.Application.Common.Interfaces.Repositories;
using DigitalData.MessagingService.Application.Common.Options;
using DigitalData.MessagingService.Domain.Entities;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
namespace DigitalData.MessagingService.Infrastructure.Services.Background;
/// <summary>
/// A hosted background service responsible for initializing the competing email consumer pool.
/// Leverages a push-based, event-driven RabbitMQ consumer to eliminate polling overhead.
/// Email account configuration is resolved exclusively from application settings; no database access is performed.
/// </summary>
public class EmailAccountSyncWorker(IRepository<EmailAccount> Repository, IOptions<EmailAccountsOptions> Options) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
foreach (var account in Options.Value.Accounts)
{
if (account is EmailAccount emailAccount)
await Repository.UpsertAsync(a => a.Username == emailAccount.Username, emailAccount, stoppingToken);
}
await Task.Delay(Timeout.Infinite, stoppingToken).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
}
}