-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters