Skip to content

Commit

Permalink
🥅 error: raise error using "error" event & fix unnecessary else after…
Browse files Browse the repository at this point in the history
… return
  • Loading branch information
Aeris1One committed Nov 14, 2023
1 parent 4b9b47f commit 27df5b9
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions plugins/monitoring/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ async def cog_load(self) -> None:
self.logger.info("Monitoring enabled")
self.loop.start() #pylint: disable=no-member
return
else:
self.logger.warning("Monitoring ping failed %s times", i + 1)
time.sleep(5)
self.logger.error("Monitoring disabled due to ping failure")
self.logger.warning("Monitoring ping failed %s times", i + 1)
time.sleep(5)
self.bot.dispatch("error", RuntimeError("Monitoring disabled due to ping failure"))

async def ping_monitoring(self):
# retrieve Discord Ping
Expand All @@ -53,27 +52,25 @@ async def ping_monitoring(self):
if resp.status != 200:
self.logger.error("Monitoring ping failed with status %s", resp.status)
return False
else:
json = await resp.json()
try:
if not json["ok"]:
self.logger.error("Monitoring ping failed with error : %s", json["msg"])
return False
else:
return True
except KeyError:
self.logger.error("Monitoring ping failed")
json = await resp.json()
try:
if not json["ok"]:
self.logger.error("Monitoring ping failed with error : %s", json["msg"])
return False
return True
except KeyError:
self.logger.error("Monitoring ping failed")
return False

@tasks.loop(seconds=20)
async def loop(self):
if await self.ping_monitoring():
self.error_counter = 0
else:
self.error_counter += 1
if self.error_counter >= 6:
self.logger.error("Monitoring disabled due to multiple ping failure")
self.loop.stop() #pylint: disable=no-member
return
self.error_counter += 1
if self.error_counter >= 6:
self.bot.dispatch("error", RuntimeError("Monitoring disabled due to multiple ping failure"))
self.loop.stop() #pylint: disable=no-member

@loop.before_loop
async def before_ping_monitoring(self):
Expand Down

0 comments on commit 27df5b9

Please sign in to comment.