Skip to content

Commit

Permalink
fix(tasks): separate timeout for each task scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
SonicGD committed Dec 5, 2024
1 parent 53c7692 commit c3d5e80
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Sitko.Core.Tasks/Scheduling/TaskSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -55,14 +56,15 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}

var tasksManager = scope.ServiceProvider.GetRequiredService<TasksManager>();
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);
Expand Down

0 comments on commit c3d5e80

Please sign in to comment.