diff --git a/reflex/app.py b/reflex/app.py index 935fe7900..a5a6dfe6b 100644 --- a/reflex/app.py +++ b/reflex/app.py @@ -1356,20 +1356,22 @@ async def health() -> JSONResponse: health_status = {"status": True} status_code = 200 - db_status, redis_status = await asyncio.gather( - get_db_status(), prerequisites.get_redis_status() - ) + tasks = [] - health_status["db"] = db_status + if get_config().db_url: + tasks.append(get_db_status()) + if get_config().redis_url: + tasks.append(prerequisites.get_redis_status()) - if redis_status is None: + results = await asyncio.gather(*tasks) + + for result in results: + health_status |= result + + if "redis" in health_status and health_status["redis"] is None: health_status["redis"] = False - else: - health_status["redis"] = redis_status - if not health_status["db"] or ( - not health_status["redis"] and redis_status is not None - ): + if not all(health_status.values()): health_status["status"] = False status_code = 503 diff --git a/reflex/model.py b/reflex/model.py index cb8bed29b..de63589fc 100644 --- a/reflex/model.py +++ b/reflex/model.py @@ -141,15 +141,13 @@ def get_async_engine(url: str | None) -> sqlalchemy.ext.asyncio.AsyncEngine: return _ASYNC_ENGINE[url] -async def get_db_status() -> bool: +async def get_db_status() -> dict[str, bool]: """Checks the status of the database connection. Attempts to connect to the database and execute a simple query to verify connectivity. Returns: - bool: The status of the database connection: - - True: The database is accessible. - - False: The database is not accessible. + The status of the database connection. """ status = True try: @@ -159,7 +157,7 @@ async def get_db_status() -> bool: except sqlalchemy.exc.OperationalError: status = False - return status + return {"db": status} SQLModelOrSqlAlchemy = Union[ diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 25e753d09..97c6a5974 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -372,16 +372,13 @@ def parse_redis_url() -> str | dict | None: return config.redis_url -async def get_redis_status() -> bool | None: +async def get_redis_status() -> dict[str, bool | None]: """Checks the status of the Redis connection. Attempts to connect to Redis and send a ping command to verify connectivity. Returns: - bool or None: The status of the Redis connection: - - True: Redis is accessible and responding. - - False: Redis is not accessible due to a connection error. - - None: Redis not used i.e redis_url is not set in rxconfig. + The status of the Redis connection. """ try: status = True @@ -393,7 +390,7 @@ async def get_redis_status() -> bool | None: except exceptions.RedisError: status = False - return status + return {"redis": status} def validate_app_name(app_name: str | None = None) -> str: