fix(commands:iplocalise|Network): close TUXBOT-BOT-4T, close TUXBOT-BOT-4S

This commit is contained in:
Romain J 2021-05-16 17:07:26 +02:00
parent f8f56add97
commit 09e69166ad
3 changed files with 23 additions and 7 deletions

View file

@ -120,15 +120,26 @@ async def get_ipwhois_result(loop, ip: str) -> NoReturn | dict:
cache=Cache.MEMORY,
namespace="network",
)
async def get_ipinfo_result(apikey: str, ip: str) -> dict:
async def get_ipinfo_result(loop, apikey: str, ip: str) -> dict:
def _get_ipinfo_result(_ip: str) -> NoReturn | dict:
"""
Q. Why no getHandlerAsync ?
A. Use of this return "Unclosed client session" and "Unclosed connector"
"""
try:
handler = ipinfo.getHandlerAsync(
apikey, request_options={"timeout": 7}
)
return (await handler.getDetails(ip)).all
handler = ipinfo.getHandler(apikey, request_options={"timeout": 7})
return (handler.getDetails(ip)).all
except RequestQuotaExceededError:
return {}
try:
return await asyncio.wait_for(
loop.run_in_executor(None, _get_ipinfo_result, str(ip)),
timeout=8,
)
except asyncio.exceptions.TimeoutError:
return {}
@cached(
ttl=24 * 3600,

View file

@ -103,6 +103,8 @@ class Network(commands.Cog):
self._peeringdb_net = await s.json()
except asyncio.exceptions.TimeoutError:
pass
else:
log.log(logging.INFO, "_update_peering_db")
# =========================================================================
# =========================================================================
@ -126,7 +128,7 @@ class Network(commands.Cog):
ip_hostname = await get_hostname(self.bot.loop, str(ip_address))
ipinfo_result = await get_ipinfo_result(
self.__config.ipinfoKey, ip_address
self.bot.loop, self.__config.ipinfoKey, ip_address
)
ipwhois_result = await get_ipwhois_result(self.bot.loop, ip_address)

View file

@ -332,7 +332,10 @@ class Tux(commands.AutoShardedBot):
self.console.log(
"Canceling", task.get_name(), f"({task.get_coro()})"
)
try:
task.cancel()
except Exception as e:
self.console.log(e)
await asyncio.gather(*pending, return_exceptions=False)
await super().close()