Added maintenance to db and gql #424

This commit is contained in:
Sven Heidemann 2023-11-06 18:02:04 +01:00
parent a7c833b9db
commit e1258151de
13 changed files with 89 additions and 10 deletions

View File

@ -11,6 +11,7 @@ from bot_api.api_thread import ApiThread
from bot_core.abc.task_abc import TaskABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.environment_variables import MAINTENANCE
from bot_core.service.data_integrity_service import DataIntegrityService
@ -58,7 +59,7 @@ class Application(DiscordBotApplicationABC):
return
self._logger.info(__name__, f"Try to start {DiscordBotService.__name__}")
if not self._config.get_configuration("MAINTENANCE"):
if not self._config.get_configuration(MAINTENANCE):
for task in self._tasks:
await self._bot.add_cog(task)

View File

@ -8,6 +8,7 @@ from cpl_core.dependency_injection import ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironmentABC
from bot_core.configuration.bot_logging_settings import BotLoggingSettings
from bot_core.environment_variables import MAINTENANCE
class StartupSettingsExtension(StartupExtensionABC):
@ -20,7 +21,7 @@ class StartupSettingsExtension(StartupExtensionABC):
configuration.add_environment_variables("SDB_")
configuration.add_environment_variables("DISCORD_")
configuration.add_configuration(
"MAINTENANCE", configuration.get_configuration("MAINTENANCE") in [True, "true", "True"]
MAINTENANCE, configuration.get_configuration(MAINTENANCE) in [True, "true", "True"]
)
configuration.add_configuration(
"MIGRATION_ONLY", configuration.get_configuration("MIGRATION_ONLY") in [True, "true", "True"]

View File

@ -6,6 +6,7 @@ from cpl_core.dependency_injection import ServiceProviderABC
from cpl_discord.service import DiscordBotServiceABC
from discord.ext import commands
from bot_core.environment_variables import MAINTENANCE
from bot_core.logging.task_logger import TaskLogger
@ -16,7 +17,7 @@ class TaskABC(commands.Cog):
@ServiceProviderABC.inject
def _is_maintenance(self, config: ConfigurationABC) -> bool:
return config.get_configuration("MAINTENANCE") is True
return config.get_configuration(MAINTENANCE) is True
@ServiceProviderABC.inject
async def _wait_until_ready(self, config: ConfigurationABC, logger: TaskLogger, bot: DiscordBotServiceABC):

View File

@ -7,6 +7,7 @@ from cpl_discord.service import DiscordBotServiceABC
from cpl_translation import TranslatePipe
from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.environment_variables import MAINTENANCE
class CoreExtensionOnReadyEvent(OnReadyABC):
@ -30,7 +31,7 @@ class CoreExtensionOnReadyEvent(OnReadyABC):
async def on_ready(self):
self._logger.debug(__name__, f"Module {type(self)} started")
if self._config.get_configuration("MAINTENANCE"):
if self._config.get_configuration(MAINTENANCE):
await self._client_utils.presence_game("common.presence.maintenance")
else:
await self._client_utils.presence_game("common.presence.running")

View File

@ -0,0 +1 @@
MAINTENANCE = "MAINTENANCE"

View File

@ -24,12 +24,11 @@ class EventChecks:
if not result:
def empty(*args, **kwargs):
pass
return
return empty
return await func(*args, **kwargs)
# return commands.check(check_if_bot_is_ready)
check_if_bot_is_ready.__name__ = func.__name__
sig = inspect.signature(func)
check_if_bot_is_ready.__signature__ = sig.replace(parameters=tuple(sig.parameters.values())[1:])

View File

@ -16,6 +16,7 @@ from bot_core.abc.client_utils_abc import ClientUtilsABC
from bot_core.abc.message_service_abc import MessageServiceABC
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings
from bot_core.environment_variables import MAINTENANCE
from bot_data.abc.client_repository_abc import ClientRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_joined_voice_channel_repository_abc import (
@ -87,7 +88,7 @@ class ClientUtilsService(ClientUtilsABC):
return client
async def check_if_bot_is_ready_yet(self) -> bool:
if self._config.get_configuration("MAINTENANCE"):
if self._config.get_configuration(MAINTENANCE):
self._logger.warn(
__name__,
f"Bot is in maintenance mode",

View File

@ -6,6 +6,7 @@ CREATE TABLE IF NOT EXISTS `CFG_TechnicianHistory`
`WaitForShutdown` BIGINT NOT NULL DEFAULT 8,
`CacheMaxMessages` BIGINT NOT NULL DEFAULT 1000000,
`MaxSteamOfferCount` BIGINT NOT NULL DEFAULT 250,
`Maintenance` BOOLEAN DEFAULT FALSE,
`FeatureFlags` JSON NULL DEFAULT ('{}'),
`Deleted` BOOL DEFAULT FALSE,
`DateFrom` DATETIME(6) NOT NULL,
@ -25,6 +26,7 @@ BEGIN
`WaitForShutdown`,
`CacheMaxMessages`,
`MaxSteamOfferCount`,
`Maintenance`,
`FeatureFlags`,
`DateFrom`,
`DateTo`)
@ -34,6 +36,7 @@ BEGIN
OLD.WaitForShutdown,
OLD.CacheMaxMessages,
OLD.MaxSteamOfferCount,
OLD.Maintenance,
OLD.FeatureFlags,
OLD.LastModifiedAt,
CURRENT_TIMESTAMP(6));
@ -52,6 +55,7 @@ BEGIN
`WaitForShutdown`,
`CacheMaxMessages`,
`MaxSteamOfferCount`,
`Maintenance`,
`FeatureFlags`,
`Deleted`,
`DateFrom`,
@ -62,6 +66,7 @@ BEGIN
OLD.WaitForShutdown,
OLD.CacheMaxMessages,
OLD.MaxSteamOfferCount,
OLD.Maintenance,
OLD.FeatureFlags,
TRUE,
OLD.LastModifiedAt,

View File

@ -0,0 +1,51 @@
from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.migration_abc import MigrationABC
from bot_data.db_context import DBContext
class MaintenanceModeMigration(MigrationABC):
name = "1.2.0_MaintenanceModeMigration"
def __init__(self, logger: DatabaseLogger, db: DBContext):
MigrationABC.__init__(self)
self._logger = logger
self._db = db
self._cursor = db.cursor
def upgrade(self):
self._logger.debug(__name__, "Running upgrade")
self._cursor.execute(
str(
f"""
ALTER TABLE CFG_Technician
ADD Maintenance BOOLEAN DEFAULT FALSE AFTER MaxSteamOfferCount;
"""
)
)
self._cursor.execute(
str(
f"""
ALTER TABLE CFG_TechnicianHistory
ADD Maintenance BOOLEAN DEFAULT FALSE AFTER MaxSteamOfferCount;
"""
)
)
self._exec(__file__, "config/technician.sql")
def downgrade(self):
self._cursor.execute(
str(
f"""
ALTER TABLE CFG_Technician DROP COLUMN Maintenance;
"""
)
)
self._cursor.execute(
str(
f"""
ALTER TABLE CFG_TechnicianHistory DROP COLUMN Maintenance;
"""
)
)

View File

@ -16,6 +16,7 @@ class TechnicianConfig(TableABC, ConfigurationModelABC):
wait_for_shutdown: int,
cache_max_messages: int,
max_steam_offer_count: int,
maintenance: bool,
feature_flags: dict[FeatureFlagsEnum],
technician_ids: List[int],
ping_urls: List[str],
@ -29,6 +30,8 @@ class TechnicianConfig(TableABC, ConfigurationModelABC):
self._wait_for_shutdown = wait_for_shutdown
self._cache_max_messages = cache_max_messages
self._max_steam_offer_count = max_steam_offer_count
self._maintenance = maintenance
self._feature_flags = feature_flags
self._technician_ids = technician_ids
self._ping_urls = ping_urls
@ -105,6 +108,14 @@ class TechnicianConfig(TableABC, ConfigurationModelABC):
def ping_urls(self, value: List[str]):
self._ping_urls = value
@property
def maintenance(self) -> bool:
return self._maintenance
@maintenance.setter
def maintenance(self, value: bool):
self._maintenance = value
@staticmethod
def get_select_all_string() -> str:
return str(

View File

@ -11,6 +11,7 @@ from bot_api.exception.service_error_code_enum import ServiceErrorCode
from bot_api.exception.service_exception import ServiceException
from bot_api.route.route import Route
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
from bot_core.environment_variables import MAINTENANCE
from bot_data.model.achievement import Achievement
from bot_data.model.auth_role_enum import AuthRoleEnum
from bot_data.model.auth_user import AuthUser
@ -93,7 +94,7 @@ class QueryABC(ObjectType):
if permissions.is_member_technician(guild.get_member(u.discord_id)):
return True
if config.get_configuration("MAINTENANCE"):
if config.get_configuration(MAINTENANCE):
return False
access = False
@ -242,11 +243,11 @@ class QueryABC(ObjectType):
match permission:
case UserRoleEnum.moderator:
can_edit = permissions.is_member_moderator
if config.get_configuration("MAINTENANCE"):
if config.get_configuration(MAINTENANCE):
can_edit = lambda x: False
case UserRoleEnum.admin:
can_edit = permissions.is_member_admin
if config.get_configuration("MAINTENANCE"):
if config.get_configuration(MAINTENANCE):
can_edit = lambda x: False
case UserRoleEnum.technician:
can_edit = permissions.is_member_technician

View File

@ -5,6 +5,7 @@ type TechnicianConfig implements TableWithHistoryQuery {
waitForShutdown: Int
cacheMaxMessages: Int
maxSteamOfferCount: Int
maintenance: Boolean
featureFlagCount: Int
featureFlags: [FeatureFlag]
pingURLs: [String]
@ -25,6 +26,7 @@ type TechnicianConfigHistory implements HistoryTableQuery {
waitForShutdown: Int
cacheMaxMessages: Int
maxSteamOfferCount: Int
maintenance: Boolean
featureFlagCount: Int
featureFlags: [FeatureFlag]
@ -62,6 +64,7 @@ input TechnicianConfigInput {
waitForShutdown: Int
cacheMaxMessages: Int
maxSteamOfferCount: Int
maintenance: Boolean
featureFlags: [FeatureFlagInput]
pingURLs: [String]
technicianIds: [String]

View File

@ -62,6 +62,9 @@ class TechnicianConfigMutation(QueryABC):
technician_config.max_steam_offer_count = (
input["maxSteamOfferCount"] if "maxSteamOfferCount" in input else technician_config.max_steam_offer_count
)
technician_config.max_steam_offer_count = (
input["maintenance"] if "maintenance" in input else technician_config.maintenance
)
old_feature_flags = technician_config.feature_flags
technician_config.feature_flags = (
dict(