diff options
| author | 2020-02-29 10:31:02 -0800 | |
|---|---|---|
| committer | 2020-03-22 15:54:43 -0700 | |
| commit | 92df4ba80ba85fb9735d8ab6867da4f72e5ed87a (patch) | |
| tree | e5fc7862fea05566f336b031b14c0820d7fd3b39 | |
| parent | HelpChannels: fix last notification time not being set (diff) | |
Scheduler: fix dict size changing while cancelling all tasks
* Make a copy of the dict
* Add a `ignore_missing` param to `cancel_task` to suppress the warning
for a task not being found
| -rw-r--r-- | bot/utils/scheduling.py | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/bot/utils/scheduling.py b/bot/utils/scheduling.py index f8b9d2d48..8b778a093 100644 --- a/bot/utils/scheduling.py +++ b/bot/utils/scheduling.py @@ -51,13 +51,18 @@ 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 del self._scheduled_tasks[task_id] @@ -69,8 +74,8 @@ class Scheduler(metaclass=CogABCMeta): """Unschedule all known tasks.""" log.debug(f"{self.cog_name}: unscheduling all tasks") - for task_id in self._scheduled_tasks: - self.cancel_task(task_id) + 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: """ |