Improve comments, remove redundant code

This commit is contained in:
Noi 2020-10-08 21:46:36 -07:00
parent 6efc436717
commit 6ee0ad939e
2 changed files with 9 additions and 10 deletions

View file

@ -20,13 +20,14 @@ namespace BirthdayBot.BackgroundServices
/// <summary> /// <summary>
/// Processes birthday updates for all available guilds synchronously /// Processes birthday updates for all available guilds synchronously
/// (to avoid database connection pool bottlenecks). /// (to avoid database connection pool bottlenecks and rate limiting).
/// </summary> /// </summary>
public override async Task OnTick(CancellationToken token) public override async Task OnTick(CancellationToken token)
{ {
var exs = new List<Exception>(); var exs = new List<Exception>();
foreach (var guild in ShardInstance.DiscordClient.Guilds) foreach (var guild in ShardInstance.DiscordClient.Guilds)
{ {
// Single guilds are fully processed and are not interrupted by task cancellation.
if (token.IsCancellationRequested) throw new TaskCanceledException(); if (token.IsCancellationRequested) throw new TaskCanceledException();
try try
{ {
@ -34,12 +35,10 @@ namespace BirthdayBot.BackgroundServices
} }
catch (Exception ex) catch (Exception ex)
{ {
if (ex is TaskCanceledException) throw;
// Catch all exceptions per-guild but continue processing, throw at end // Catch all exceptions per-guild but continue processing, throw at end
exs.Add(ex); exs.Add(ex);
} }
} }
//Log($"Completed processing {ShardInstance.DiscordClient.Guilds.Count} guilds.");
if (exs.Count != 0) throw new AggregateException(exs); if (exs.Count != 0) throw new AggregateException(exs);
// TODO metrics for role sets, unsets, announcements - and how to do that for singles too? // TODO metrics for role sets, unsets, announcements - and how to do that for singles too?
@ -69,7 +68,9 @@ namespace BirthdayBot.BackgroundServices
if (diag.RoleCheck != null) return diag; if (diag.RoleCheck != null) return diag;
// Determine who's currently having a birthday // Determine who's currently having a birthday
//await guild.DownloadUsersAsync(); // Note: This is where we'd call DownloadUsersAsync, but this method is capable of blocking indefinitely
// and making the task completely unresponsive. Must investigate further before calling it here and disabling
// AlwaysDownloadUsers in client settings.
var users = await GuildUserConfiguration.LoadAllAsync(guild.Id); var users = await GuildUserConfiguration.LoadAllAsync(guild.Id);
var tz = gc.TimeZone; var tz = gc.TimeZone;
var birthdays = GetGuildCurrentBirthdays(users, tz); var birthdays = GetGuildCurrentBirthdays(users, tz);

View file

@ -54,7 +54,8 @@ namespace BirthdayBot.BackgroundServices
} }
/// <summary> /// <summary>
/// *The* background task. Executes service tasks and handles errors. /// *The* background task for the shard.
/// Executes service tasks and handles errors.
/// </summary> /// </summary>
private async Task WorkerLoop() private async Task WorkerLoop()
{ {
@ -65,17 +66,14 @@ namespace BirthdayBot.BackgroundServices
{ {
await Task.Delay(Interval * 1000, _workerCanceller.Token); await Task.Delay(Interval * 1000, _workerCanceller.Token);
// Wait a while for a stable connection, the threshold for which is defined within ConnectionStatus. // ConnectionStatus will always run. Its result determines if remaining tasks also this time.
await ConnStatus.OnTick(_workerCanceller.Token); await ConnStatus.OnTick(_workerCanceller.Token);
if (!ConnStatus.Stable) continue; if (!ConnStatus.Stable) continue;
// Execute tasks sequentially // Execute tasks sequentially
foreach (var service in _workers) foreach (var service in _workers)
{ {
try try { await service.OnTick(_workerCanceller.Token); }
{
await service.OnTick(_workerCanceller.Token);
}
catch (Exception ex) catch (Exception ex)
{ {
var svcname = service.GetType().Name; var svcname = service.GetType().Name;