mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-10 05:48:39 +00:00
Handle exceptions in repeating tasks
This commit is contained in:
parent
425245fd07
commit
8b1121a4f8
1 changed files with 30 additions and 24 deletions
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using LBPUnion.ProjectLighthouse.Database;
|
using LBPUnion.ProjectLighthouse.Database;
|
||||||
|
using LBPUnion.ProjectLighthouse.Extensions;
|
||||||
using LBPUnion.ProjectLighthouse.Logging;
|
using LBPUnion.ProjectLighthouse.Logging;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Logging;
|
using LBPUnion.ProjectLighthouse.Types.Logging;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Maintenance;
|
using LBPUnion.ProjectLighthouse.Types.Maintenance;
|
||||||
|
@ -46,6 +47,8 @@ public class RepeatingTaskService : BackgroundService
|
||||||
{
|
{
|
||||||
await Task.Yield();
|
await Task.Yield();
|
||||||
while (!stoppingToken.IsCancellationRequested)
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (!this.TryGetNextTask(out IRepeatingTask? task) || task == null)
|
if (!this.TryGetNextTask(out IRepeatingTask? task) || task == null)
|
||||||
{
|
{
|
||||||
|
@ -61,21 +64,24 @@ public class RepeatingTaskService : BackgroundService
|
||||||
{
|
{
|
||||||
TimeSpan timeToWait = task.RepeatInterval.Subtract(timeElapsedSinceRun);
|
TimeSpan timeToWait = task.RepeatInterval.Subtract(timeElapsedSinceRun);
|
||||||
Logger.Debug($"Waiting {timeToWait} for {task.Name}", LogArea.Maintenance);
|
Logger.Debug($"Waiting {timeToWait} for {task.Name}", LogArea.Maintenance);
|
||||||
try
|
|
||||||
{
|
|
||||||
await Task.Delay(timeToWait, stoppingToken);
|
await Task.Delay(timeToWait, stoppingToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using IServiceScope scope = this.provider.CreateScope();
|
||||||
|
DatabaseContext database = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||||
|
// 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)
|
catch (TaskCanceledException)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger.Error($"Error while running repeating task: {e.ToDetailedException()}", LogArea.Maintenance);
|
||||||
}
|
}
|
||||||
|
|
||||||
using IServiceScope scope = this.provider.CreateScope();
|
|
||||||
DatabaseContext database = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
|
||||||
await task.Run(database);
|
|
||||||
Logger.Debug($"Successfully ran task \"{task.Name}\"", LogArea.Maintenance);
|
|
||||||
task.LastRan = DateTime.UtcNow;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue