1
0

healthcheck.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import requests
  2. import socket
  3. import time
  4. import threading
  5. from http.server import BaseHTTPRequestHandler, HTTPServer
  6. import myanimebot.globals as globals
  7. import myanimebot.utils as utils
  8. class MyServer(BaseHTTPRequestHandler):
  9. def do_GET(self):
  10. self.send_response(200)
  11. self.send_header("Content-type", "text/html")
  12. self.end_headers()
  13. self.wfile.write(bytes("<html><head><title>MyAnimeBot Healthcheck status</title></head>", "utf-8"))
  14. self.wfile.write(bytes("<body>", "utf-8"))
  15. self.wfile.write(bytes("<h1>MyAnimeBot Healthcheck status</h1><p>", "utf-8"))
  16. ####
  17. self.wfile.write(bytes("Script version: {}".format(globals.VERSION), "utf-8"))
  18. ####
  19. self.wfile.write(bytes("</p></body></html>", "utf-8"))
  20. def start_healthcheck(ip, port):
  21. webServer = HTTPServer((ip, port), MyServer)
  22. globals.logger.info("Healthcheck started on http://{}:{}".format(socket.gethostname(), port))
  23. try:
  24. webServer.serve_forever()
  25. except KeyboardInterrupt:
  26. pass
  27. except Exception as e:
  28. globals.logger.error("The healthcheck crashed: ".format(e))
  29. async def main(asyncioloop):
  30. ''' Main function that starts the Healthcheck web page '''
  31. globals.logger.info("Starting up Healtcheck...")
  32. daemon = threading.Thread(name='healthcheck', target=start_healthcheck, args=(globals.HEALTHCHECK_IP, globals.HEALTHCHECK_PORT))
  33. daemon.setDaemon(True)
  34. daemon.start()