diff --git a/chat_server/server_config.py b/chat_server/server_config.py index 05089e70..f6eb4d40 100644 --- a/chat_server/server_config.py +++ b/chat_server/server_config.py @@ -27,10 +27,12 @@ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import os +from typing import Optional from config import Configuration from chat_server.server_utils.sftp_utils import init_sftp_connector from utils.logging_utils import LOG +from utils.database_utils import DatabaseController server_config_path = os.environ.get( "CHATSERVER_CONFIG", "~/.local/share/neon/credentials.json" @@ -40,17 +42,20 @@ ) -def _init_db_controller(db_config: dict): +def _init_db_controller(db_config: dict) -> Optional[DatabaseController]: from chat_server.server_utils.db_utils import DbUtils - from utils.database_utils import DatabaseController # Determine configured database dialect dialect = db_config.pop("dialect", "mongo") - # Create a database connection - db_controller = DatabaseController(config_data=db_config) - db_controller.attach_connector(dialect=dialect) - db_controller.connect() + try: + # Create a database connection + db_controller = DatabaseController(config_data=db_config) + db_controller.attach_connector(dialect=dialect) + db_controller.connect() + except Exception as e: + LOG.exception(f"DatabaseController init failed: {e}") + return None # Initialize convenience class DbUtils.init(db_controller) @@ -68,14 +73,15 @@ def _init_db_controller(db_config: dict): # ovos-config has built-in mechanisms for loading configuration files based # on envvars, so the configuration structure is simplified from ovos_config.config import Configuration - app_config = Configuration().get("CHAT_SERVER") + app_config = Configuration().get("CHAT_SERVER") or dict() env_spec = os.environ.get("KLAT_ENV") if env_spec and app_config.get(env_spec): LOG.warning("Legacy configuration handling KLAT_ENV envvar") app_config = app_config.get(env_spec) db_controller = _init_db_controller(app_config.get("connection_properties", Configuration().get( - "DATABASE_CONFIG"))) + "DATABASE_CONFIG", + {}))) LOG.info(f"App config: {app_config}")