Skip to content

Commit 48ee656

Browse files
feat: DIA-993: add health and ready endpoints (#73)
Co-authored-by: Matt Bernstein <[email protected]>
1 parent c240b48 commit 48ee656

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

server/app.py

+47-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import fastapi
21
import logging
32
import pickle
43
from enum import Enum
4+
from typing import Any, Dict, Generic, List, Literal, Optional, TypeVar
5+
import os
6+
7+
import fastapi
8+
from adala.agents import Agent
9+
from fastapi import HTTPException
510
from fastapi.middleware.cors import CORSMiddleware
6-
from typing import Generic, TypeVar, Optional, List, Dict, Any, Literal
7-
from typing_extensions import Annotated
811
from pydantic import BaseModel
912
from pydantic.functional_validators import AfterValidator
10-
from adala.agents import Agent
11-
from tasks.process_file import process_file
13+
from typing_extensions import Annotated
14+
import uvicorn
15+
from redis import Redis
16+
1217
from log_middleware import LogMiddleware
18+
from tasks.process_file import app as celery_app
19+
from tasks.process_file import process_file
1320

1421
logger = logging.getLogger(__name__)
1522

@@ -174,3 +181,38 @@ def cancel_job(job_id):
174181
return Response[JobStatusResponse](
175182
data=JobStatusResponse(status=Status.CANCELED)
176183
)
184+
185+
186+
@app.get('/health')
187+
async def health():
188+
"""
189+
Check if the app is alive.
190+
191+
If app is alive (e.g. started), returns status code 200.
192+
"""
193+
return {'status': 'ok'}
194+
195+
196+
@app.get('/ready')
197+
async def ready():
198+
"""
199+
Check if the app is ready to serve requests.
200+
201+
See if we can reach redis. If not, raise a 500 error. Else, return 200.
202+
"""
203+
try:
204+
redis_url = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')
205+
redis_conn = Redis.from_url(redis_url, socket_connect_timeout=1)
206+
redis_conn.ping()
207+
except Exception as exception:
208+
raise HTTPException(
209+
status_code=500,
210+
detail=f'Error when checking Redis connection: {exception}',
211+
)
212+
213+
return {'status': 'ok'}
214+
215+
216+
if __name__ == '__main__':
217+
# for debugging
218+
uvicorn.run("app:app", host='0.0.0.0', port=30001)

0 commit comments

Comments
 (0)