2021-08-04 18:53:58 +02:00
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
import traceback
|
|
|
|
from logging.config import dictConfig
|
|
|
|
from pathlib import Path
|
|
|
|
|
2021-09-09 23:04:19 +02:00
|
|
|
from mobilizon_reshare.config.config import get_settings
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.storage.db import tear_down, MoReDB
|
2021-08-04 18:53:58 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def graceful_exit(code):
|
|
|
|
await tear_down()
|
|
|
|
exit(code)
|
|
|
|
|
|
|
|
|
|
|
|
async def init(settings_file):
|
2021-09-09 23:04:19 +02:00
|
|
|
settings = get_settings(settings_file)
|
2021-08-04 18:53:58 +02:00
|
|
|
dictConfig(settings["logging"])
|
|
|
|
db_path = Path(settings.db_path)
|
2021-08-16 10:49:52 +02:00
|
|
|
db = MoReDB(db_path)
|
2021-08-04 18:53:58 +02:00
|
|
|
await db.setup()
|
|
|
|
|
|
|
|
|
|
|
|
async def _safe_execution(f, settings_file):
|
|
|
|
await init(settings_file)
|
|
|
|
return_code = 1
|
|
|
|
try:
|
|
|
|
return_code = await f()
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
|
|
|
finally:
|
|
|
|
logger.debug("Closing")
|
|
|
|
await graceful_exit(return_code)
|
|
|
|
|
|
|
|
|
|
|
|
def safe_execution(f, settings_file):
|
|
|
|
asyncio.run(_safe_execution(f, settings_file))
|