Fix StaleDataCleaner not working

This commit is contained in:
Noi 2020-06-03 00:17:54 -07:00
parent 99786a7b70
commit f839890b73
2 changed files with 11 additions and 15 deletions

View file

@ -11,22 +11,17 @@ namespace BirthdayBot
/// </summary> /// </summary>
class BackgroundServiceRunner class BackgroundServiceRunner
{ {
// Amount of idle time between each round of task execution, in seconds. #if !DEBUG
#if DEBUG
// Amount of idle time between each round of task execution, in seconds.
const int Interval = 10;
// Amount of time between start and first round of processing, in seconds.
const int StartDelay = 15;
#else
// Amount of idle time between each round of task execution, in seconds. // Amount of idle time between each round of task execution, in seconds.
const int Interval = 8 * 60; const int Interval = 8 * 60;
// Amount of time between start and first round of processing, in seconds. // Amount of time between start and first round of processing, in seconds.
const int StartDelay = 60; const int StartDelay = 60;
#else
const int Interval = 10;
const int StartDelay = 15;
#endif #endif
const string LogName = nameof(BackgroundServiceRunner); const string LogName = nameof(BackgroundServiceRunner);
private List<BackgroundService> _workers; private List<BackgroundService> _workers;
@ -43,7 +38,8 @@ namespace BirthdayBot
{ {
{new GuildStatistics(instance)}, {new GuildStatistics(instance)},
{new Heartbeat(instance)}, {new Heartbeat(instance)},
{BirthdayUpdater} {BirthdayUpdater},
{new StaleDataCleaner(instance)}
}; };
} }

View file

@ -44,7 +44,7 @@ namespace BirthdayBot.BackgroundServices
cUpdateGuildUser.CommandText = $"update {GuildUserSettings.BackingTable} set last_seen = now() " cUpdateGuildUser.CommandText = $"update {GuildUserSettings.BackingTable} set last_seen = now() "
+ "where guild_id = @Gid and user_id = @Uid"; + "where guild_id = @Gid and user_id = @Uid";
var pUpdateGU_g = cUpdateGuildUser.Parameters.Add("@Gid", NpgsqlDbType.Bigint); var pUpdateGU_g = cUpdateGuildUser.Parameters.Add("@Gid", NpgsqlDbType.Bigint);
var pUpdateGU_u = cUpdateGuild.Parameters.Add("@Uid", NpgsqlDbType.Bigint); var pUpdateGU_u = cUpdateGuildUser.Parameters.Add("@Uid", NpgsqlDbType.Bigint);
cUpdateGuildUser.Prepare(); cUpdateGuildUser.Prepare();
// Do actual updates // Do actual updates
@ -67,20 +67,20 @@ namespace BirthdayBot.BackgroundServices
// Delete all old values - expects referencing tables to have 'on delete cascade' // Delete all old values - expects referencing tables to have 'on delete cascade'
using (var t = db.BeginTransaction()) using (var t = db.BeginTransaction())
{ {
int staleGuilds, staleUsers;
using (var c = db.CreateCommand()) using (var c = db.CreateCommand())
{ {
// Delete data for guilds not seen in 4 weeks // Delete data for guilds not seen in 4 weeks
c.CommandText = $"delete from {GuildStateInformation.BackingTable} where (now() - interval '28 days') > last_seen"; c.CommandText = $"delete from {GuildStateInformation.BackingTable} where (now() - interval '28 days') > last_seen";
var r = c.ExecuteNonQuery(); staleGuilds = c.ExecuteNonQuery();
if (r != 0) Log($"Removed {r} stale guild(s).");
} }
using (var c = db.CreateCommand()) using (var c = db.CreateCommand())
{ {
// Delete data for users not seen in 8 weeks // Delete data for users not seen in 8 weeks
c.CommandText = $"delete from {GuildUserSettings.BackingTable} where (now() - interval '56 days') > last_seen"; c.CommandText = $"delete from {GuildUserSettings.BackingTable} where (now() - interval '56 days') > last_seen";
var r = c.ExecuteNonQuery(); staleUsers = c.ExecuteNonQuery();
if (r != 0) Log($"Removed {r} stale user(s).");
} }
Log($"Will remove {staleGuilds} guilds, {staleUsers} users.");
t.Commit(); t.Commit();
} }
} }