2021-08-04 18:53:58 +02:00
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
import traceback
|
|
|
|
from logging.config import dictConfig
|
|
|
|
from pathlib import Path
|
2022-03-06 10:41:02 +01:00
|
|
|
import sys
|
2021-08-04 18:53:58 +02:00
|
|
|
|
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__)
|
|
|
|
|
|
|
|
|
2022-01-27 20:54:14 +01:00
|
|
|
async def graceful_exit():
|
2021-08-04 18:53:58 +02:00
|
|
|
await tear_down()
|
|
|
|
|
|
|
|
|
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)
|
2022-01-26 10:11:16 +01:00
|
|
|
await db.setup()
|
2021-08-04 18:53:58 +02:00
|
|
|
|
|
|
|
|
2022-01-08 00:54:27 +01:00
|
|
|
async def _safe_execution(f):
|
2022-01-26 10:11:16 +01:00
|
|
|
await init()
|
2021-12-07 16:58:12 +01:00
|
|
|
|
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")
|
2022-01-27 20:54:14 +01:00
|
|
|
await graceful_exit()
|
|
|
|
return return_code
|
2021-08-04 18:53:58 +02:00
|
|
|
|
|
|
|
|
2022-01-08 00:54:27 +01:00
|
|
|
def safe_execution(f):
|
2022-01-27 20:54:14 +01:00
|
|
|
code = asyncio.run(_safe_execution(f))
|
2022-03-06 10:41:02 +01:00
|
|
|
sys.exit(code)
|