[Downloader] Log errors from initialization task (#3444)

* Update downloader.py

* Create 3444.misc.rst

* enhance(downloader): don't type infinitely on init error

* fix(downloader): unindent `_ready_raised` check

* Update downloader.py
This commit is contained in:
jack1142 2020-01-26 18:16:13 +01:00 committed by Michael H
parent 41fdcb2ae8
commit fc5fc08962
2 changed files with 19 additions and 0 deletions

View File

@ -0,0 +1 @@
Log errors that may happen in initialization task.

View File

@ -55,6 +55,7 @@ class Downloader(commands.Cog):
self._repo_manager = RepoManager()
self._ready = asyncio.Event()
self._init_task = None
self._ready_raised = False
def _create_lib_folder(self, *, remove_first: bool = False) -> None:
if remove_first:
@ -67,13 +68,30 @@ class Downloader(commands.Cog):
async def cog_before_invoke(self, ctx: commands.Context) -> None:
async with ctx.typing():
await self._ready.wait()
if self._ready_raised:
await ctx.send(
"There was an error during Downloader's initialization."
" Check logs for more information."
)
raise commands.CheckFailure()
def cog_unload(self):
if self._init_task is not None:
self._init_task.cancel()
def create_init_task(self):
def _done_callback(task: asyncio.Task) -> None:
exc = task.exception()
if exc is not None:
log.error(
"An unexpected error occurred during Downloader's initialization.",
exc_info=exc,
)
self._ready_raised = True
self._ready.set()
self._init_task = asyncio.create_task(self.initialize())
self._init_task.add_done_callback(_done_callback)
async def initialize(self) -> None:
await self._repo_manager.initialize()