refactor(infrastructure): resolve IImapEmailService per-iteration from scoped DI in EmailSyncWorker; add structured logging and per-account error handling
This commit is contained in:
@@ -1,28 +1,22 @@
|
|||||||
using DigitalData.MessagingService.Application.Common.Dto.MailSearch;
|
|
||||||
using DigitalData.MessagingService.Application.Common.Interfaces;
|
using DigitalData.MessagingService.Application.Common.Interfaces;
|
||||||
using DigitalData.MessagingService.Application.Common.Interfaces.Repositories;
|
using DigitalData.MessagingService.Application.Common.Interfaces.Repositories;
|
||||||
using DigitalData.MessagingService.Application.Common.Options;
|
using DigitalData.MessagingService.Application.Common.Options;
|
||||||
using DigitalData.MessagingService.Domain.Entities;
|
using DigitalData.MessagingService.Domain.Entities;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace DigitalData.MessagingService.Infrastructure.Services.Background;
|
namespace DigitalData.MessagingService.Infrastructure.Services.Background;
|
||||||
|
|
||||||
public class EmailSyncWorker(IImapEmailService imapService, IOptions<EmailAccountsOptions> Options, IServiceProvider Provider) : BackgroundService
|
public class EmailSyncWorker(IOptions<EmailAccountsOptions> Options, IServiceProvider Provider, ILogger<EmailSyncWorker> Logger) : BackgroundService
|
||||||
{
|
{
|
||||||
private DateFilter? _dateFilter = null;
|
private readonly string DefaultFolder = "INBOX";
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
await UpsertSeedEmailAccount(stoppingToken);
|
await UpsertSeedEmailAccount(stoppingToken);
|
||||||
|
|
||||||
if (imapService is not LimilabsImapEmailService limapService)
|
|
||||||
{
|
|
||||||
await Task.Delay(Timeout.Infinite, stoppingToken).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var interval = TimeSpan.FromSeconds(Options.Value.SyncIntervalSeconds);
|
var interval = TimeSpan.FromSeconds(Options.Value.SyncIntervalSeconds);
|
||||||
|
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
@@ -31,23 +25,23 @@ public class EmailSyncWorker(IImapEmailService imapService, IOptions<EmailAccoun
|
|||||||
|
|
||||||
var emailAccountRepo = scope.ServiceProvider.GetRequiredService<IRepository<EmailAccount>>();
|
var emailAccountRepo = scope.ServiceProvider.GetRequiredService<IRepository<EmailAccount>>();
|
||||||
|
|
||||||
|
var imapService = scope.ServiceProvider.GetRequiredService<IImapEmailService>();
|
||||||
|
|
||||||
foreach (var account in await emailAccountRepo.GetAllAsync(stoppingToken))
|
foreach (var account in await emailAccountRepo.GetAllAsync(stoppingToken))
|
||||||
if (account.ImapServer is not null)
|
if (account.ImapServer is not null)
|
||||||
{
|
{
|
||||||
// init or update last date filter
|
Logger.LogDebug("Email synchronization has started for account {username} in folder {folder}.", account.Username, DefaultFolder);
|
||||||
_dateFilter = _dateFilter is null
|
|
||||||
? new DateFilter
|
try
|
||||||
{
|
{
|
||||||
After = null,
|
var res = await imapService.SyncEmailsAsync(account, DefaultFolder, stoppingToken);
|
||||||
Before = DateTime.UtcNow
|
Logger.LogDebug("Email synchronization has completed for account {username} in folder {folder}. Processed: {processedCount}, Failed: {failedCount}", account.Username, DefaultFolder, res.ProcessedCount, res.FailedCount);
|
||||||
}
|
}
|
||||||
: new DateFilter
|
catch(Exception ex)
|
||||||
{
|
{
|
||||||
After = _dateFilter.Before,
|
// Log the exception or handle it as needed
|
||||||
Before = DateTime.UtcNow
|
Logger.LogError(ex, "Error syncing emails for account {username}", account.Username);
|
||||||
};
|
}
|
||||||
|
|
||||||
await limapService.SyncEmailsAsync(account, _dateFilter, cancel: stoppingToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.Delay(interval, stoppingToken).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
|
await Task.Delay(interval, stoppingToken).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
|
||||||
|
|||||||
Reference in New Issue
Block a user