Refactor: Replace EmailSenderWorker with AsyncIniteWorker
Removed EmailSenderWorker and its configuration, including EmailSenderWorkerConfiguration. Introduced AsyncIniteWorker as a replacement, simplifying the design by removing configuration-based toggling. - Deleted EmailSenderWorkerConfiguration.cs. - Removed EmailSenderWorker and its registration from Program.cs. - Registered AsyncIniteWorker in DependencyInjection.cs. - Renamed and refactored EmailSenderWorker to AsyncIniteWorker. - Updated namespace and removed unused configuration dependencies.
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
namespace DigitalData.EmailProfiler.API.Configurations;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration for EmailSenderWorker
|
||||
/// </summary>
|
||||
public class EmailSenderWorkerConfiguration
|
||||
{
|
||||
public const string SectionName = "Workers:EmailSender";
|
||||
|
||||
public bool Enabled { get; set; } = true;
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
using DigitalData.EmailProfiler.API.Configurations;
|
||||
using DigitalData.EmailProfiler.API.Middleware;
|
||||
using DigitalData.EmailProfiler.API.Workers;
|
||||
using DigitalData.EmailProfiler.Application;
|
||||
using DigitalData.EmailProfiler.Application.Common.Dtos;
|
||||
using DigitalData.EmailProfiler.Infrastructure;
|
||||
@@ -43,16 +41,10 @@ try
|
||||
// Register Infrastructure layer (RabbitMQ, Repositories, etc.)
|
||||
builder.Services.AddInfrastructure(builder.Configuration);
|
||||
|
||||
// Register EmailSenderWorker configuration
|
||||
builder.Services.Configure<EmailSenderWorkerConfiguration>(builder.Configuration.GetSection(EmailSenderWorkerConfiguration.SectionName));
|
||||
|
||||
// Register EmailAccount configuration (IOptions<EmailAccountDto>)
|
||||
builder.Services.Configure<EmailAccountDto>(
|
||||
builder.Configuration.GetSection("EmailAccount"));
|
||||
|
||||
// Register Background Workers
|
||||
builder.Services.AddHostedService<EmailSenderWorker>();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
@@ -2,6 +2,7 @@ using DigitalData.EmailProfiler.Application.Common.Interfaces;
|
||||
using DigitalData.EmailProfiler.Infrastructure.Messaging;
|
||||
using DigitalData.EmailProfiler.Infrastructure.Queue;
|
||||
using DigitalData.EmailProfiler.Infrastructure.Services;
|
||||
using DigitalData.EmailProfiler.Infrastructure.Services.Background;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@@ -42,6 +43,9 @@ public static class DependencyInjection
|
||||
.PersistKeysToFileSystem(new DirectoryInfo(@"C:\ProgramData\EmailService\Keys"))
|
||||
.SetApplicationName("EmailProfiler");
|
||||
|
||||
// Register Background Workers
|
||||
services.AddHostedService<AsyncIniteWorker>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,20 @@
|
||||
using DigitalData.EmailProfiler.API.Configurations;
|
||||
using DigitalData.EmailProfiler.Application.Common.Interfaces;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DigitalData.EmailProfiler.API.Workers;
|
||||
namespace DigitalData.EmailProfiler.Infrastructure.Services.Background;
|
||||
|
||||
/// <summary>
|
||||
/// Background worker that processes outgoing emails from RabbitMQ queue.
|
||||
/// Uses event-driven RabbitMQ consumer (push-based) instead of polling.
|
||||
/// NO database operations - email account configuration comes from appsettings.
|
||||
/// </summary>
|
||||
public class EmailSenderWorker(
|
||||
public class AsyncIniteWorker(
|
||||
IOutgoingEmailQueue EmailQueue,
|
||||
ILogger<EmailSenderWorker> Logger,
|
||||
IOptions<EmailSenderWorkerConfiguration> configuration) : BackgroundService
|
||||
ILogger<AsyncIniteWorker> Logger) : BackgroundService
|
||||
{
|
||||
private readonly EmailSenderWorkerConfiguration _config = configuration.Value;
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
if (!_config.Enabled)
|
||||
{
|
||||
Logger.LogInformation("EmailSenderWorker is disabled in configuration");
|
||||
return;
|
||||
}
|
||||
|
||||
Logger.LogInformation("EmailSenderWorker started (event-driven RabbitMQ consumer)");
|
||||
|
||||
try
|
||||
@@ -32,7 +23,7 @@ public class EmailSenderWorker(
|
||||
await EmailQueue.InitAsync(stoppingToken);
|
||||
|
||||
// Keep worker alive until cancellation
|
||||
await Task.Delay(Timeout.Infinite, stoppingToken);
|
||||
// await Task.Delay(Timeout.Infinite, stoppingToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
@@ -44,6 +35,6 @@ public class EmailSenderWorker(
|
||||
throw;
|
||||
}
|
||||
|
||||
Logger.LogInformation("EmailSenderWorker stopped");
|
||||
// Logger.LogInformation("EmailSenderWorker stopped");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user