feat: catch_everything_and_restart print name of the task (#2417)

remove type from `Coroutine` from the create_tasks
This commit is contained in:
dni ⚡ 2024-04-17 10:51:07 +02:00 committed by GitHub
parent d9880c4de8
commit 0c3aabf77a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,7 +4,7 @@ import time
import traceback import traceback
import uuid import uuid
from http import HTTPStatus from http import HTTPStatus
from typing import Coroutine, Dict, List, Optional from typing import Dict, List, Optional
from loguru import logger from loguru import logger
from py_vapid import Vapid from py_vapid import Vapid
@ -33,7 +33,7 @@ def create_permanent_task(func):
return create_task(catch_everything_and_restart(func)) return create_task(catch_everything_and_restart(func))
def create_unique_task(name: str, coro: Coroutine): def create_unique_task(name: str, coro):
if unique_tasks.get(name): if unique_tasks.get(name):
logger.warning(f"task `{name}` already exists, cancelling it") logger.warning(f"task `{name}` already exists, cancelling it")
try: try:
@ -45,8 +45,8 @@ def create_unique_task(name: str, coro: Coroutine):
return task return task
def create_permanent_unique_task(name: str, coro: Coroutine): def create_permanent_unique_task(name: str, coro):
return create_unique_task(name, catch_everything_and_restart(coro)) return create_unique_task(name, catch_everything_and_restart(coro, name))
def cancel_all_tasks(): def cancel_all_tasks():
@ -62,17 +62,17 @@ def cancel_all_tasks():
logger.warning(f"error while cancelling task `{name}`: {exc!s}") logger.warning(f"error while cancelling task `{name}`: {exc!s}")
async def catch_everything_and_restart(func): async def catch_everything_and_restart(func, name: str = "unnamed"):
try: try:
await func() await func()
except asyncio.CancelledError: except asyncio.CancelledError:
raise # because we must pass this up raise # because we must pass this up
except Exception as exc: except Exception as exc:
logger.error("caught exception in background task:", exc) logger.error(f"exception in background task `{name}`:", exc)
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())
logger.error("will restart the task in 5 seconds.") logger.error("will restart the task in 5 seconds.")
await asyncio.sleep(5) await asyncio.sleep(5)
await catch_everything_and_restart(func) await catch_everything_and_restart(func, name)
invoice_listeners: Dict[str, asyncio.Queue] = {} invoice_listeners: Dict[str, asyncio.Queue] = {}