Decouple DB instantiation from logger instantion.

This commit is contained in:
Giacomo Leidi 2023-07-11 14:15:01 +02:00
parent 201e259d37
commit c94873c7fa
No known key found for this signature in database
GPG Key ID: D075F59A480549C3
5 changed files with 21 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import sys
import traceback import traceback
from mobilizon_reshare.config.command import CommandConfig from mobilizon_reshare.config.command import CommandConfig
from mobilizon_reshare.config.config import init_logging
from mobilizon_reshare.storage.db import tear_down, init from mobilizon_reshare.storage.db import tear_down, init
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -15,6 +16,7 @@ async def graceful_exit():
async def _safe_execution(function): async def _safe_execution(function):
init_logging()
await init() await init()
return_code = 1 return_code = 1

View File

@ -17,7 +17,7 @@ from mobilizon_reshare.cli.commands.retry.main import (
) )
from mobilizon_reshare.cli.commands.start.main import start_command as start_main from mobilizon_reshare.cli.commands.start.main import start_command as start_main
from mobilizon_reshare.config.command import CommandConfig from mobilizon_reshare.config.command import CommandConfig
from mobilizon_reshare.config.config import current_version, get_settings from mobilizon_reshare.config.config import current_version, get_settings, init_logging
from mobilizon_reshare.config.publishers import publisher_names from mobilizon_reshare.config.publishers import publisher_names
from mobilizon_reshare.dataclasses.event import _EventPublicationStatus from mobilizon_reshare.dataclasses.event import _EventPublicationStatus
from mobilizon_reshare.models.publication import PublicationStatus from mobilizon_reshare.models.publication import PublicationStatus
@ -27,7 +27,8 @@ from mobilizon_reshare.publishers import get_active_publishers
def test_settings(ctx, param, value): def test_settings(ctx, param, value):
if not value or ctx.resilient_parsing: if not value or ctx.resilient_parsing:
return return
get_settings() settings = get_settings()
init_logging(settings)
click.echo("OK!") click.echo("OK!")
ctx.exit() ctx.exit()

View File

@ -1,5 +1,6 @@
import importlib.resources import importlib.resources
import logging import logging
from logging.config import dictConfig
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Optional
@ -38,6 +39,12 @@ def current_version() -> str:
return fp.read() return fp.read()
def init_logging(settings: Optional[Dynaconf] = None):
if settings is None:
settings = get_settings()
dictConfig(settings["logging"])
def get_settings_files_paths() -> Optional[str]: def get_settings_files_paths() -> Optional[str]:
dirs = AppDirs(appname="mobilizon-reshare", version=current_version()) dirs = AppDirs(appname="mobilizon-reshare", version=current_version())
@ -54,7 +61,7 @@ def get_settings_files_paths() -> Optional[str]:
return config_path return config_path
def build_settings(validators: Optional[list[Validator]] = None): def build_settings(validators: Optional[list[Validator]] = None) -> Dynaconf:
""" """
Creates a Dynaconf base object. Configuration files are checked in this order: Creates a Dynaconf base object. Configuration files are checked in this order:
@ -78,7 +85,7 @@ def build_settings(validators: Optional[list[Validator]] = None):
return config return config
def build_and_validate_settings(): def build_and_validate_settings() -> Dynaconf:
""" """
Creates a settings object to be used in the application. It collects and apply generic validators and validators Creates a settings object to be used in the application. It collects and apply generic validators and validators
specific for each publisher, notifier and publication strategy. specific for each publisher, notifier and publication strategy.
@ -128,9 +135,9 @@ class CustomConfig:
cls._instance = None cls._instance = None
def get_settings(): def get_settings() -> Dynaconf:
return CustomConfig.get_instance().settings return CustomConfig.get_instance().settings
def get_settings_without_validation(): def get_settings_without_validation() -> Dynaconf:
return build_settings() return build_settings()

View File

@ -1,5 +1,4 @@
import logging import logging
from logging.config import dictConfig
from pathlib import Path from pathlib import Path
import pkg_resources import pkg_resources
@ -92,11 +91,7 @@ async def tear_down():
return await Tortoise.close_connections() return await Tortoise.close_connections()
async def init(init_logging=True): async def init():
if init_logging:
dictConfig(get_settings()["logging"])
# init storage # init storage
url = get_db_url() url = get_db_url()
if url.scheme == "sqlite": if url.scheme == "sqlite":

View File

@ -3,6 +3,7 @@ import logging
from fastapi import FastAPI from fastapi import FastAPI
from fastapi_pagination import add_pagination from fastapi_pagination import add_pagination
from mobilizon_reshare.config.config import init_logging as init_log
from mobilizon_reshare.storage.db import init as init_db, get_db_url from mobilizon_reshare.storage.db import init as init_db, get_db_url
from mobilizon_reshare.web.backend.events.endpoints import ( from mobilizon_reshare.web.backend.events.endpoints import (
register_endpoints as register_event_endpoints, register_endpoints as register_event_endpoints,
@ -38,7 +39,9 @@ def init_endpoints(app):
@app.on_event("startup") @app.on_event("startup")
async def init_app(init_logging=True): async def init_app(init_logging=True):
if init_logging:
init_log()
check_database() check_database()
await init_db(init_logging=init_logging) await init_db()
init_endpoints(app) init_endpoints(app)
return app return app