Move background delay handling; improve background loop

This commit is contained in:
Noi 2020-04-03 20:26:18 -07:00
parent ddcde10e09
commit 670309715b
2 changed files with 38 additions and 22 deletions

View file

@ -11,7 +11,12 @@ namespace BirthdayBot
/// </summary> /// </summary>
class BackgroundServiceRunner class BackgroundServiceRunner
{ {
const int Interval = 8 * 60; // Tick interval in seconds. Adjust as needed. // Amount of idle time between each round of task execution, in seconds.
const int Interval = 8 * 60;
// Amount of time between start and first round of processing, in seconds.
const int StartDelay = 60;
const string LogName = nameof(BackgroundServiceRunner);
private List<BackgroundService> _workers; private List<BackgroundService> _workers;
private readonly CancellationTokenSource _workerCancel; private readonly CancellationTokenSource _workerCancel;
@ -48,31 +53,47 @@ namespace BirthdayBot
/// </summary> /// </summary>
private async Task WorkerLoop() private async Task WorkerLoop()
{ {
#if !DEBUG
// Start an initial delay before tasks begin running
Program.Log(LogName, $"Delaying first background execution by {StartDelay} seconds.");
try { await Task.Delay(StartDelay * 1000, _workerCancel.Token); }
catch (TaskCanceledException) { return; }
#else
Program.Log(LogName, "Debug build - skipping initial processing delay.");
#endif
while (!_workerCancel.IsCancellationRequested) while (!_workerCancel.IsCancellationRequested)
{ {
// Initiate background tasks
var tasks = new List<Task>();
foreach (var service in _workers) tasks.Add(service.OnTick());
var alltasks = Task.WhenAll(tasks);
// Await and check result
// Cancellation token not checked at this point...
try try
{ {
// Delay a bit before we start (or continue) work. await alltasks;
await Task.Delay(Interval * 1000, _workerCancel.Token);
// Execute background tasks.
var tasks = new List<Task>();
foreach (var service in _workers)
{
tasks.Add(service.OnTick());
}
await Task.WhenAll(tasks);
}
catch (TaskCanceledException)
{
return;
} }
catch (Exception ex) catch (Exception ex)
{ {
Program.Log("Background task", "Unhandled exception during background task execution:"); var exs = alltasks.Exception;
Program.Log("Background task", ex.ToString()); if (exs != null)
{
Program.Log(LogName, $"{exs.InnerExceptions.Count} exception(s) during background task execution:");
foreach (var iex in exs.InnerExceptions)
{
Program.Log(LogName, iex.Message);
} }
} }
else
{
Program.Log(LogName, ex.ToString());
}
}
try { await Task.Delay(Interval * 1000, _workerCancel.Token); }
catch (TaskCanceledException) { return; }
}
} }
} }
} }

View file

@ -60,11 +60,6 @@ namespace BirthdayBot
await DiscordClient.LoginAsync(TokenType.Bot, Config.BotToken); await DiscordClient.LoginAsync(TokenType.Bot, Config.BotToken);
await DiscordClient.StartAsync(); await DiscordClient.StartAsync();
#if !DEBUG
Program.Log("Background processing", "Delaying start");
await Task.Delay(90000); // TODO don't keep doing this
Program.Log("Background processing", "Delay complete");
#endif
_worker.Start(); _worker.Start();
await Task.Delay(-1); await Task.Delay(-1);