diff --git a/BirthdayBot/BackgroundServiceRunner.vb b/BirthdayBot/BackgroundServiceRunner.vb index 42240ae..031a1c1 100644 --- a/BirthdayBot/BackgroundServiceRunner.vb +++ b/BirthdayBot/BackgroundServiceRunner.vb @@ -17,7 +17,8 @@ Class BackgroundServiceRunner Workers = New List(Of BackgroundService) From { {New GuildStatistics(instance)}, {New Heartbeat(instance)}, - {BirthdayUpdater} + {BirthdayUpdater}, + {New StaleDataCleaner(instance)} } End Sub diff --git a/BirthdayBot/BackgroundServices/StaleDataCleaner.vb b/BirthdayBot/BackgroundServices/StaleDataCleaner.vb new file mode 100644 index 0000000..f271959 --- /dev/null +++ b/BirthdayBot/BackgroundServices/StaleDataCleaner.vb @@ -0,0 +1,39 @@ +Imports Npgsql +''' +''' Automatically removes database information for guilds that have not been accessed in a long time. +''' +Class StaleDataCleaner + Inherits BackgroundService + + Public Sub New(instance As BirthdayBot) + MyBase.New(instance) + End Sub + + Public Overrides Async Function OnTick() As Task + Using db = Await BotInstance.Config.DatabaseSettings.OpenConnectionAsync() + ' Update only for all guilds the bot has cached + Using c = db.CreateCommand() + c.CommandText = $"update {GuildStateInformation.BackingTable} set last_seen = now() " + + "where guild_id = @Gid" + Dim updateGuild = c.Parameters.Add("@Gid", NpgsqlTypes.NpgsqlDbType.Bigint) + c.Prepare() + + Dim list As New List(Of ULong)(BotInstance.GuildCache.Keys) + For Each id In list + updateGuild.Value = CLng(id) + c.ExecuteNonQuery() + Next + End Using + + ' Delete all old values - expects referencing tables to have 'on delete cascade' + Using t = db.BeginTransaction() + Using c = db.CreateCommand() + ' Delete data for guilds not seen in 2 weeks + c.CommandText = $"delete from {GuildUserSettings.BackingTable} where (now() - interval '14 days') > last_seen" + Dim r = c.ExecuteNonQuery() + If r <> 0 Then Log($"Removed {r} stale guild(s).") + End Using + End Using + End Using + End Function +End Class diff --git a/BirthdayBot/BirthdayBot.vbproj b/BirthdayBot/BirthdayBot.vbproj index 4bd0e98..ba06d23 100644 --- a/BirthdayBot/BirthdayBot.vbproj +++ b/BirthdayBot/BirthdayBot.vbproj @@ -4,7 +4,7 @@ Exe BirthdayBot netcoreapp2.0 - 1.4.1 + 1.4.2 Noi Discord bot for birthday reminders. diff --git a/BirthdayBot/Data/GuildStateInformation.vb b/BirthdayBot/Data/GuildStateInformation.vb index 35d1991..298ed92 100644 --- a/BirthdayBot/Data/GuildStateInformation.vb +++ b/BirthdayBot/Data/GuildStateInformation.vb @@ -320,13 +320,14 @@ Class GuildStateInformation "moderator_role bigint null, " + "announce_message text null, " + "announce_message_pl text null, " + - "announce_ping boolean not null default FALSE" + + "announce_ping boolean not null default FALSE, " + + "last_seen timestamptz not null default NOW()" + ")" c.ExecuteNonQuery() End Using Using c = db.CreateCommand() c.CommandText = $"create table if not exists {BackingTableBans} (" + - $"guild_id bigint not null references {BackingTable}, " + + $"guild_id bigint not null references {BackingTable} ON DELETE CASCADE, " + "user_id bigint not null, " + "PRIMARY KEY (guild_id, user_id)" + ")" diff --git a/BirthdayBot/Data/GuildUserSettings.vb b/BirthdayBot/Data/GuildUserSettings.vb index 2b79ca3..173c666 100644 --- a/BirthdayBot/Data/GuildUserSettings.vb +++ b/BirthdayBot/Data/GuildUserSettings.vb @@ -125,7 +125,7 @@ Class GuildUserSettings Friend Shared Sub SetUpDatabaseTable(db As NpgsqlConnection) Using c = db.CreateCommand() c.CommandText = $"create table if not exists {BackingTable} (" + - $"guild_id bigint not null references {GuildStateInformation.BackingTable}, " + + $"guild_id bigint not null references {GuildStateInformation.BackingTable} ON DELETE CASCADE, " + "user_id bigint not null, " + "birth_month integer not null, " + "birth_day integer not null, " +