aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils/scheduling.py
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2021-01-29 19:46:30 +0300
committerGravatar GitHub <[email protected]>2021-01-29 19:46:30 +0300
commit9b2d71f91ab827b7a6bad59a87026415b7ba519b (patch)
treef7d93433ccaf9ed2ebd843368767f0e545be14c5 /bot/utils/scheduling.py
parentMerge branch 'trashcan-mods' of github.com:python-discord/bot into trashcan-mods (diff)
parentMerge pull request #1372 from anand2312/duckpond-check (diff)
Merge branch 'master' into trashcan-mods
Diffstat (limited to '')
-rw-r--r--bot/utils/scheduling.py17
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)