aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Mark <[email protected]>2021-06-12 11:11:45 -0700
committerGravatar GitHub <[email protected]>2021-06-12 11:11:45 -0700
commit01826e8969f9d5c35928b4951f9756df0f1cf2bb (patch)
tree44aebe9c4ed0a2cc0b9a4c96e354ed992f1f607a
parentMerge pull request #1636 from ichard26/reminder-for-psf-black (diff)
parentMove the suppressed_exceptions argument to an optional kwarg (diff)
Merge #1637 - add an optional loop kwarg to scheduling.create_task
-rw-r--r--bot/utils/messages.py2
-rw-r--r--bot/utils/scheduling.py19
2 files changed, 17 insertions, 4 deletions
diff --git a/bot/utils/messages.py b/bot/utils/messages.py
index b6f6c1f66..d4a921161 100644
--- a/bot/utils/messages.py
+++ b/bot/utils/messages.py
@@ -54,7 +54,7 @@ def reaction_check(
log.trace(f"Removing reaction {reaction} by {user} on {reaction.message.id}: disallowed user.")
scheduling.create_task(
reaction.message.remove_reaction(reaction.emoji, user),
- HTTPException, # Suppress the HTTPException if adding the reaction fails
+ suppressed_exceptions=(HTTPException,),
name=f"remove_reaction-{reaction}-{reaction.message.id}-{user}"
)
return False
diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py
index 2dc485f24..bb83b5c0d 100644
--- a/bot/utils/scheduling.py
+++ b/bot/utils/scheduling.py
@@ -161,9 +161,22 @@ class Scheduler:
self._log.error(f"Error in task #{task_id} {id(done_task)}!", exc_info=exception)
-def create_task(coro: t.Awaitable, *suppressed_exceptions: t.Type[Exception], **kwargs) -> asyncio.Task:
- """Wrapper for `asyncio.create_task` which logs exceptions raised in the task."""
- task = asyncio.create_task(coro, **kwargs)
+def create_task(
+ coro: t.Awaitable,
+ *,
+ suppressed_exceptions: tuple[t.Type[Exception]] = (),
+ event_loop: t.Optional[asyncio.AbstractEventLoop] = None,
+ **kwargs,
+) -> asyncio.Task:
+ """
+ Wrapper for creating asyncio `Task`s which logs exceptions raised in the task.
+
+ If the loop kwarg is provided, the task is created from that event loop, otherwise the running loop is used.
+ """
+ if event_loop is not None:
+ task = event_loop.create_task(coro, **kwargs)
+ else:
+ task = asyncio.create_task(coro, **kwargs)
task.add_done_callback(partial(_log_task_exception, suppressed_exceptions=suppressed_exceptions))
return task