mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-24 09:24:12 +00:00
Noi
578f2545f2
-Remove webhook logging references --Including config line. Idea is abandoned for now. -Remove unneeded comments -Remove diagnostic messages --Corresponding problems were solved by moving to dedicated hardware -Update style on all affected files
75 lines
2.9 KiB
C#
75 lines
2.9 KiB
C#
namespace BirthdayBot.BackgroundServices;
|
|
|
|
/// <summary>
|
|
/// Handles the execution of periodic background tasks specific to each shard.
|
|
/// </summary>
|
|
class ShardBackgroundWorker : IDisposable {
|
|
/// <summary>
|
|
/// The interval, in seconds, in which background tasks are attempted to be run within a shard.
|
|
/// </summary>
|
|
public const int Interval = 40;
|
|
|
|
private readonly Task _workerTask;
|
|
private readonly CancellationTokenSource _workerCanceller;
|
|
private readonly List<BackgroundService> _workers;
|
|
private int _tickCount = -1;
|
|
|
|
private ShardInstance Instance { get; }
|
|
|
|
public DateTimeOffset LastBackgroundRun { get; private set; }
|
|
public string? CurrentExecutingService { get; private set; }
|
|
|
|
public ShardBackgroundWorker(ShardInstance instance) {
|
|
Instance = instance;
|
|
_workerCanceller = new CancellationTokenSource();
|
|
|
|
_workers = new List<BackgroundService>()
|
|
{
|
|
{new AutoUserDownload(instance)},
|
|
{new BirthdayRoleUpdate(instance)},
|
|
{new DataRetention(instance)},
|
|
{new ExternalStatisticsReporting(instance)}
|
|
};
|
|
|
|
_workerTask = Task.Factory.StartNew(WorkerLoop, _workerCanceller.Token);
|
|
}
|
|
|
|
public void Dispose() {
|
|
_workerCanceller.Cancel();
|
|
_workerTask.Wait(5000);
|
|
if (!_workerTask.IsCompleted)
|
|
Instance.Log("Dispose", "Warning: Background worker has not yet stopped. Forcing its disposal.");
|
|
_workerTask.Dispose();
|
|
_workerCanceller.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// *The* background task for the shard.
|
|
/// Executes service tasks and handles errors.
|
|
/// </summary>
|
|
private async Task WorkerLoop() {
|
|
LastBackgroundRun = DateTimeOffset.UtcNow;
|
|
try {
|
|
while (!_workerCanceller.IsCancellationRequested) {
|
|
await Task.Delay(Interval * 1000, _workerCanceller.Token).ConfigureAwait(false);
|
|
|
|
// Skip this round of task execution if the client is not connected
|
|
if (Instance.DiscordClient.ConnectionState != Discord.ConnectionState.Connected) continue;
|
|
|
|
// Execute tasks sequentially
|
|
foreach (var service in _workers) {
|
|
CurrentExecutingService = service.GetType().Name;
|
|
try {
|
|
if (_workerCanceller.IsCancellationRequested) break;
|
|
_tickCount++;
|
|
await service.OnTick(_tickCount, _workerCanceller.Token).ConfigureAwait(false);
|
|
} catch (Exception ex) when (ex is not TaskCanceledException) {
|
|
Instance.Log(nameof(WorkerLoop), $"{CurrentExecutingService} encountered an exception:\n" + ex.ToString());
|
|
}
|
|
}
|
|
CurrentExecutingService = null;
|
|
LastBackgroundRun = DateTimeOffset.UtcNow;
|
|
}
|
|
} catch (TaskCanceledException) { }
|
|
}
|
|
}
|