Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api polling service to message admins when polls exceed threshold #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ persist:
path: "!HERMES!/persist.dat"
admins:
- itismadness

polling:
heartbeat: 60
threshold: 5
27 changes: 27 additions & 0 deletions hermes/hermes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
time.sleep(self.api_poll_heartbeat)
time.sleep(self.bot.api_poll_heartbeat)


class Listener(threading.Thread):
"""
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions hermes/modules/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")