From 890cb6f6b5487b2d0786365131a957a3c10b62c7 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 2 Oct 2022 02:54:08 +0200 Subject: [PATCH] Added feature-flags #48 --- src/bot/config/appsettings.development.json | 2 +- .../config/appsettings.edrafts-pc-ubuntu.json | 2 +- src/bot/config/appsettings.production.json | 2 +- src/bot/config/appsettings.staging.json | 2 +- src/bot/main.py | 14 ++++++---- src/bot/startup.py | 14 ---------- src/bot/startup_settings_extension.py | 28 +++++++++++++++++++ src/modules/boot_log/boot_log_extension.py | 5 ++++ src/modules/database/database_extension.py | 4 +++ 9 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 src/bot/startup_settings_extension.py diff --git a/src/bot/config/appsettings.development.json b/src/bot/config/appsettings.development.json index d75fdfea..44dbc166 100644 --- a/src/bot/config/appsettings.development.json +++ b/src/bot/config/appsettings.development.json @@ -17,7 +17,7 @@ "de" ] }, - "FeatureFlag": { + "FeatureFlags": { "AdminModule": true, "BaseModule": true, "BootLogModule": true, diff --git a/src/bot/config/appsettings.edrafts-pc-ubuntu.json b/src/bot/config/appsettings.edrafts-pc-ubuntu.json index 6523b2d9..20585cb5 100644 --- a/src/bot/config/appsettings.edrafts-pc-ubuntu.json +++ b/src/bot/config/appsettings.edrafts-pc-ubuntu.json @@ -19,7 +19,7 @@ "Token": "OTk4MTYwNDI3Njg5MTgxMjM3.GI7h67.BqD6Lu1Tz0MuG8iktYrcLnHi1pNozyMiWFGTKI", "Prefix": "!ke " }, - "FeatureFlag": { + "FeatureFlags": { "AdminModule": true, "BaseModule": true, "BootLogModule": true, diff --git a/src/bot/config/appsettings.production.json b/src/bot/config/appsettings.production.json index 1f2dd507..515c60af 100644 --- a/src/bot/config/appsettings.production.json +++ b/src/bot/config/appsettings.production.json @@ -28,7 +28,7 @@ "Buffered": "true", "AuthPlugin": "mysql_native_password" }, - "FeatureFlag": { + "FeatureFlags": { "AdminModule": true, "BaseModule": true, "BootLogModule": true, diff --git a/src/bot/config/appsettings.staging.json b/src/bot/config/appsettings.staging.json index 3ddd1c3f..605fe7fc 100644 --- a/src/bot/config/appsettings.staging.json +++ b/src/bot/config/appsettings.staging.json @@ -17,7 +17,7 @@ "de" ] }, - "FeatureFlag": { + "FeatureFlags": { "AdminModule": true, "BaseModule": true, "BootLogModule": true, diff --git a/src/bot/main.py b/src/bot/main.py index 0bfb5a82..29c81fc1 100644 --- a/src/bot/main.py +++ b/src/bot/main.py @@ -8,6 +8,7 @@ from bot.application import Application from bot.startup import Startup from bot.startup_discord_extension import StartupDiscordExtension from bot.startup_migration_extension import StartupMigrationExtension +from bot.startup_settings_extension import StartupSettingsExtension from modules.boot_log.boot_log_extension import BootLogExtension from modules.database.database_extension import DatabaseExtension @@ -18,12 +19,13 @@ class Program: self.app: Optional[Application] = None async def start(self): - app_builder = ApplicationBuilder(Application) - app_builder.use_extension(StartupDiscordExtension) - app_builder.use_extension(StartupMigrationExtension) - app_builder.use_extension(BootLogExtension) - app_builder.use_extension(DatabaseExtension) - app_builder.use_startup(Startup) + app_builder = ApplicationBuilder(Application) \ + .use_extension(StartupSettingsExtension) \ + .use_extension(StartupDiscordExtension) \ + .use_extension(StartupMigrationExtension) \ + .use_extension(BootLogExtension) \ + .use_extension(DatabaseExtension) \ + .use_startup(Startup) self.app: Application = await app_builder.build_async() await self.app.run_async() diff --git a/src/bot/startup.py b/src/bot/startup.py index 4f936340..689c9819 100644 --- a/src/bot/startup.py +++ b/src/bot/startup.py @@ -1,5 +1,3 @@ -import os -from datetime import datetime from typing import Optional from cpl_core.application import StartupABC @@ -37,22 +35,10 @@ class Startup(StartupABC): def __init__(self): StartupABC.__init__(self) - self._start_time = datetime.now() - self._config: Optional[ConfigurationABC] = None self._feature_flags: Optional[FeatureFlagsSettings] = None def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC: - environment.set_working_directory(os.path.dirname(os.path.realpath(__file__))) - configuration.add_environment_variables('KDB_') - configuration.add_environment_variables('DISCORD_') - - configuration.add_json_file(f'config/appsettings.json', optional=False) - configuration.add_json_file(f'config/appsettings.{environment.environment_name}.json', optional=True) - configuration.add_json_file(f'config/appsettings.{environment.host_name}.json', optional=True) - - configuration.add_configuration('Startup_StartTime', str(self._start_time)) - self._config = configuration self._feature_flags = configuration.get_configuration(FeatureFlagsSettings) return configuration diff --git a/src/bot/startup_settings_extension.py b/src/bot/startup_settings_extension.py new file mode 100644 index 00000000..8c27e69b --- /dev/null +++ b/src/bot/startup_settings_extension.py @@ -0,0 +1,28 @@ +import os +from datetime import datetime + +from cpl_core.application import StartupExtensionABC +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironmentABC + + +class StartupSettingsExtension(StartupExtensionABC): + + def __init__(self): + self._start_time = datetime.now() + + def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironmentABC): + # this shit has to be done here because we need settings in subsequent startup extensions + environment.set_working_directory(os.path.dirname(os.path.realpath(__file__))) + configuration.add_environment_variables('KDB_') + configuration.add_environment_variables('DISCORD_') + + configuration.add_json_file(f'config/appsettings.json', optional=False) + configuration.add_json_file(f'config/appsettings.{environment.environment_name}.json', optional=True) + configuration.add_json_file(f'config/appsettings.{environment.host_name}.json', optional=True) + + configuration.add_configuration('Startup_StartTime', str(self._start_time)) + + def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): + pass diff --git a/src/modules/boot_log/boot_log_extension.py b/src/modules/boot_log/boot_log_extension.py index 8bb36c7e..42db4b8e 100644 --- a/src/modules/boot_log/boot_log_extension.py +++ b/src/modules/boot_log/boot_log_extension.py @@ -5,6 +5,8 @@ from cpl_core.configuration import ConfigurationABC from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.logging import LoggerABC +from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings + class BootLogExtension(ApplicationExtensionABC): @@ -12,6 +14,9 @@ class BootLogExtension(ApplicationExtensionABC): pass async def run(self, config: ConfigurationABC, services: ServiceProviderABC): + feature_flags: FeatureFlagsSettings = config.get_configuration(FeatureFlagsSettings) + if not feature_flags.boot_log_module: + return logger: LoggerABC = services.get_service(LoggerABC) logger.debug(__name__, 'BootLog extension started') config.add_configuration('Bot_StartTime', str(datetime.now())) diff --git a/src/modules/database/database_extension.py b/src/modules/database/database_extension.py index edcca9b9..883752bb 100644 --- a/src/modules/database/database_extension.py +++ b/src/modules/database/database_extension.py @@ -5,6 +5,7 @@ from cpl_core.configuration import ConfigurationABC from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.logging import LoggerABC +from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings from bot_data.service.migration_service import MigrationService @@ -14,6 +15,9 @@ class DatabaseExtension(ApplicationExtensionABC): pass async def run(self, config: ConfigurationABC, services: ServiceProviderABC): + feature_flags: FeatureFlagsSettings = config.get_configuration(FeatureFlagsSettings) + if not feature_flags.database_module: + return logger: LoggerABC = services.get_service(LoggerABC) logger.debug(__name__, 'Database extension started') config.add_configuration('Database_StartTime', str(datetime.now()))