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.Repositories;
|
||||
using DigitalData.MessagingService.Application.Common.Options;
|
||||
using DigitalData.MessagingService.Domain.Entities;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
@@ -31,23 +25,23 @@ public class EmailSyncWorker(IImapEmailService imapService, IOptions<EmailAccoun
|
||||
|
||||
var emailAccountRepo = scope.ServiceProvider.GetRequiredService<IRepository<EmailAccount>>();
|
||||
|
||||
var imapService = scope.ServiceProvider.GetRequiredService<IImapEmailService>();
|
||||
|
||||
foreach (var account in await emailAccountRepo.GetAllAsync(stoppingToken))
|
||||
if (account.ImapServer is not null)
|
||||
{
|
||||
// init or update last date filter
|
||||
_dateFilter = _dateFilter is null
|
||||
? new DateFilter
|
||||
{
|
||||
After = null,
|
||||
Before = DateTime.UtcNow
|
||||
}
|
||||
: new DateFilter
|
||||
{
|
||||
After = _dateFilter.Before,
|
||||
Before = DateTime.UtcNow
|
||||
};
|
||||
|
||||
await limapService.SyncEmailsAsync(account, _dateFilter, cancel: stoppingToken);
|
||||
Logger.LogDebug("Email synchronization has started for account {username} in folder {folder}.", account.Username, DefaultFolder);
|
||||
|
||||
try
|
||||
{
|
||||
var res = await imapService.SyncEmailsAsync(account, DefaultFolder, stoppingToken);
|
||||
Logger.LogDebug("Email synchronization has completed for account {username} in folder {folder}. Processed: {processedCount}, Failed: {failedCount}", account.Username, DefaultFolder, res.ProcessedCount, res.FailedCount);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
// Log the exception or handle it as needed
|
||||
Logger.LogError(ex, "Error syncing emails for account {username}", account.Username);
|
||||
}
|
||||
}
|
||||
|
||||
await Task.Delay(interval, stoppingToken).ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
|
||||
|
||||
Reference in New Issue
Block a user