aboutsummaryrefslogtreecommitdiffstats
path: root/bot/utils/scheduling.py
diff options
context:
space:
mode:
authorGravatar Joseph Banks <[email protected]>2020-06-17 21:05:47 +0100
committerGravatar GitHub <[email protected]>2020-06-17 21:05:47 +0100
commitc1312f97327733b60555644da49c0419eb6759cb (patch)
treecbfb3f6060eb9338527466a01054315bf43a085e /bot/utils/scheduling.py
parentDelete the loop argument from schedule_task calls (diff)
parentMerge pull request #991 from crazygmr101/feature/cooldown-tag (diff)
Merge branch 'master' into #364-offensive-msg-autodeletion
Diffstat (limited to '')
-rw-r--r--bot/utils/scheduling.py22
1 files changed, 17 insertions, 5 deletions
diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py
index 5760ec2d4..8b778a093 100644
--- a/bot/utils/scheduling.py
+++ b/bot/utils/scheduling.py
@@ -51,20 +51,32 @@ class Scheduler(metaclass=CogABCMeta):
self._scheduled_tasks[task_id] = task
log.debug(f"{self.cog_name}: scheduled task #{task_id} {id(task)}.")
- def cancel_task(self, task_id: t.Hashable) -> None:
- """Unschedule the task identified by `task_id`."""
+ def cancel_task(self, task_id: t.Hashable, ignore_missing: bool = False) -> None:
+ """
+ Unschedule the task identified by `task_id`.
+
+ If `ignore_missing` is True, a warning will not be sent if a task isn't found.
+ """
log.trace(f"{self.cog_name}: cancelling task #{task_id}...")
task = self._scheduled_tasks.get(task_id)
if not task:
- log.warning(f"{self.cog_name}: failed to unschedule {task_id} (no task found).")
+ if not ignore_missing:
+ log.warning(f"{self.cog_name}: failed to unschedule {task_id} (no task found).")
return
- task.cancel()
del self._scheduled_tasks[task_id]
+ task.cancel()
log.debug(f"{self.cog_name}: unscheduled task #{task_id} {id(task)}.")
+ def cancel_all(self) -> None:
+ """Unschedule all known tasks."""
+ log.debug(f"{self.cog_name}: unscheduling all tasks")
+
+ for task_id in self._scheduled_tasks.copy():
+ self.cancel_task(task_id, ignore_missing=True)
+
def _task_done_callback(self, task_id: t.Hashable, done_task: asyncio.Task) -> None:
"""
Delete the task and raise its exception if one exists.
@@ -98,6 +110,6 @@ class Scheduler(metaclass=CogABCMeta):
# Log the exception if one exists.
if exception:
log.error(
- f"{self.cog_name}: error in task #{task_id} {id(scheduled_task)}!",
+ f"{self.cog_name}: error in task #{task_id} {id(done_task)}!",
exc_info=exception
)