2021-08-04 18:53:58 +02:00
|
|
|
import asyncio
|
2022-07-12 07:39:56 +02:00
|
|
|
import functools
|
2021-08-04 18:53:58 +02:00
|
|
|
import logging
|
2022-03-06 10:41:02 +01:00
|
|
|
import sys
|
2022-10-14 22:11:27 +02:00
|
|
|
import traceback
|
2021-08-04 18:53:58 +02:00
|
|
|
|
2022-07-12 07:39:56 +02:00
|
|
|
from mobilizon_reshare.config.command import CommandConfig
|
2022-10-14 22:11:27 +02:00
|
|
|
from mobilizon_reshare.storage.db import tear_down, init
|
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-07-12 07:39:56 +02:00
|
|
|
async def _safe_execution(function):
|
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:
|
2022-07-12 07:39:56 +02:00
|
|
|
return_code = await function()
|
2021-08-04 18:53:58 +02:00
|
|
|
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-07-12 07:39:56 +02:00
|
|
|
def safe_execution(function, command_config: CommandConfig = None):
|
|
|
|
if command_config:
|
|
|
|
function = functools.partial(function, command_config)
|
|
|
|
|
|
|
|
code = asyncio.run(_safe_execution(function))
|
2022-03-06 10:41:02 +01:00
|
|
|
sys.exit(code)
|