diff --git a/ProjectLighthouse/Services/RepeatingTaskService.cs b/ProjectLighthouse/Services/RepeatingTaskService.cs index 715c7feb..2395059e 100644 --- a/ProjectLighthouse/Services/RepeatingTaskService.cs +++ b/ProjectLighthouse/Services/RepeatingTaskService.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Database; +using LBPUnion.ProjectLighthouse.Extensions; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types.Logging; using LBPUnion.ProjectLighthouse.Types.Maintenance; @@ -47,35 +48,40 @@ public class RepeatingTaskService : BackgroundService await Task.Yield(); while (!stoppingToken.IsCancellationRequested) { - if (!this.TryGetNextTask(out IRepeatingTask? task) || task == null) + try { - // If we fail to fetch the next task then something has gone wrong and the service should halt - Logger.Debug("Failed to fetch next smallest task", LogArea.Maintenance); - return; - } - - TimeSpan timeElapsedSinceRun = DateTime.UtcNow.Subtract(task.LastRan); - - // If the task's repeat interval hasn't elapsed - if (timeElapsedSinceRun < task.RepeatInterval) - { - TimeSpan timeToWait = task.RepeatInterval.Subtract(timeElapsedSinceRun); - Logger.Debug($"Waiting {timeToWait} for {task.Name}", LogArea.Maintenance); - try + if (!this.TryGetNextTask(out IRepeatingTask? task) || task == null) { + // If we fail to fetch the next task then something has gone wrong and the service should halt + Logger.Debug("Failed to fetch next smallest task", LogArea.Maintenance); + return; + } + + TimeSpan timeElapsedSinceRun = DateTime.UtcNow.Subtract(task.LastRan); + + // If the task's repeat interval hasn't elapsed + if (timeElapsedSinceRun < task.RepeatInterval) + { + TimeSpan timeToWait = task.RepeatInterval.Subtract(timeElapsedSinceRun); + Logger.Debug($"Waiting {timeToWait} for {task.Name}", LogArea.Maintenance); await Task.Delay(timeToWait, stoppingToken); } - catch (TaskCanceledException) - { - break; - } - } - using IServiceScope scope = this.provider.CreateScope(); - DatabaseContext database = scope.ServiceProvider.GetRequiredService(); - await task.Run(database); - Logger.Debug($"Successfully ran task \"{task.Name}\"", LogArea.Maintenance); - task.LastRan = DateTime.UtcNow; + using IServiceScope scope = this.provider.CreateScope(); + DatabaseContext database = scope.ServiceProvider.GetRequiredService(); + // Set LastRan before running so if an exception occurs, the task doesn't immediately try to run again + task.LastRan = DateTime.UtcNow; + await task.Run(database); + Logger.Debug($"Successfully ran task \"{task.Name}\"", LogArea.Maintenance); + } + catch (TaskCanceledException) + { + break; + } + catch (Exception e) + { + Logger.Error($"Error while running repeating task: {e.ToDetailedException()}", LogArea.Maintenance); + } } } } \ No newline at end of file