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)
|
|
|
|
|
|
|
|
|
2022-01-08 00:54:27 +01:00
|
|
|
async def init():
|
|
|
|
settings = get_settings()
|
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-12-07 16:58:12 +01:00
|
|
|
db_setup = asyncio.create_task(db.setup())
|
2022-01-08 00:54:27 +01:00
|
|
|
_, _ = await asyncio.wait({db_setup}, return_when=asyncio.FIRST_EXCEPTION)
|
2021-12-07 16:58:12 +01:00
|
|
|
if db_setup.exception():
|
|
|
|
logging.critical("exception during db setup")
|
|
|
|
raise db_setup.exception()
|
2021-08-04 18:53:58 +02:00
|
|
|
|
|
|
|
|
2022-01-08 00:54:27 +01:00
|
|
|
async def _safe_execution(f):
|
|
|
|
init_task = asyncio.create_task(init())
|
|
|
|
_, _ = await asyncio.wait({init_task}, return_when=asyncio.FIRST_EXCEPTION)
|
2021-12-07 16:58:12 +01:00
|
|
|
if init_task.exception():
|
|
|
|
logging.critical("exception during init")
|
|
|
|
# raise init_task.exception()
|
|
|
|
# sys.exit(1)
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.stop()
|
|
|
|
|
2021-08-04 18:53:58 +02:00
|
|
|
return_code = 1
|
|
|
|
try:
|
|
|
|
return_code = await f()
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
|
|
|
finally:
|
|
|
|
logger.debug("Closing")
|
|
|
|
await graceful_exit(return_code)
|
|
|
|
|
|
|
|
|
2022-01-08 00:54:27 +01:00
|
|
|
def safe_execution(f):
|
|
|
|
asyncio.run(_safe_execution(f))
|