autoconf - only update data when needed and atomic changed metadata update

This commit is contained in:
florian 2023-08-22 16:33:24 +02:00
parent 00cb6c1a8b
commit 3d13cf345e
No known key found for this signature in database
GPG key ID: 3D80806F12602A7C
2 changed files with 98 additions and 66 deletions

View file

@ -3,6 +3,7 @@
from os import getenv
from time import sleep
from typing import Optional
from copy import deepcopy
from ConfigCaller import ConfigCaller # type: ignore
from Database import Database # type: ignore
@ -15,7 +16,7 @@ class Config(ConfigCaller):
self.__logger = setup_logger("Config", getenv("LOG_LEVEL", "INFO"))
self.__instances = []
self.__services = []
self.__configs = []
self.__configs = {}
self.__config = {}
self._db = Database(self.__logger)
@ -34,43 +35,65 @@ class Config(ConfigCaller):
env_instances["SERVER_NAME"] = env_instances["SERVER_NAME"].strip()
return self._full_env(env_instances, env_services)
def update_needed(self, instances, services, configs=None) -> bool:
def update_needed(self, instances, services, configs={}) -> bool:
if instances != self.__instances:
return True
elif services != self.__services:
return True
elif not configs is None and configs != self.__configs:
elif configs != self.__configs:
return True
return False
def apply(self, instances, services, configs=None) -> bool:
def apply(self, instances, services, configs={}) -> bool:
success = True
# update values
self.__instances = instances
self.__services = services
self.__configs = configs
self.__config = self.__get_full_env()
# update types
updates = {
"instances": False,
"services": False,
"configs": False,
"config": False
}
changes = []
if instances != self.__instances:
self.__instances = instances
updates["instances"] = True
changes.append("instances")
if services != self.__services:
self.__services = services
updates["services"] = True
if configs != self.__configs:
self.__configs = configs
updates["configs"] = True
changes.append("custom_configs")
if updates["instances"] or updates["services"]:
old_env = self.__get_full_env()
self.__config = self.__get_full_env()
if self.__config != old_env:
updates["config"] = True
changes.append("config")
custom_configs = []
for config_type in self.__configs:
for file, data in self.__configs[config_type].items():
site = None
name = file
if "/" in file:
exploded = file.split("/")
site = exploded[0]
name = exploded[1]
custom_configs.append(
{
"value": data,
"exploded": [
site,
config_type,
name.replace(".conf", ""),
],
}
)
if updates["configs"]:
for config_type in self.__configs:
for file, data in self.__configs[config_type].items():
site = None
name = file
if "/" in file:
exploded = file.split("/")
site = exploded[0]
name = exploded[1]
custom_configs.append(
{
"value": data,
"exploded": [
site,
config_type,
name.replace(".conf", ""),
],
}
)
while not self._db.is_initialized():
self.__logger.warning(
@ -79,24 +102,31 @@ class Config(ConfigCaller):
sleep(5)
# update instances in database
err = self._db.update_instances(self.__instances)
if err:
self.__logger.error(f"Failed to update instances: {err}")
if updates["instances"]:
err = self._db.update_instances(self.__instances, changed=False)
if err:
self.__logger.error(f"Failed to update instances: {err}")
# save config to database
err = self._db.save_config(self.__config, "autoconf")
if err:
success = False
self.__logger.error(
f"Can't save config in database: {err}, config may not work as expected",
)
if updates["config"]:
err = self._db.save_config(self.__config, "autoconf", changed=False)
if err:
success = False
self.__logger.error(
f"Can't save config in database: {err}, config may not work as expected",
)
# save custom configs to database
err = self._db.save_custom_configs(custom_configs, "autoconf")
if err:
success = False
if updates["configs"]:
err = self._db.save_custom_configs(custom_configs, "autoconf", changed=False)
if err:
success = False
self.__logger.error(
f"Can't save autoconf custom configs in database: {err}, custom configs may not work as expected",
)
# update changes in db
ret = self._db.checked_changes(changes, value=True)
if ret:
self.__logger.error(
f"Can't save autoconf custom configs in database: {err}, custom configs may not work as expected",
f"An error occurred when setting the changes to checked in the database : {ret}"
)
return success

View file

@ -329,8 +329,8 @@ class Database:
except BaseException:
return format_exc()
def checked_changes(self, changes: Optional[List[str]] = None) -> str:
"""Set that the config, the custom configs, the plugins and instances didn't change"""
def checked_changes(self, changes: Optional[List[str]] = None, value: Optional[bool] = False) -> str:
"""Set changed bit for config, custom configs, instances and plugins"""
changes = changes or [
"config",
"custom_configs",
@ -345,13 +345,13 @@ class Database:
return "The metadata are not set yet, try again"
if "config" in changes:
metadata.config_changed = False
metadata.config_changed = value
if "custom_configs" in changes:
metadata.custom_configs_changed = False
metadata.custom_configs_changed = value
if "external_plugins" in changes:
metadata.external_plugins_changed = False
metadata.external_plugins_changed = value
if "instances" in changes:
metadata.instances_changed = False
metadata.instances_changed = value
session.commit()
except BaseException:
return format_exc()
@ -470,7 +470,7 @@ class Database:
return True, ""
def save_config(self, config: Dict[str, Any], method: str) -> str:
def save_config(self, config: Dict[str, Any], method: str, changed: Optional[bool] = True) -> str:
"""Save the config in the database"""
to_put = []
with self.__db_session() as session:
@ -716,12 +716,13 @@ class Database:
Global_values.suffix == suffix,
).update({Global_values.value: value})
with suppress(ProgrammingError, OperationalError):
metadata = session.query(Metadata).get(1)
if metadata is not None:
if not metadata.first_config_saved:
metadata.first_config_saved = True
metadata.config_changed = True
if changed:
with suppress(ProgrammingError, OperationalError):
metadata = session.query(Metadata).get(1)
if metadata is not None:
if not metadata.first_config_saved:
metadata.first_config_saved = True
metadata.config_changed = True
try:
session.add_all(to_put)
@ -732,7 +733,7 @@ class Database:
return ""
def save_custom_configs(
self, custom_configs: List[Dict[str, Tuple[str, List[str]]]], method: str
self, custom_configs: List[Dict[str, Tuple[str, List[str]]]], method: str, changed: Optional[bool] = True
) -> str:
"""Save the custom configs in the database"""
message = ""
@ -813,11 +814,11 @@ class Database:
else {}
)
)
with suppress(ProgrammingError, OperationalError):
metadata = session.query(Metadata).get(1)
if metadata is not None:
metadata.custom_configs_changed = True
if changed:
with suppress(ProgrammingError, OperationalError):
metadata = session.query(Metadata).get(1)
if metadata is not None:
metadata.custom_configs_changed = True
try:
session.add_all(to_put)
@ -1749,7 +1750,7 @@ class Database:
return ""
def update_instances(self, instances: List[Dict[str, Any]]) -> str:
def update_instances(self, instances: List[Dict[str, Any]], changed: Optional[bool] = True) -> str:
"""Update instances."""
to_put = []
with self.__db_session() as session:
@ -1764,10 +1765,11 @@ class Database:
)
)
with suppress(ProgrammingError, OperationalError):
metadata = session.query(Metadata).get(1)
if metadata is not None:
metadata.instances_changed = True
if changed:
with suppress(ProgrammingError, OperationalError):
metadata = session.query(Metadata).get(1)
if metadata is not None:
metadata.instances_changed = True
try:
session.add_all(to_put)