diff options
| author | 2021-01-26 22:31:28 +0800 | |
|---|---|---|
| committer | 2021-01-26 22:31:28 +0800 | |
| commit | 78a99e7f6fed5f872960ea39873280cbe604465d (patch) | |
| tree | 48ef1eb300817cd5df71764ec5f962225a8606eb /bot/utils/scheduling.py | |
| parent | Update `is_helper_viewable` check (diff) | |
| parent | Merge pull request #1380 from HassanAbouelela/alphabetize-config (diff) | |
Merge branch 'master' into duckpond-check
Diffstat (limited to '')
| -rw-r--r-- | bot/utils/scheduling.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index 03f31d78f..4dd036e4f 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -155,3 +155,20 @@ class Scheduler: # Log the exception if one exists. if exception: self._log.error(f"Error in task #{task_id} {id(done_task)}!", exc_info=exception) + + +def create_task(*args, **kwargs) -> asyncio.Task: + """Wrapper for `asyncio.create_task` which logs exceptions raised in the task.""" + task = asyncio.create_task(*args, **kwargs) + task.add_done_callback(_log_task_exception) + return task + + +def _log_task_exception(task: asyncio.Task) -> None: + """Retrieve and log the exception raised in `task` if one exists.""" + with contextlib.suppress(asyncio.CancelledError): + exception = task.exception() + # Log the exception if one exists. + if exception: + log = logging.getLogger(__name__) + log.error(f"Error in task {task.get_name()} {id(task)}!", exc_info=exception) |