From bc94d31a8d2e19a6caee1692c92b1991cb48340d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 1 Dec 2023 19:50:18 +0100 Subject: [PATCH] Added logs cleanup --- bot/src/bot/extension/clean_logs_extension.py | 22 +++++++++++++++++++ bot/src/bot/main.py | 2 ++ 2 files changed, 24 insertions(+) create mode 100644 bot/src/bot/extension/clean_logs_extension.py diff --git a/bot/src/bot/extension/clean_logs_extension.py b/bot/src/bot/extension/clean_logs_extension.py new file mode 100644 index 00000000..4493f0fe --- /dev/null +++ b/bot/src/bot/extension/clean_logs_extension.py @@ -0,0 +1,22 @@ +import os +import shutil +from datetime import datetime + +from cpl_core.application.application_extension_abc import ApplicationExtensionABC +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_query.extension import List + + +class CleanLogsExtension(ApplicationExtensionABC): + def __init__(self): + pass + + async def run(self, config: ConfigurationABC, services: ServiceProviderABC): + ( + List(str, os.listdir("logs/")) + .where(lambda x: os.path.isdir(f"logs/{x}")) + .order_by() + .where(lambda x: (datetime.now() - datetime.strptime(x, "%Y-%m-%d")).days >= 7) + .for_each(lambda x: shutil.rmtree(f"logs/{x}")) + ) diff --git a/bot/src/bot/main.py b/bot/src/bot/main.py index 1efe7d63..bef4964a 100644 --- a/bot/src/bot/main.py +++ b/bot/src/bot/main.py @@ -6,6 +6,7 @@ from cpl_core.application import ApplicationBuilder from cpl_core.console import Console from bot.application import Application +from bot.extension.clean_logs_extension import CleanLogsExtension from bot.extension.init_bot_extension import InitBotExtension from bot.startup import Startup from bot.startup_discord_extension import StartupDiscordExtension @@ -31,6 +32,7 @@ class Program: .use_extension(StartupDiscordExtension) .use_extension(StartupModuleExtension) .use_extension(StartupMigrationExtension) + .use_extension(CleanLogsExtension) .use_extension(DatabaseExtension) .use_extension(ConfigExtension) .use_extension(InitBotExtension)