diff --git a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/Background/EmailSyncWorker.cs b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/Background/EmailSyncWorker.cs index 706381f..c4e184a 100644 --- a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/Background/EmailSyncWorker.cs +++ b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/Background/EmailSyncWorker.cs @@ -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 Options, IServiceProvider Provider) : BackgroundService +public class EmailSyncWorker(IOptions Options, IServiceProvider Provider, ILogger 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>(); + var imapService = scope.ServiceProvider.GetRequiredService(); + 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);