aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-02-15 12:47:42 -0800
committerGravatar MarkKoz <[email protected]>2020-02-15 12:47:42 -0800
commitfd313d2b1dac8b627d3d731e0898fbdff6642616 (patch)
tree9c9a24037bb6995c5127384924588da40dda4da0
parentBot: send not-closed warnings as log messages (diff)
API: add argument to force recreation of the session
-rw-r--r--bot/api.py12
-rw-r--r--bot/bot.py2
2 files changed, 10 insertions, 4 deletions
diff --git a/bot/api.py b/bot/api.py
index c168a869d..37c1497fc 100644
--- a/bot/api.py
+++ b/bot/api.py
@@ -72,16 +72,22 @@ class APIClient:
await self.session.close()
self._ready.clear()
- def recreate(self, **session_kwargs) -> None:
+ def recreate(self, force: bool = False, **session_kwargs) -> None:
"""
Schedule the aiohttp session to be created with `session_kwargs` if it's been closed.
+ If `force` is True, the session will be recreated even if an open one exists. If a task to
+ create the session is pending, it will be cancelled.
+
`session_kwargs` is merged with the kwargs given when the `APIClient` was created and
overwrites those default kwargs.
"""
- if self.session is None or self.session.closed:
+ if force or self.session is None or self.session.closed:
+ if force and self._creation_task:
+ self._creation_task.cancel()
+
# Don't schedule a task if one is already in progress.
- if self._creation_task is None or self._creation_task.done():
+ if force or self._creation_task is None or self._creation_task.done():
self._creation_task = self.loop.create_task(self._create_session(**session_kwargs))
async def maybe_raise_for_status(self, response: aiohttp.ClientResponse, should_raise: bool) -> None:
diff --git a/bot/bot.py b/bot/bot.py
index 088b94a1f..3094a27c5 100644
--- a/bot/bot.py
+++ b/bot/bot.py
@@ -97,4 +97,4 @@ class Bot(commands.Bot):
)
self.http_session = aiohttp.ClientSession(connector=self._connector)
- self.api_client.recreate(connector=self._connector)
+ self.api_client.recreate(force=True, connector=self._connector)