From 121bdd16e8ee53d83822e9320232a65ea2ab540a Mon Sep 17 00:00:00 2001 From: Numerlor <25886452+Numerlor@users.noreply.github.com> Date: Fri, 11 Dec 2020 23:44:59 +0100 Subject: Move parse_queue cleanup into finally block The finally will make sure we reset the task and log it no matter what happens, additionally the clearing of the variable is now only done in one place as the finally also executes when the coro is cancelled --- bot/exts/info/doc/_cog.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index 78d9c6b9b..603d7df97 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -117,20 +117,21 @@ class CachedParser: The coroutine will run as long as the queue is not empty, resetting `self._parse_task` to None when finished. """ log.trace("Starting queue parsing.") - while self._queue: - item, soup = self._queue.pop() - try: - markdown = get_symbol_markdown(soup, item) - await doc_cache.set(item, markdown) - except Exception: - log.exception(f"Unexpected error when handling {item}") - else: - if (future := self._item_futures.get(item)) is not None: - future.set_result(markdown) - await asyncio.sleep(0.1) - - self._parse_task = None - log.trace("Finished parsing queue.") + try: + while self._queue: + item, soup = self._queue.pop() + try: + markdown = get_symbol_markdown(soup, item) + await doc_cache.set(item, markdown) + except Exception: + log.exception(f"Unexpected error when handling {item}") + else: + if (future := self._item_futures.get(item)) is not None: + future.set_result(markdown) + await asyncio.sleep(0.1) + finally: + self._parse_task = None + log.trace("Finished parsing queue.") def _move_to_front(self, item: Union[QueueItem, DocItem]) -> None: """Map a DocItem to its page so that the symbol will be parsed once the page is requested.""" @@ -155,7 +156,6 @@ class CachedParser: await future if self._parse_task is not None: self._parse_task.cancel() - self._parse_task = None self._queue.clear() self._page_symbols.clear() self._item_futures.clear() -- cgit v1.2.3