diff --git a/src/DigitalData.EmailProfiler.Application/Common/Interfaces/IOutgoingEmailQueue.cs b/src/DigitalData.EmailProfiler.Application/Common/Interfaces/IOutgoingEmailQueue.cs index fe9a1a2..8785a6a 100644 --- a/src/DigitalData.EmailProfiler.Application/Common/Interfaces/IOutgoingEmailQueue.cs +++ b/src/DigitalData.EmailProfiler.Application/Common/Interfaces/IOutgoingEmailQueue.cs @@ -11,10 +11,4 @@ public interface IOutgoingEmailQueue Task EnqueueAsync(OutgoingEmailEvent outgoingEmailEvent, CancellationToken cancellationToken = default); Task GetQueueDepthAsync(CancellationToken cancellationToken = default); - - /// - /// Initialize RabbitMQ connection, channel, exchanges, and queues asynchronously. - /// Called lazily on first use via EnsureInitializedAsync. - /// - Task InitAsync(CancellationToken cancellationToken = default); } diff --git a/src/DigitalData.EmailProfiler.Infrastructure/DependencyInjection.cs b/src/DigitalData.EmailProfiler.Infrastructure/DependencyInjection.cs index fc1b03c..66ec492 100644 --- a/src/DigitalData.EmailProfiler.Infrastructure/DependencyInjection.cs +++ b/src/DigitalData.EmailProfiler.Infrastructure/DependencyInjection.cs @@ -44,7 +44,7 @@ public static class DependencyInjection .SetApplicationName("EmailProfiler"); // Register Background Workers - services.AddHostedService(); + services.AddHostedService(); return services; } diff --git a/src/DigitalData.EmailProfiler.Infrastructure/Services/Background/AsyncInitWorker.cs b/src/DigitalData.EmailProfiler.Infrastructure/Services/Background/AsyncInitWorker.cs new file mode 100644 index 0000000..66a5cb8 --- /dev/null +++ b/src/DigitalData.EmailProfiler.Infrastructure/Services/Background/AsyncInitWorker.cs @@ -0,0 +1,32 @@ +using DigitalData.EmailProfiler.Application.Common.Interfaces; +using DigitalData.EmailProfiler.Infrastructure.Queue; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace DigitalData.EmailProfiler.Infrastructure.Services.Background; + +/// +/// A hosted background service responsible for initializing the outgoing email queue consumer. +/// 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. +/// +public class AsyncInitWorker(IOutgoingEmailQueue EmailQueue, ILogger Logger) : BackgroundService +{ + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + Logger.LogInformation("Outgoing email queue worker is starting. Initializing event-driven RabbitMQ consumer."); + + try + { + // Initialize the RabbitMQ push-based consumer. This call is non-blocking; + // message processing is handled asynchronously via registered event callbacks. + if (EmailQueue is OutgoingEmailQueue outgoingEmailQueue) + await outgoingEmailQueue.InitAsync(stoppingToken); + } + catch (Exception ex) + { + Logger.LogError(ex, "A critical error occurred while initializing the outgoing email queue consumer. The worker cannot proceed."); + throw; + } + } +} \ No newline at end of file diff --git a/src/DigitalData.EmailProfiler.Infrastructure/Services/Background/AsyncIniteWorker.cs b/src/DigitalData.EmailProfiler.Infrastructure/Services/Background/AsyncIniteWorker.cs deleted file mode 100644 index 9f145a0..0000000 --- a/src/DigitalData.EmailProfiler.Infrastructure/Services/Background/AsyncIniteWorker.cs +++ /dev/null @@ -1,40 +0,0 @@ -using DigitalData.EmailProfiler.Application.Common.Interfaces; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; - -namespace DigitalData.EmailProfiler.Infrastructure.Services.Background; - -/// -/// 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. -/// -public class AsyncIniteWorker( - IOutgoingEmailQueue EmailQueue, - ILogger Logger) : BackgroundService -{ - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - Logger.LogInformation("EmailSenderWorker started (event-driven RabbitMQ consumer)"); - - try - { - // Start RabbitMQ consumer (event-driven, non-blocking) - await EmailQueue.InitAsync(stoppingToken); - - // Keep worker alive until cancellation - // await Task.Delay(Timeout.Infinite, stoppingToken); - } - catch (OperationCanceledException) - { - Logger.LogInformation("EmailSenderWorker is stopping"); - } - catch (Exception ex) - { - Logger.LogError(ex, "EmailSenderWorker failed to start"); - throw; - } - - // Logger.LogInformation("EmailSenderWorker stopped"); - } -} \ No newline at end of file