diff --git a/config.yml.sample b/config.yml.sample index 640fcc2..292f0d4 100644 --- a/config.yml.sample +++ b/config.yml.sample @@ -46,4 +46,6 @@ persist: path: "!HERMES!/persist.dat" admins: - itismadness - +polling: + heartbeat: 60 + threshold: 5 diff --git a/hermes/hermes.py b/hermes/hermes.py index 54b4a0f..d67afa8 100644 --- a/hermes/hermes.py +++ b/hermes/hermes.py @@ -132,6 +132,11 @@ def __init__(self): setattr(self, attr, self._dispatch) self.logger.info("-> Loaded IRC") + self.api_poll_heartbeat = self.config.polling.heartbeat + self.api_poll_threshold = self.config.polling.threshold + self.api_poll_results = [] + self.api_poll_messaged = False + def set_nick(self, connection): connection.send_raw('NICK {}'.format(self.nick)) connection.send_raw('SETIDENT {} {}'.format(self.nick, self.nick)) @@ -313,6 +318,26 @@ def stop(self): self.alive = False self.join() +class PollApi(threading.Thread): + def __init__(self, bot): + super().__init__() + self.bot = bot + + def run(self): + while True: + result = False + user = self.bot.database.get_user(1) + if user == None: + result = True + if len(self.bot.api_poll_results) < self.bot.api_poll_threshold: + self.bot.api_poll_results = self.bot.api_poll_results + [result] + else: + self.bot.api_poll_results = self.bot.api_poll_results[1:] + [result] + if all(self.bot.api_poll_results) and not self.bot.api_poll_messaged: + for admin in self.bot.config.admins: + self.bot.connection.privmsg(admin, "Bad polls exceeded threshold. Is the site down?") + self.bot.api_poll_messaged = True + time.sleep(self.api_poll_heartbeat) class Listener(threading.Thread): """ @@ -488,6 +513,8 @@ def run_hermes(): save_thread = None try: hermes = Hermes() + api_poller = PollApi(hermes) + api_poller.start() save_thread = SaveData(hermes) save_thread.start() # thread = BotCheck(hermes) diff --git a/hermes/modules/bot.py b/hermes/modules/bot.py index 31a447b..e54cfc9 100644 --- a/hermes/modules/bot.py +++ b/hermes/modules/bot.py @@ -92,3 +92,12 @@ def view_log(bot, connection, event): connection.privmsg(event.source.nick, line.strip()) except Exception as e: connection.privmsg(event.source.nick, e) + + +@admin_only() +@privmsg() +@command("resetpolls") +def reset_polls(bot, connection, event): + bot.api_poll_results = [] + bot.api_poll_messaged = False + connection.privmsg(event.source.nick, "Reset API polling service.")