From 51b9700e2b0984affccfb65bcd6fedbf3314f770 Mon Sep 17 00:00:00 2001 From: Bastian Blank Date: Mon, 18 Dec 2023 13:20:17 +0100 Subject: [PATCH] Add some readiness check --- src/glvd/web/__init__.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/glvd/web/__init__.py b/src/glvd/web/__init__.py index 1049854..051ad7e 100644 --- a/src/glvd/web/__init__.py +++ b/src/glvd/web/__init__.py @@ -3,7 +3,8 @@ import collections.abc import contextlib -from quart import Quart +from quart import Quart, current_app +import sqlalchemy as sa from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine, create_async_engine @@ -43,4 +44,27 @@ def create_app(): from .v1_cves import bp as bp_v1_cves app.register_blueprint(bp_v1_cves) + @app.route('/readiness') + async def readiness(): + failed = False + status = {} + try: + async with getattr(current_app, 'db_begin')() as conn: + (await conn.execute(sa.text('SELECT TRUE'))).one() + status['db_check'] = { + 'status': 'ok', + } + except Exception: + failed = True + status['db_check'] = { + 'status': 'failed', + } + + if failed: + status['status'] = 'failed' + return (status, 503) + else: + status['status'] = 'ok' + return (status, 200) + return app