Skip to content

Commit

Permalink
PTFE-1196 ping redis on healthcheck calls (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarmet authored Dec 19, 2023
1 parent 4ba4563 commit 1d8b1e9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
18 changes: 16 additions & 2 deletions runner_manager/routers/_health.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
from fastapi import APIRouter, Response
import logging

from fastapi import APIRouter, Depends, Response
from redis import Redis

from runner_manager.dependencies import get_redis

router = APIRouter(prefix="/_health")

log = logging.getLogger(__name__)


@router.get("/", status_code=200)
def healthcheck():
def healthcheck(r: Redis = Depends(get_redis)):
"""Healthcheck endpoint that answers to GET requests on /_health"""

try:
r.ping()
except Exception as exp:
log.error("Redis healthcheck failed: %s", exp)
return Response(status_code=500)

return Response(status_code=200)
17 changes: 16 additions & 1 deletion tests/api/test_health.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
from redis import Redis

from runner_manager.dependencies import get_redis


def test_healthcheck(client):
response = client.get("/_health")
response = client.get("/_health/")
assert response.status_code == 200


def test_healthcheck_redis_unavailable(client, fastapp):
fake_connection = Redis.from_url("redis://localhost:63799/0")
fastapp.dependency_overrides[get_redis] = lambda: fake_connection
response = client.get("/_health/")
assert response.status_code == 500
fastapp.dependency_overrides[get_redis] = get_redis
response = client.get("/_health/")
assert response.status_code == 200

0 comments on commit 1d8b1e9

Please sign in to comment.