aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils/scheduling.py
diff options
context:
space:
mode:
authorGravatar xithrius <[email protected]>2021-01-26 04:00:58 -0800
committerGravatar xithrius <[email protected]>2021-01-26 04:00:58 -0800
commit2eb51b6d284fa5cbd6ba2c39f2b27950e179366e (patch)
tree84d605798ff1e66fc1cc0a75160a9c557409fb5a /bot/utils/scheduling.py
parentReorganizes constants.py (diff)
parentMerge pull request #1383 from Inheritanc-e/remove_interpreter (diff)
Merge remote-tracking branch 'origin/master' into alphabetize-config
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)