Decouple DB instantiation from logger instantion. (#188)

This commit is contained in:
Giacomo Leidi 2023-07-11 22:24:56 +02:00 committed by GitHub
parent ff7567dc1b
commit bf3170cb6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import sys
import traceback
from mobilizon_reshare.config.command import CommandConfig
from mobilizon_reshare.config.config import init_logging
from mobilizon_reshare.storage.db import tear_down, init
logger = logging.getLogger(__name__)
@ -15,6 +16,7 @@ async def graceful_exit():
async def _safe_execution(function):
init_logging()
await init()
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.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.dataclasses.event import _EventPublicationStatus
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):
if not value or ctx.resilient_parsing:
return
get_settings()
settings = get_settings()
init_logging(settings)
click.echo("OK!")
ctx.exit()

View File

@ -1,5 +1,6 @@
import importlib.resources
import logging
from logging.config import dictConfig
from pathlib import Path
from typing import Optional
@ -38,6 +39,12 @@ def current_version() -> str:
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]:
dirs = AppDirs(appname="mobilizon-reshare", version=current_version())
@ -54,7 +61,7 @@ def get_settings_files_paths() -> Optional[str]:
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:
@ -78,7 +85,7 @@ def build_settings(validators: Optional[list[Validator]] = None):
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
specific for each publisher, notifier and publication strategy.
@ -128,9 +135,9 @@ class CustomConfig:
cls._instance = None
def get_settings():
def get_settings() -> Dynaconf:
return CustomConfig.get_instance().settings
def get_settings_without_validation():
def get_settings_without_validation() -> Dynaconf:
return build_settings()

View File

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

View File

@ -3,6 +3,7 @@ import logging
from fastapi import FastAPI
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.web.backend.events.endpoints import (
register_endpoints as register_event_endpoints,
@ -38,7 +39,9 @@ def init_endpoints(app):
@app.on_event("startup")
async def init_app(init_logging=True):
if init_logging:
init_log()
check_database()
await init_db(init_logging=init_logging)
await init_db()
init_endpoints(app)
return app