Skip to content

Commit

Permalink
feat: add logs to scheduling service
Browse files Browse the repository at this point in the history
  • Loading branch information
pogromistik committed Dec 12, 2023
1 parent fa4b903 commit 629a892
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Sitko.Core.Tasks/Scheduling/TaskSchedulingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ public TaskSchedulingService(IServiceScopeFactory serviceScopeFactory,

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogInformation("Init scheduling service {Type}", typeof(TTask));
while (!stoppingToken.IsCancellationRequested)
{
try
{
logger.LogInformation("Run scheduling task {Type}", typeof(TTask));
var now = DateTime.UtcNow;
var nextDate = taskOptions.Value.CronExpression.GetNextOccurrence(now);
if (nextDate != null)
Expand All @@ -43,6 +45,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
if (optionsMonitor.CurrentValue.IsAllTasksDisabled ||
optionsMonitor.CurrentValue.DisabledTasks.Contains(typeof(TTask).Name))
{
logger.LogInformation("Skip disabled task {Type}", typeof(TTask));
return;
}

Expand All @@ -52,6 +55,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
using (await scheduleLock.LockAsync(cts.Token))
{
var tasks = await scheduler.GetTasksAsync(stoppingToken);
logger.LogInformation("Found {Count} {Type} tasks", tasks.Length, typeof(TTask));
foreach (var task in tasks)
{
try
Expand All @@ -68,15 +72,18 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
}
}
logger.LogInformation("Scheduling task {Type} success", typeof(TTask));
}
catch (TaskCanceledException)
{
logger.LogInformation("Scheduling task {Type} is canceled", typeof(TTask));
// do nothing
}
catch (Exception ex)
{
logger.LogError("Error schedule tasks {Type}: {Error}", typeof(TTask), ex);
}
}
logger.LogInformation("Exit from scheduling task {Type}", typeof(TTask));
}
}

0 comments on commit 629a892

Please sign in to comment.