Optimize save_config script

This commit is contained in:
Théophile Diot 2023-09-28 14:28:17 +01:00
parent 48096d711c
commit 0f7df13df3
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
2 changed files with 49 additions and 14 deletions

View file

@ -352,6 +352,8 @@ class Database:
return "The metadata are not set yet, try again"
if "config" in changes:
if not metadata.first_config_saved:
metadata.first_config_saved = True
metadata.config_changed = value
if "custom_configs" in changes:
metadata.custom_configs_changed = value
@ -1733,7 +1735,9 @@ class Database:
)
]
def add_instance(self, hostname: str, port: int, server_name: str) -> str:
def add_instance(
self, hostname: str, port: int, server_name: str, changed: Optional[bool] = True
) -> str:
"""Add instance."""
with self.__db_session() as session:
db_instance = (
@ -1750,6 +1754,12 @@ class Database:
Instances(hostname=hostname, port=port, server_name=server_name)
)
if changed:
with suppress(ProgrammingError, OperationalError):
metadata = session.query(Metadata).get(1)
if metadata is not None:
metadata.instances_changed = True
try:
session.commit()
except BaseException:

View file

@ -340,43 +340,68 @@ if __name__ == "__main__":
if args.init:
sys_exit(0)
err = db.save_config(config_files, args.method)
changes = []
err = db.save_config(config_files, args.method, changed=False)
if not err:
err1 = db.save_custom_configs(custom_confs, args.method)
else:
err = None
err1 = None
if err or err1:
logger.error(
f"Can't save config to database : {err or err1}",
if err:
logger.warning(
f"Couldn't save config to database : {err}, config may not work as expected"
)
sys_exit(1)
else:
changes.append("config")
logger.info("Config successfully saved to database")
if args.method != "ui":
err1 = db.save_custom_configs(custom_confs, args.method, changed=False)
if err1:
logger.warning(
f"Couldn't save custom configs to database : {err1}, custom configs may not work as expected"
)
else:
changes.append("custom_configs")
logger.info("Custom configs successfully saved to database")
if apis:
for api in apis:
endpoint_data = api.endpoint.replace("http://", "").split(":")
err = db.add_instance(
endpoint_data[0], endpoint_data[1].replace("/", ""), api.host
endpoint_data[0],
endpoint_data[1].replace("/", ""),
api.host,
changed=False,
)
if err:
logger.warning(err)
else:
if "instances" not in changes:
changes.append("instances")
logger.info(
f"Instance {endpoint_data[0]} successfully saved to database"
)
else:
err = db.add_instance(
"127.0.0.1",
config_files.get("API_HTTP_PORT", 5000),
config_files.get("API_SERVER_NAME", "bwapi"),
changed=False,
)
if err:
logger.warning(err)
else:
changes.append("instances")
logger.info("Instance 127.0.0.1 successfully saved to database")
# update changes in db
ret = db.checked_changes(changes, value=True)
if ret:
logger.error(
f"An error occurred when setting the changes to checked in the database : {ret}"
)
except SystemExit as e:
raise e
sys_exit(e.code)
except:
logger.error(
f"Exception while executing config saver : {format_exc()}",