2021-12-06 01:20:05 +00:00
|
|
|
|
namespace BirthdayBot.BackgroundServices;
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the execution of periodic background tasks specific to each shard.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class ShardBackgroundWorker : IDisposable {
|
2020-10-05 04:40:38 +00:00
|
|
|
|
/// <summary>
|
2021-12-06 01:20:05 +00:00
|
|
|
|
/// The interval, in seconds, in which background tasks are attempted to be run within a shard.
|
2020-10-05 04:40:38 +00:00
|
|
|
|
/// </summary>
|
2021-12-06 01:20:05 +00:00
|
|
|
|
public const int Interval = 40;
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
private readonly Task _workerTask;
|
|
|
|
|
private readonly CancellationTokenSource _workerCanceller;
|
|
|
|
|
private readonly List<BackgroundService> _workers;
|
|
|
|
|
private int _tickCount = -1;
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
private ShardInstance Instance { get; }
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
public DateTimeOffset LastBackgroundRun { get; private set; }
|
|
|
|
|
public string? CurrentExecutingService { get; private set; }
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
public ShardBackgroundWorker(ShardInstance instance) {
|
|
|
|
|
Instance = instance;
|
|
|
|
|
_workerCanceller = new CancellationTokenSource();
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
_workers = new List<BackgroundService>()
|
|
|
|
|
{
|
|
|
|
|
{new AutoUserDownload(instance)},
|
|
|
|
|
{new BirthdayRoleUpdate(instance)},
|
2021-05-30 19:00:49 +00:00
|
|
|
|
{new DataRetention(instance)},
|
|
|
|
|
{new ExternalStatisticsReporting(instance)}
|
2020-10-05 04:40:38 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
_workerTask = Task.Factory.StartNew(WorkerLoop, _workerCanceller.Token);
|
|
|
|
|
}
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
/// <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);
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
// Skip this round of task execution if the client is not connected
|
|
|
|
|
if (Instance.DiscordClient.ConnectionState != Discord.ConnectionState.Connected) continue;
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
2021-12-06 01:20:05 +00:00
|
|
|
|
// 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());
|
2020-10-05 04:40:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-06 01:20:05 +00:00
|
|
|
|
CurrentExecutingService = null;
|
|
|
|
|
LastBackgroundRun = DateTimeOffset.UtcNow;
|
2020-10-05 04:40:38 +00:00
|
|
|
|
}
|
2021-12-06 01:20:05 +00:00
|
|
|
|
} catch (TaskCanceledException) { }
|
2020-10-05 04:40:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|