Fixed config loading #1.1.0.rc1

This commit is contained in:
Sven Heidemann 2023-08-16 08:30:12 +02:00
parent 1f47636e8d
commit 456d939b47
6 changed files with 26 additions and 10 deletions

View File

@ -1,29 +1,45 @@
from cpl_core.configuration import ConfigurationABC
from cpl_core.dependency_injection import ServiceProviderABC
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_data.abc.server_config_repository_abc import ServerConfigRepositoryABC
from bot_data.abc.technician_config_repository_abc import TechnicianConfigRepositoryABC
from bot_data.model.server import Server
from bot_data.model.technician_config import TechnicianConfig
from bot_data.service.server_config_seeder import ServerConfigSeeder
from bot_data.service.technician_config_seeder import TechnicianConfigSeeder
class ConfigService:
def __init__(
self,
config: ConfigurationABC,
services: ServiceProviderABC,
technician_config_repo: TechnicianConfigRepositoryABC,
server_config_repo: ServerConfigRepositoryABC,
tech_seeder: TechnicianConfigSeeder,
server_seeder: ServerConfigSeeder,
):
self._config = config
self._services = services
self._technician_config_repo = technician_config_repo
self._server_config_repo = server_config_repo
def reload_technician_config(self):
self._tech_seeder = tech_seeder
self._server_seeder = server_seeder
async def reload_technician_config(self):
if not self._technician_config_repo.does_technician_config_exists():
await self._tech_seeder.seed()
technician_config = self._technician_config_repo.get_technician_config()
self._config.add_configuration(TechnicianConfig, technician_config)
self._config.add_configuration(FeatureFlagsSettings, FeatureFlagsSettings(**technician_config.feature_flags))
def reload_server_config(self, server: Server):
async def reload_server_config(self, server: Server):
if not self._server_config_repo.does_server_config_exists(server.id):
await self._server_seeder.seed()
server_config = self._server_config_repo.get_server_config_by_server(server.id)
self._config.add_configuration(
f"{type(server_config).__name__}_{server_config.server.discord_id}", server_config

View File

@ -74,14 +74,14 @@ class ServerConfigRepositoryService(ServerConfigRepositoryABC):
)
def does_server_config_exists(self, server_id: int) -> bool:
self._logger.trace(__name__, f"Send SQL command: {ServerConfig.get_select_by_id_string(server_id)}")
result = self._context.select(ServerConfig.get_select_by_id_string(server_id))
self._logger.trace(__name__, f"Send SQL command: {ServerConfig.get_select_by_server_id_string(server_id)}")
result = self._context.select(ServerConfig.get_select_by_server_id_string(server_id))
return len(result) > 0
def get_server_config_by_server(self, server_id: int) -> ServerConfig:
self._logger.trace(__name__, f"Send SQL command: {ServerConfig.get_select_by_id_string(server_id)}")
result = self._context.select(ServerConfig.get_select_by_id_string(server_id))[0]
self._logger.trace(__name__, f"Send SQL command: {ServerConfig.get_select_by_server_id_string(server_id)}")
result = self._context.select(ServerConfig.get_select_by_server_id_string(server_id))[0]
return self._from_result(result)

View File

@ -174,5 +174,5 @@ class ServerConfigMutation(QueryABC):
self._server_configs.add_server_team_role_id_config(role_id)
self._config_service.reload_server_config(new_config.server)
self._bot.loop.create_task(self._config_service.reload_server_config(new_config.server))
self._permissions.on_ready()

View File

@ -113,5 +113,5 @@ class TechnicianConfigMutation(QueryABC):
self._technician_configs.add_technician_id_config(TechnicianIdConfig(technician_id))
self._config_service.reload_technician_config()
self._bot.loop.create_task(self._config_service.reload_technician_config())
self._permissions.on_ready()

View File

@ -19,4 +19,4 @@ class ConfigExtension(ApplicationExtensionABC):
logger: LoggerABC = services.get_service(LoggerABC)
logger.debug(__name__, "Config extension started")
config: ConfigService = services.get_service(ConfigService)
config.reload_technician_config()
await config.reload_technician_config()

View File

@ -26,4 +26,4 @@ class ConfigOnReadyEvent(OnReadyABC):
async def on_ready(self):
for guild in self._bot.guilds:
self._config_service.reload_server_config(self._servers.get_server_by_discord_id(guild.id))
await self._config_service.reload_server_config(self._servers.get_server_by_discord_id(guild.id))