Skip to content

Commit

Permalink
Add health-check server (RemiRigal#21)
Browse files Browse the repository at this point in the history
* Add health-check server
  • Loading branch information
RemiRigal authored Apr 27, 2022
1 parent f476aef commit c868c0e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ FROM python:3.8-alpine
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8

RUN apk add --update curl && \
rm -rf /var/cache/apk/*

COPY ./requirements.txt /app/requirements.txt

RUN python3 -m pip install --no-cache-dir -r /app/requirements.txt
Expand All @@ -13,4 +16,6 @@ WORKDIR /app

VOLUME /config

HEALTHCHECK CMD curl --fail http://localhost/ || exit 1

ENTRYPOINT ["python3", "main.py", "-c", "/config/config.yaml"]
12 changes: 11 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
from utils.scheduler import Scheduler
from utils.configuration import Configuration
from utils.plex import PlexUtils
from utils.healthcheck import HealthcheckServer


class PlexAutoLanguages(object):

def __init__(self, user_config_path: str):
self.alive = False
self.set_signal_handlers()
self.notifier = None
self.set_signal_handlers()
self.healthcheck_server = HealthcheckServer("Plex-Auto-Languages", self.is_ready, self.is_healthy)
self.healthcheck_server.start()
self.config = Configuration(user_config_path)
# Plex
self.plex = PlexServer(self.config.get("plex.url"), self.config.get("plex.token"))
Expand Down Expand Up @@ -48,6 +51,12 @@ def get_plex_user_id(self):
logger.error("Unable to find the user id associated with the provided Plex Token")
sys.exit(0)

def is_ready(self):
return self.alive

def is_healthy(self):
return self.alive and self.notifier is not None and self.notifier.is_alive()

def set_signal_handlers(self):
signal.signal(signal.SIGINT, self.stop)
signal.signal(signal.SIGTERM, self.stop)
Expand All @@ -69,6 +78,7 @@ def start(self):
logger.info("Stopping scheduler")
self.scheduler.stop_event.set()
logger.info("Stopping alert listener")
self.healthcheck_server.shutdown()

def alert_listener_callback(self, message: dict):
if self.config.get("trigger_on_play") and message["type"] == "playing":
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ websocket-client>=1.3.1
apprise>=0.9.8
PyYAML>=6.0
schedule>=1.1.0
Flask>=2.0.3
41 changes: 41 additions & 0 deletions utils/healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json
import logging
from flask import Flask
from typing import Callable
from threading import Thread
from werkzeug.serving import make_server


flask_logger = logging.getLogger("werkzeug")
flask_logger.setLevel(logging.ERROR)


class HealthcheckServer(Thread):

def __init__(self, name: str, is_ready: Callable, is_healthy: Callable):
super(HealthcheckServer, self).__init__()
self._is_healthy = is_healthy
self._is_ready = is_ready
self._app = Flask(name)
self._server = make_server("0.0.0.0", 80, self._app)
self._ctx = self._app.app_context()
self._ctx.push()

@self._app.route("/")
@self._app.route("/health")
def __health():
healthy = self._is_healthy()
code = 200 if healthy else 400
return json.dumps({"healthy": healthy}), code

@self._app.route("/ready")
def __ready():
ready = self._is_ready()
code = 200 if ready else 400
return json.dumps({"ready": ready}), code

def run(self):
self._server.serve_forever()

def shutdown(self):
self._server.shutdown()

0 comments on commit c868c0e

Please sign in to comment.