2020-10-10 06:48:43 +00:00
|
|
|
|
using BirthdayBot.Data;
|
2022-11-22 05:20:48 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-10-10 06:48:43 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
2021-10-15 01:55:04 +00:00
|
|
|
|
namespace BirthdayBot.BackgroundServices;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Automatically removes database information for guilds that have not been accessed in a long time.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class DataRetention : BackgroundService {
|
2021-11-22 19:49:55 +00:00
|
|
|
|
const int ProcessInterval = 5400 / ShardBackgroundWorker.Interval; // Process about once per hour and a half
|
2022-08-10 00:04:24 +00:00
|
|
|
|
|
2021-11-22 19:49:55 +00:00
|
|
|
|
// Amount of days without updates before data is considered stale and up for deletion.
|
|
|
|
|
const int StaleGuildThreshold = 180;
|
|
|
|
|
const int StaleUserThreashold = 360;
|
2020-10-10 06:48:43 +00:00
|
|
|
|
|
2021-10-15 01:55:04 +00:00
|
|
|
|
public DataRetention(ShardInstance instance) : base(instance) { }
|
2020-10-10 06:48:43 +00:00
|
|
|
|
|
2021-10-15 01:55:04 +00:00
|
|
|
|
public override async Task OnTick(int tickCount, CancellationToken token) {
|
|
|
|
|
// On each tick, run only a set group of guilds, each group still processed every ProcessInterval ticks.
|
2021-11-22 19:49:55 +00:00
|
|
|
|
if ((tickCount + ShardInstance.ShardId) % ProcessInterval != 0) return;
|
2021-10-15 01:55:04 +00:00
|
|
|
|
|
2022-08-10 00:04:24 +00:00
|
|
|
|
try {
|
|
|
|
|
await DbConcurrentOperationsLock.WaitAsync(token);
|
|
|
|
|
await RemoveStaleEntriesAsync();
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
DbConcurrentOperationsLock.Release();
|
|
|
|
|
} catch (ObjectDisposedException) { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task RemoveStaleEntriesAsync() {
|
2022-03-21 19:11:30 +00:00
|
|
|
|
using var db = new BotDatabaseContext();
|
|
|
|
|
var now = DateTimeOffset.UtcNow;
|
2020-10-10 06:48:43 +00:00
|
|
|
|
|
2022-11-22 05:20:48 +00:00
|
|
|
|
// Update guilds
|
2022-11-23 07:19:37 +00:00
|
|
|
|
var localGuilds = ShardInstance.DiscordClient.Guilds.Select(g => g.Id).ToList();
|
2022-11-22 05:20:48 +00:00
|
|
|
|
var updatedGuilds = await db.GuildConfigurations
|
|
|
|
|
.Where(g => localGuilds.Contains(g.GuildId))
|
|
|
|
|
.ExecuteUpdateAsync(upd => upd.SetProperty(p => p.LastSeen, now));
|
2020-10-10 06:48:43 +00:00
|
|
|
|
|
2022-11-22 05:20:48 +00:00
|
|
|
|
// Update guild users
|
|
|
|
|
var updatedUsers = 0;
|
|
|
|
|
foreach (var guild in ShardInstance.DiscordClient.Guilds) {
|
2022-11-23 07:19:37 +00:00
|
|
|
|
var localUsers = guild.Users.Select(u => u.Id).ToList();
|
2022-11-22 05:20:48 +00:00
|
|
|
|
updatedUsers += await db.UserEntries
|
2022-11-23 07:19:37 +00:00
|
|
|
|
.Where(gu => gu.GuildId == guild.Id)
|
2022-11-22 05:20:48 +00:00
|
|
|
|
.Where(gu => localUsers.Contains(gu.UserId))
|
|
|
|
|
.ExecuteUpdateAsync(upd => upd.SetProperty(p => p.LastSeen, now));
|
2022-03-21 19:11:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// And let go of old data
|
2022-11-22 05:20:48 +00:00
|
|
|
|
var staleGuildCount = await db.GuildConfigurations
|
|
|
|
|
.Where(g => now - TimeSpan.FromDays(StaleGuildThreshold) > g.LastSeen)
|
|
|
|
|
.ExecuteDeleteAsync();
|
|
|
|
|
var staleUserCount = await db.UserEntries
|
|
|
|
|
.Where(gu => now - TimeSpan.FromDays(StaleUserThreashold) > gu.LastSeen)
|
|
|
|
|
.ExecuteDeleteAsync();
|
2022-03-21 19:11:30 +00:00
|
|
|
|
|
2022-11-22 05:20:48 +00:00
|
|
|
|
// Build report
|
2022-03-21 19:11:30 +00:00
|
|
|
|
var resultText = new StringBuilder();
|
|
|
|
|
resultText.Append($"Updated {updatedGuilds} guilds, {updatedUsers} users.");
|
|
|
|
|
if (staleGuildCount != 0 || staleUserCount != 0) {
|
|
|
|
|
resultText.Append(" Discarded ");
|
|
|
|
|
if (staleGuildCount != 0) {
|
|
|
|
|
resultText.Append($"{staleGuildCount} guilds");
|
|
|
|
|
if (staleUserCount != 0) resultText.Append(", ");
|
2021-10-15 01:55:04 +00:00
|
|
|
|
}
|
2022-03-21 19:11:30 +00:00
|
|
|
|
if (staleUserCount != 0) {
|
|
|
|
|
resultText.Append($"{staleUserCount} users");
|
2020-10-10 06:48:43 +00:00
|
|
|
|
}
|
2022-03-21 19:11:30 +00:00
|
|
|
|
resultText.Append('.');
|
2020-10-10 06:48:43 +00:00
|
|
|
|
}
|
2022-03-21 19:11:30 +00:00
|
|
|
|
Log(resultText.ToString());
|
2020-10-10 06:48:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|