Skip to content

Commit

Permalink
fix(botbase): actually fix close and startup
Browse files Browse the repository at this point in the history
  • Loading branch information
ooliver1 committed Oct 3, 2022
1 parent 241dcf9 commit ab8a243
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions botbase/botbase.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

from asyncio import TimeoutError as AsyncTimeoutError
from asyncio import sleep, wait_for
from contextlib import suppress
from importlib import import_module
from logging import CRITICAL, INFO, Formatter, getLogger
from logging.handlers import RotatingFileHandler
Expand Down Expand Up @@ -236,11 +239,18 @@ def asyncio_handler(self, _, context: dict) -> None:
)
)

async def startup(self) -> None:
async def start(self, *args, **kwargs) -> None:
if self.db_enabled:
db = await create_pool(*self.db_args, **self.db_kwargs)
assert db is not None
self.db = db
for tries in range(5):
try:
db = await create_pool(*self.db_args, **self.db_kwargs)
assert db is not None
self.db = db
except AssertionError:
await sleep(2.5 * tries + 1)
else:
break

await self.db.execute(self.database_init)

if self.aiohttp_enabled:
Expand All @@ -249,9 +259,9 @@ async def startup(self) -> None:
if self.blacklist_enabled and self.db_enabled:
self.blacklist = Blacklist(self.db)

def run(self, *args, **kwargs) -> None:
self.loop.create_task(self.startup())
await super().start(*args, **kwargs)

def run(self, *args, **kwargs) -> None:
cog_dir = f"{self.mod}/cogs" if self.mod else "./cogs"
cogs = Path(cog_dir)

Expand All @@ -268,9 +278,13 @@ def run(self, *args, **kwargs) -> None:
super().run(*args, **kwargs)

async def close(self, *args, **kwargs) -> None:
if self.aiohttp_enabled:
if self.aiohttp_enabled and hasattr(self, "session"):
await self.session.close()

if self.db_enabled and hasattr(self, "db"):
with suppress(AsyncTimeoutError):
await wait_for(self.db.close(), timeout=5)

await super().close(*args, **kwargs)

@staticmethod
Expand Down

0 comments on commit ab8a243

Please sign in to comment.