Explorar o código

Premise of a healthcheck web page

Penta %!s(int64=5) %!d(string=hai) anos
pai
achega
eca3915b8c
Modificáronse 4 ficheiros con 57 adicións e 0 borrados
  1. 4 0
      myanimebot.example.conf
  2. 4 0
      myanimebot/discord.py
  3. 3 0
      myanimebot/globals.py
  4. 46 0
      myanimebot/healthcheck.py

+ 4 - 0
myanimebot.example.conf

@@ -36,3 +36,7 @@ iconMAL = https://cdn.myanimelist.net/img/sp/icon/apple-touch-icon-256.png
 iconAniList = https://anilist.co/img/icons/android-chrome-512x512.png
 iconBot = http://myanimebot.pentou.eu/rsc/bot_avatar.jpg
 
+# Healthcheck web page configuration
+healthcheck_enabled = false
+healthcheck_port = 15200
+healthcheck_ip = 127.0.0.1

+ 4 - 0
myanimebot/discord.py

@@ -14,6 +14,7 @@ import discord
 
 # Our modules
 import myanimebot.anilist as anilist
+import myanimebot.healthcheck as healthcheck
 import myanimebot.commands as commands
 import myanimebot.globals as globals  # TODO Rename globals module
 import myanimebot.myanimelist as myanimelist
@@ -31,6 +32,9 @@ class MyAnimeBot(discord.Client):
 
         if globals.ANI_ENABLED:
             globals.task_feed_anilist = globals.client.loop.create_task(anilist.background_check_feed(globals.client.loop))
+		
+        if globals.HEALTHCHECK_ENABLED:
+            globals.task_healthcheck = globals.client.loop.create_task(healthcheck.main(globals.client.loop))
 
         globals.task_thumbnail = globals.client.loop.create_task(update_thumbnail_catalog(globals.client.loop))
         globals.task_gameplayed = globals.client.loop.create_task(change_gameplayed(globals.client.loop))

+ 3 - 0
myanimebot/globals.py

@@ -67,6 +67,9 @@ ANILIST_PROFILE_URL="https://anilist.co/user/"
 DB_USER_NAME="mal_user" # Column's name for usernames in the t_users table
 MAL_ENABLED=CONFIG.getboolean("mal_enabled", True)
 ANI_ENABLED=CONFIG.getboolean("ani_enabled", True)
+HEALTHCHECK_ENABLED=CONFIG.getboolean("healthcheck_enabled", False)
+HEALTHCHECK_PORT=CONFIG.getint("healthcheck_port", 15200)
+HEALTHCHECK_IP=CONFIG.get("healthcheck_ip", "0.0.0.0")
 
 # Log configuration
 log_format='%(asctime)-13s : %(name)-15s : %(levelname)-8s : %(message)s'

+ 46 - 0
myanimebot/healthcheck.py

@@ -0,0 +1,46 @@
+import requests
+import socket
+import time
+import threading
+
+from http.server import BaseHTTPRequestHandler, HTTPServer
+
+
+import myanimebot.globals as globals
+import myanimebot.utils as utils
+
+class MyServer(BaseHTTPRequestHandler):
+    def do_GET(self):
+        self.send_response(200)
+        self.send_header("Content-type", "text/html")
+        self.end_headers()
+        self.wfile.write(bytes("<html><head><title>MyAnimeBot Healthcheck status</title></head>", "utf-8"))
+        self.wfile.write(bytes("<body>", "utf-8"))
+        self.wfile.write(bytes("<h1>MyAnimeBot Healthcheck status</h1><p>", "utf-8"))
+
+        ####
+        self.wfile.write(bytes("Script version: {}".format(globals.VERSION), "utf-8"))
+        ####
+
+        self.wfile.write(bytes("</p></body></html>", "utf-8"))
+
+def start_healthcheck(ip, port):
+    webServer = HTTPServer((ip, port), MyServer)
+    globals.logger.info("Healthcheck started on http://{}:{}".format(socket.gethostname(), port))
+
+    try:
+        webServer.serve_forever()
+    except KeyboardInterrupt:
+        pass
+    except Exception as e:
+        globals.logger.error("The healthcheck crashed: ".format(e))
+
+async def main(asyncioloop):
+    ''' Main function that starts the Healthcheck web page '''
+
+    globals.logger.info("Starting up Healtcheck...")
+
+    daemon = threading.Thread(name='healthcheck', target=start_healthcheck, args=(globals.HEALTHCHECK_IP, globals.HEALTHCHECK_PORT))
+    daemon.setDaemon(True) 
+    daemon.start()
+