Skip to content

Commit 2d4f588

Browse files
committed
heartbeat api
1 parent a8ff095 commit 2d4f588

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/backend/app/main.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import sys
44
from contextlib import asynccontextmanager
55
from loguru import logger as log
6-
from fastapi import FastAPI, Request
6+
from fastapi import FastAPI, Request, Depends
77
from fastapi.middleware.cors import CORSMiddleware
88
from fastapi.templating import Jinja2Templates
9-
from fastapi.responses import RedirectResponse, JSONResponse
9+
from fastapi.responses import RedirectResponse, JSONResponse, Response
1010
from psycopg_pool import AsyncConnectionPool
1111
from app.config import settings
1212
from app.projects import project_routes
@@ -15,7 +15,10 @@
1515
from app.users import user_routes
1616
from app.tasks import task_routes
1717
from app.gcp import gcp_routes
18-
18+
from app.models.enums import HTTPStatus
19+
from typing import Annotated
20+
from psycopg import Connection
21+
from app.db.database import get_db
1922

2023
root = os.path.dirname(os.path.abspath(__file__))
2124
frontend_html = Jinja2Templates(directory="frontend_html")
@@ -136,8 +139,22 @@ async def home(request: Request):
136139
return RedirectResponse(f"{settings.API_PREFIX}/docs")
137140

138141

139-
known_browsers = ["Mozilla", "Chrome", "Safari", "Opera", "Edge", "Firefox"]
142+
@api.get("/__heartbeat__")
143+
async def heartbeat_plus_db(db: Annotated[Connection, Depends(get_db)]):
144+
"""Heartbeat that checks that API and DB are both up and running."""
145+
try:
146+
async with db.cursor() as cur:
147+
await cur.execute("SELECT 1")
148+
return Response(status_code=HTTPStatus.OK)
149+
except Exception as e:
150+
log.warning(e)
151+
log.warning("Server failed __heartbeat__ database connection check")
152+
return JSONResponse(
153+
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, content={"error": str(e)}
154+
)
155+
140156

157+
known_browsers = ["Mozilla", "Chrome", "Safari", "Opera", "Edge", "Firefox"]
141158

142159
@api.exception_handler(404)
143160
async def custom_404_handler(request: Request, _):

src/backend/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "drone-tm"
3-
version = "0.1.0"
3+
version = "2025.2.0"
44
description = "Community-driven drone imagery generation"
55
authors = [
66
{name = "HOTOSM", email = "sysadmin@hotosm.org"},

0 commit comments

Comments
 (0)