35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
namespace Socialize.Api.Modules.CalendarIntegrations.Services;
|
|
|
|
public sealed class CalendarImportBackgroundService(
|
|
IServiceScopeFactory scopeFactory,
|
|
ILogger<CalendarImportBackgroundService> 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<CalendarImportSyncService>();
|
|
await syncService.RefreshDueSourcesAsync(stoppingToken);
|
|
}
|
|
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
|
|
{
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Calendar import background sync failed.");
|
|
}
|
|
}
|
|
}
|