forked from RemiRigal/Plex-Auto-Languages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add health-check server (RemiRigal#21)
* Add health-check server
- Loading branch information
Showing
4 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ websocket-client>=1.3.1 | |
apprise>=0.9.8 | ||
PyYAML>=6.0 | ||
schedule>=1.1.0 | ||
Flask>=2.0.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |