namespace Socialize.Api.Modules.CalendarIntegrations.Services; public sealed class CalendarImportBackgroundService( IServiceScopeFactory scopeFactory, ILogger logger) : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { using PeriodicTimer timer = new(TimeSpan.FromHours(6)); while (!stoppingToken.IsCancellationRequested) { await RefreshDueSourcesAsync(stoppingToken); await timer.WaitForNextTickAsync(stoppingToken); } } private async Task RefreshDueSourcesAsync(CancellationToken stoppingToken) { try { using IServiceScope scope = scopeFactory.CreateScope(); CalendarImportSyncService syncService = scope.ServiceProvider.GetRequiredService(); await syncService.RefreshDueSourcesAsync(stoppingToken); } catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { } catch (Exception ex) { logger.LogError(ex, "Calendar import background sync failed."); } } }