diff --git a/documentation/api/change_log.rst b/documentation/api/change_log.rst index b19fdf993..ad1dd350f 100644 --- a/documentation/api/change_log.rst +++ b/documentation/api/change_log.rst @@ -9,6 +9,7 @@ v3.0-10 | 2023-06-12 """""""""""""""""""" - Introduced the ``storage-efficiency`` field to the ``flex-model``field for `/sensors//schedules/trigger` (POST). +- Introduced the ``database_redis`` optional field to the response of the endpoint `/health/ready` (GET). v3.0-9 | 2023-04-26 """"""""""""""""""" diff --git a/documentation/changelog.rst b/documentation/changelog.rst index fb9f7db29..167a8fb8c 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -23,8 +23,11 @@ Infrastructure / Support ---------------------- * Add support for profiling Flask API calls using ``pyinstrument`` (if installed). Can be enabled by setting the environment variable ``FLEXMEASURES_PROFILE_REQUESTS`` to ``True`` [see `PR #722 `_] +* The endpoint `[POST] /health/ready `_ returns the status of the Redis connection, if configured [see `PR #699 `_] +/api/v3_0/health/ready + v0.14.1 | June 26, 2023 ============================ diff --git a/flexmeasures/api/v3_0/health.py b/flexmeasures/api/v3_0/health.py index cff0db8e3..cf2909d6b 100644 --- a/flexmeasures/api/v3_0/health.py +++ b/flexmeasures/api/v3_0/health.py @@ -2,6 +2,8 @@ from flask_classful import FlaskView, route from flask_json import as_json + +from redis.exceptions import ConnectionError from flexmeasures.data import db @@ -14,6 +16,18 @@ def _check_sql_database(): return False +def _check_redis() -> bool: + """Check status of the redis instance + + :return: True if the redis instance is active, False otherwise + """ + try: + current_app.redis_connection.ping() + return True + except ConnectionError: + return False + + class HealthAPI(FlaskView): route_base = "/health" @@ -32,11 +46,19 @@ def is_ready(self): .. sourcecode:: json { - 'database_sql': True + 'database_sql': True, + 'database_redis': False } """ - status = {"database_sql": _check_sql_database()} # TODO: check redis + + status = { + "database_sql": _check_sql_database(), + } + + if current_app.config.get("FLEXMEASURES_REDIS_PASSWORD") is not None: + status["database_redis"] = _check_redis() + if all(status.values()): return status, 200 else: