sh_discord_bot/kdb-bot/src/bot/application.py

83 lines
3.4 KiB
Python
Raw Normal View History

2022-10-02 13:13:23 +02:00
from cpl_core.configuration import ConfigurationABC
2022-07-14 16:32:46 +02:00
from cpl_core.console import Console
from cpl_core.dependency_injection import ServiceProviderABC
2022-07-16 19:47:04 +02:00
from cpl_core.logging import LoggerABC
from cpl_discord.application import DiscordBotApplicationABC
from cpl_discord.configuration import DiscordBotSettings
from cpl_discord.service import DiscordBotServiceABC, DiscordBotService
from cpl_translation import TranslatePipe, TranslationServiceABC, TranslationSettings
2022-07-14 16:32:46 +02:00
2022-10-13 19:57:56 +02:00
from bot_api.api_thread import ApiThread
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.service.data_integrity_service import DataIntegrityService
2022-10-13 19:57:56 +02:00
2022-07-14 16:32:46 +02:00
2022-07-16 19:47:04 +02:00
class Application(DiscordBotApplicationABC):
2022-07-14 16:32:46 +02:00
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
2022-07-16 19:47:04 +02:00
DiscordBotApplicationABC.__init__(self, config, services)
2022-10-02 12:14:44 +02:00
self._services = services
2022-10-13 19:57:56 +02:00
self._config = config
2022-10-02 12:14:44 +02:00
2022-07-16 19:47:04 +02:00
# cpl-core
self._logger: LoggerABC = services.get_service(LoggerABC)
self._data_integrity: DataIntegrityService = services.get_service(DataIntegrityService)
2022-07-16 19:47:04 +02:00
# cpl-discord
self._bot: DiscordBotServiceABC = services.get_service(DiscordBotServiceABC)
2023-01-13 23:54:06 +01:00
self._bot_settings: DiscordBotSettings = config.get_configuration(DiscordBotSettings)
2022-07-16 19:47:04 +02:00
# cpl-translation
2023-01-13 23:54:06 +01:00
self._translation: TranslationServiceABC = services.get_service(TranslationServiceABC)
2022-10-03 12:42:42 +02:00
self._t: TranslatePipe = services.get_service(TranslatePipe)
2022-07-14 16:32:46 +02:00
2023-01-13 23:54:06 +01:00
self._feature_flags: FeatureFlagsSettings = config.get_configuration(FeatureFlagsSettings)
2022-10-13 19:57:56 +02:00
# api
if self._feature_flags.get_flag(FeatureFlagsEnum.api_module):
self._api: ApiThread = services.get_service(ApiThread)
2022-10-02 13:25:29 +02:00
self._is_stopping = False
2022-07-14 16:32:46 +02:00
async def configure(self):
2023-01-13 23:54:06 +01:00
self._translation.load_by_settings(self._configuration.get_configuration(TranslationSettings))
2022-07-14 16:32:46 +02:00
async def main(self):
2022-07-16 19:47:04 +02:00
try:
2023-01-13 23:35:42 +01:00
self._logger.debug(__name__, f"Starting...")
2022-10-13 19:57:56 +02:00
2023-01-13 23:35:42 +01:00
if (
self._feature_flags.get_flag(FeatureFlagsEnum.api_module)
and self._feature_flags.get_flag(FeatureFlagsEnum.api_only)
and self._environment.environment_name == "development"
):
2022-10-16 21:36:08 +02:00
self._api.start()
2022-10-14 07:16:12 +02:00
self._api.join()
2022-10-13 19:57:56 +02:00
return
2023-03-30 14:37:02 +02:00
self._logger.info(__name__, f"Try to start {DiscordBotService.__name__}")
2022-07-16 19:47:04 +02:00
await self._bot.start_async()
2022-10-02 12:14:44 +02:00
await self._bot.stop_async()
2022-07-16 19:47:04 +02:00
except Exception as e:
2023-01-13 23:35:42 +01:00
self._logger.error(__name__, "Start failed", e)
2022-07-16 19:47:04 +02:00
async def stop_async(self):
2022-10-02 13:25:29 +02:00
if self._is_stopping:
return
self._is_stopping = True
2022-07-16 19:47:04 +02:00
try:
2023-03-30 14:37:02 +02:00
self._logger.info(__name__, f"Try to stop {DiscordBotService.__name__}")
if self._feature_flags.get_flag(FeatureFlagsEnum.api_module):
self._api.stop()
self._data_integrity.check_data_integrity(is_for_shutdown=True)
2022-07-16 19:47:04 +02:00
await self._bot.close()
2023-03-30 14:37:02 +02:00
self._logger.info(__name__, f"Stopped {DiscordBotService.__name__}")
2022-07-16 19:47:04 +02:00
except Exception as e:
2023-01-13 23:35:42 +01:00
self._logger.error(__name__, "stop failed", e)
2022-07-16 19:47:04 +02:00
Console.write_line()
2022-07-19 11:58:33 +02:00
def is_restart(self):
2023-01-13 23:54:06 +01:00
return True if self._configuration.get_configuration("IS_RESTART") == "true" else False #