diff --git a/src/Sitko.Core.Tasks/Scheduling/TaskSchedulingService.cs b/src/Sitko.Core.Tasks/Scheduling/TaskSchedulingService.cs index f99d8319f..521b77c9c 100644 --- a/src/Sitko.Core.Tasks/Scheduling/TaskSchedulingService.cs +++ b/src/Sitko.Core.Tasks/Scheduling/TaskSchedulingService.cs @@ -38,7 +38,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) logger.LogInformation("Next scheduling time for task {Type}: {Date}", typeof(TTask), nextDate); if (nextDate != null) { - var secondsToWait = Math.Round((nextDate - now).Value.TotalSeconds, MidpointRounding.ToPositiveInfinity); + var secondsToWait = Math.Round((nextDate - now).Value.TotalSeconds, + MidpointRounding.ToPositiveInfinity); logger.LogInformation("Wait {Seconds} seconds before scheduling task {Type}", secondsToWait, typeof(TTask)); await Task.Delay(TimeSpan.FromSeconds(secondsToWait), stoppingToken); @@ -55,14 +56,15 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } var tasksManager = scope.ServiceProvider.GetRequiredService(); - var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1)); var tasks = await scheduler.GetTasksAsync(stoppingToken); logger.LogInformation("Found {Count} {Type} tasks", tasks.Length, typeof(TTask)); foreach (var task in tasks) { try { - var runResult = await tasksManager.RunAsync(task, cancellationToken: cts.Token); + var runResult = await tasksManager + .RunAsync(task, cancellationToken: stoppingToken) // cancels if application is stopping + .WaitAsync(TimeSpan.FromMinutes(1), stoppingToken); // don't hang too long if (!runResult.IsSuccess) { throw new InvalidOperationException(runResult.ErrorMessage);