mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-22 05:54:36 +00:00
Remove unused guild data after a period of inactivity
This commit is contained in:
parent
80eb23c951
commit
863e043650
5 changed files with 46 additions and 5 deletions
|
@ -17,7 +17,8 @@ Class BackgroundServiceRunner
|
||||||
Workers = New List(Of BackgroundService) From {
|
Workers = New List(Of BackgroundService) From {
|
||||||
{New GuildStatistics(instance)},
|
{New GuildStatistics(instance)},
|
||||||
{New Heartbeat(instance)},
|
{New Heartbeat(instance)},
|
||||||
{BirthdayUpdater}
|
{BirthdayUpdater},
|
||||||
|
{New StaleDataCleaner(instance)}
|
||||||
}
|
}
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
|
|
39
BirthdayBot/BackgroundServices/StaleDataCleaner.vb
Normal file
39
BirthdayBot/BackgroundServices/StaleDataCleaner.vb
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
Imports Npgsql
|
||||||
|
''' <summary>
|
||||||
|
''' Automatically removes database information for guilds that have not been accessed in a long time.
|
||||||
|
''' </summary>
|
||||||
|
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
|
|
@ -4,7 +4,7 @@
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<RootNamespace>BirthdayBot</RootNamespace>
|
<RootNamespace>BirthdayBot</RootNamespace>
|
||||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||||
<Version>1.4.1</Version>
|
<Version>1.4.2</Version>
|
||||||
<Authors>Noi</Authors>
|
<Authors>Noi</Authors>
|
||||||
<Company />
|
<Company />
|
||||||
<Description>Discord bot for birthday reminders.</Description>
|
<Description>Discord bot for birthday reminders.</Description>
|
||||||
|
|
|
@ -320,13 +320,14 @@ Class GuildStateInformation
|
||||||
"moderator_role bigint null, " +
|
"moderator_role bigint null, " +
|
||||||
"announce_message text null, " +
|
"announce_message text null, " +
|
||||||
"announce_message_pl 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()
|
c.ExecuteNonQuery()
|
||||||
End Using
|
End Using
|
||||||
Using c = db.CreateCommand()
|
Using c = db.CreateCommand()
|
||||||
c.CommandText = $"create table if not exists {BackingTableBans} (" +
|
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, " +
|
"user_id bigint not null, " +
|
||||||
"PRIMARY KEY (guild_id, user_id)" +
|
"PRIMARY KEY (guild_id, user_id)" +
|
||||||
")"
|
")"
|
||||||
|
|
|
@ -125,7 +125,7 @@ Class GuildUserSettings
|
||||||
Friend Shared Sub SetUpDatabaseTable(db As NpgsqlConnection)
|
Friend Shared Sub SetUpDatabaseTable(db As NpgsqlConnection)
|
||||||
Using c = db.CreateCommand()
|
Using c = db.CreateCommand()
|
||||||
c.CommandText = $"create table if not exists {BackingTable} (" +
|
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, " +
|
"user_id bigint not null, " +
|
||||||
"birth_month integer not null, " +
|
"birth_month integer not null, " +
|
||||||
"birth_day integer not null, " +
|
"birth_day integer not null, " +
|
||||||
|
|
Loading…
Reference in a new issue