Refactor EmailSenderWorker configuration handling
Introduced a dedicated `EmailSenderWorkerConfiguration` class to centralize and simplify configuration management for the `EmailSenderWorker`. Updated `Program.cs` to use this class for dependency injection and removed the inline configuration logic from `EmailSenderWorker.cs`. Simplified the worker's constructor by leveraging `IOptions<EmailSenderWorkerConfiguration>`. Removed the unused `MaxRetryCount` property from the configuration class and `appsettings.json`. Cleaned up `using` directives in `Program.cs` and `EmailSenderWorker.cs` to include the new namespace and remove redundant imports. These changes improve maintainability and align with best practices.
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
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,4 +1,4 @@
|
|||||||
using DigitalData.EmailProfiler.API;
|
using DigitalData.EmailProfiler.API.Configurations;
|
||||||
using DigitalData.EmailProfiler.API.Middleware;
|
using DigitalData.EmailProfiler.API.Middleware;
|
||||||
using DigitalData.EmailProfiler.API.Workers;
|
using DigitalData.EmailProfiler.API.Workers;
|
||||||
using DigitalData.EmailProfiler.Application;
|
using DigitalData.EmailProfiler.Application;
|
||||||
@@ -44,8 +44,7 @@ try
|
|||||||
builder.Services.AddInfrastructure(builder.Configuration);
|
builder.Services.AddInfrastructure(builder.Configuration);
|
||||||
|
|
||||||
// Register EmailSenderWorker configuration
|
// Register EmailSenderWorker configuration
|
||||||
builder.Services.Configure<EmailSenderWorkerConfiguration>(
|
builder.Services.Configure<EmailSenderWorkerConfiguration>(builder.Configuration.GetSection(EmailSenderWorkerConfiguration.SectionName));
|
||||||
builder.Configuration.GetSection(EmailSenderWorkerConfiguration.SectionName));
|
|
||||||
|
|
||||||
// Register EmailAccount configuration (IOptions<EmailAccountDto>)
|
// Register EmailAccount configuration (IOptions<EmailAccountDto>)
|
||||||
builder.Services.Configure<EmailAccountDto>(
|
builder.Services.Configure<EmailAccountDto>(
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
|
using DigitalData.EmailProfiler.API.Configurations;
|
||||||
using DigitalData.EmailProfiler.Application.Common.Events;
|
using DigitalData.EmailProfiler.Application.Common.Events;
|
||||||
using DigitalData.EmailProfiler.Application.Common.Interfaces;
|
using DigitalData.EmailProfiler.Application.Common.Interfaces;
|
||||||
using DigitalData.EmailProfiler.Application.EmailSending.Commands;
|
using DigitalData.EmailProfiler.Application.EmailSending.Commands;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace DigitalData.EmailProfiler.API.Workers;
|
namespace DigitalData.EmailProfiler.API.Workers;
|
||||||
|
|
||||||
@@ -13,11 +15,9 @@ public class EmailSenderWorker(
|
|||||||
IEmailQueue EmailQueue,
|
IEmailQueue EmailQueue,
|
||||||
IEmailService EmailService,
|
IEmailService EmailService,
|
||||||
ILogger<EmailSenderWorker> Logger,
|
ILogger<EmailSenderWorker> Logger,
|
||||||
IConfiguration configuration) : BackgroundService
|
IOptions<EmailSenderWorkerConfiguration> configuration) : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly EmailSenderWorkerConfiguration _config =
|
private readonly EmailSenderWorkerConfiguration _config = configuration.Value;
|
||||||
configuration.GetSection(EmailSenderWorkerConfiguration.SectionName)
|
|
||||||
.Get<EmailSenderWorkerConfiguration>() ?? new EmailSenderWorkerConfiguration();
|
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
@@ -80,20 +80,4 @@ public class EmailSenderWorker(
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Configuration for EmailSenderWorker
|
|
||||||
/// </summary>
|
|
||||||
public class EmailSenderWorkerConfiguration
|
|
||||||
{
|
|
||||||
public const string SectionName = "Workers:EmailSender";
|
|
||||||
|
|
||||||
public bool Enabled { get; set; } = true;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Maximum retry attempts before moving to Dead Letter Queue
|
|
||||||
/// NOTE: Currently NOT used - all failed emails move directly to DLQ without retry
|
|
||||||
/// </summary>
|
|
||||||
public int MaxRetryCount { get; set; } = 3;
|
|
||||||
}
|
|
||||||
@@ -13,8 +13,7 @@
|
|||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"Workers": {
|
"Workers": {
|
||||||
"EmailSender": {
|
"EmailSender": {
|
||||||
"Enabled": true,
|
"Enabled": true
|
||||||
"MaxRetryCount": 3
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"LuckyPennySoftLicenseKey": "eyJhbGciOiJSUzI1NiIsImtpZCI6Ikx1Y2t5UGVubnlTb2Z0d2FyZUxpY2Vuc2VLZXkvYmJiMTNhY2I1OTkwNGQ4OWI0Y2IxYzg1ZjA4OGNjZjkiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2x1Y2t5cGVubnlzb2Z0d2FyZS5jb20iLCJhdWQiOiJMdWNreVBlbm55U29mdHdhcmUiLCJleHAiOiIxODE2MTI4MDAwIiwiaWF0IjoiMTc4NDYyNDU1NyIsImFjY291bnRfaWQiOiIwMTk4M2M1OWU0YjM3MjhlYmZkMzEwM2MyYTQ4NmU4NSIsImN1c3RvbWVyX2lkIjoiMDE5ODNjNTllNGIzNzI4ZWJmZDMxMDNjMmE0ODZlODUiLCJzdWJfaWQiOiItIiwiZWRpdGlvbiI6IjAiLCJ0eXBlIjoiMiJ9.IUUO926m9crYGYxMjjKD_n9BnUm-EDyjFIn0YmMUCo7C-QTwvB8WhXP8veTSFsBq-leIIDJ4jyl7Pgc_7ciwg1XhUSIs4mkQroEUaSFCGOxw7Pi41WM8MK5YFSa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