diff --git a/my_modules/app/setup.py b/my_modules/app/setup.py index 8e18249..8192f40 100644 --- a/my_modules/app/setup.py +++ b/my_modules/app/setup.py @@ -68,6 +68,7 @@ LIMITER = Limiter( ) convex_runtime = ConvexWorkerPool(os.getenv("CONVEX_URL")) +app.convex_runtime = convex_runtime @app.before_serving async def init_convex(): diff --git a/routes/__init__.py b/routes/__init__.py index 8cc3206..4ce3485 100644 --- a/routes/__init__.py +++ b/routes/__init__.py @@ -4,3 +4,6 @@ from .auth.login import auth_login_bp from .side.main import side_main_bp from .side.upload import upload_bp + +# Health +from .api.health import health_bp diff --git a/routes/api/health.py b/routes/api/health.py new file mode 100644 index 0000000..83ea5b3 --- /dev/null +++ b/routes/api/health.py @@ -0,0 +1,27 @@ +from my_modules.decoratory.header import format_response, feature_flag_required +from my_modules.app.setup import LIMITER +from my_modules.app.logger import logger + +from quart import Blueprint, current_app + +health_bp = Blueprint("health", __name__) + +@health_bp.route("/convex", methods=["GET"]) +@LIMITER.limit("30 per minute") +@feature_flag_required("convex_health", fallback=False, status_code=404) +@format_response +async def get_convex_health_for_api(): + runtime = getattr(current_app, "convex_runtime", None) + if runtime is None: + return { + "status": "unavailable", + "description": "Convex runtime is not attached to app", + }, 503 + + is_alive = await runtime.is_alive() + metrics = runtime.get_metrics() + + return { + "alive": is_alive, + "metrics": metrics, + }, 200 if is_alive else 503 diff --git a/run.py b/run.py index 5f381e1..d9bab6a 100755 --- a/run.py +++ b/run.py @@ -10,7 +10,8 @@ import routes.handeling.errorsAndBots from routes import ( basic_bp, auth_login_bp, side_main_bp, - upload_bp + upload_bp, + health_bp ) # Views for Requests adding the uris @@ -20,5 +21,7 @@ app.register_blueprint(auth_login_bp) app.register_blueprint(side_main_bp) app.register_blueprint(upload_bp) +app.register_blueprint(health_bp, url_prefix='/health') + if __name__ == '__main__': app.run(debug=WEB_DEBUG, port=5502)