Skip to content

Commit

Permalink
added msg cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Sep 28, 2023
1 parent d193cff commit bd5b77c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/VahterBanBot/Bot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ let onUpdate
let banUserInDb =
message.ReplyToMessage.From
|> DbUser.newUser
|> DbUser.banUser message.From.Id (Option.ofObj message.Text)
|> DbUser.banUser message.From.Id (Option.ofObj message.ReplyToMessage.Text)
|> DB.upsertUser

let deletedUserMessagesTask = task {
Expand Down
48 changes: 48 additions & 0 deletions src/VahterBanBot/Cleanup.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module VahterBanBot.Cleanup

open System.Threading.Tasks
open Microsoft.Extensions.Logging
open Telegram.Bot
open Telegram.Bot.Types
open VahterBanBot.Types
open VahterBanBot.Utils
open System
open System.Threading
open Microsoft.Extensions.Hosting

type CleanupService(
logger: ILogger<CleanupService>,
telegramClient: ITelegramBotClient,
botConf: BotConfiguration
) =
let cleanupInterval =
getEnvOr "MESSAGES_CLEANUP_INTERVAL_SEC" "86400" // 1 day
|> int
|> TimeSpan.FromSeconds
let cleanupOldLimit =
getEnvOr "MESSAGES_CLEANUP_OLD_LIMIT_SEC" "259200" // 3 days
|> int
|> TimeSpan.FromSeconds
let mutable timer: Timer = null

let cleanup _ = task {
let! cleanupMsgs = DB.cleanupOldMessages cleanupOldLimit

let msg = $"Cleaned up {cleanupMsgs} messages"

let! _ = telegramClient.SendTextMessageAsync(
ChatId(botConf.LogsChannelId),
msg
)
logger.LogInformation msg
}

interface IHostedService with
member this.StartAsync(cancellationToken) =
timer <- new Timer(TimerCallback(cleanup >> ignore), null, TimeSpan.Zero, cleanupInterval)
Task.CompletedTask

member this.StopAsync(cancellationToken) =
match timer with
| null -> Task.CompletedTask
| timer -> timer.DisposeAsync().AsTask()
17 changes: 17 additions & 0 deletions src/VahterBanBot/DB.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module VahterBanBot.DB

open System
open System.Threading.Tasks
open Npgsql
open VahterBanBot.Types
Expand Down Expand Up @@ -122,3 +123,19 @@ let deleteUserMessages (userId: int64): Task<int> =
)
return messagesDeleted
}

let cleanupOldMessages (howOld: TimeSpan): Task<int> =
task {
use conn = new NpgsqlConnection(connString)

//language=postgresql
let sql = "DELETE FROM message WHERE created_at < @thatOld"

let! messagesDeleted =
conn.ExecuteAsync(
sql,
{| thatOld = DateTime.UtcNow.Subtract howOld |}
)

return messagesDeleted
}
3 changes: 2 additions & 1 deletion src/VahterBanBot/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ open Telegram.Bot
open Telegram.Bot.Types
open Giraffe
open Microsoft.Extensions.DependencyInjection
open VahterBanBot
open VahterBanBot.Cleanup
open VahterBanBot.Utils
open VahterBanBot.Bot
open VahterBanBot.Types
Expand Down Expand Up @@ -38,6 +38,7 @@ let builder = WebApplication.CreateBuilder()
%builder.Services
.AddSingleton(botConf)
.AddGiraffe()
.AddHostedService<CleanupService>()
.AddHttpClient("telegram_bot_client")
.AddTypedClient(fun httpClient sp ->
let options = TelegramBotClientOptions(botConf.BotToken)
Expand Down
1 change: 1 addition & 0 deletions src/VahterBanBot/VahterBanBot.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Compile Include="Types.fs" />
<Compile Include="DB.fs" />
<Compile Include="Bot.fs" />
<Compile Include="Cleanup.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

Expand Down

0 comments on commit bd5b77c

Please sign in to comment.