-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PTFE-1196 ping redis on healthcheck calls (#503)
- Loading branch information
Showing
2 changed files
with
32 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |