|
1 |
| -import fastapi |
2 | 1 | import logging
|
3 | 2 | import pickle
|
4 | 3 | 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 |
5 | 10 | from fastapi.middleware.cors import CORSMiddleware
|
6 |
| -from typing import Generic, TypeVar, Optional, List, Dict, Any, Literal |
7 |
| -from typing_extensions import Annotated |
8 | 11 | from pydantic import BaseModel
|
9 | 12 | 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 | + |
12 | 17 | from log_middleware import LogMiddleware
|
| 18 | +from tasks.process_file import app as celery_app |
| 19 | +from tasks.process_file import process_file |
13 | 20 |
|
14 | 21 | logger = logging.getLogger(__name__)
|
15 | 22 |
|
@@ -174,3 +181,38 @@ def cancel_job(job_id):
|
174 | 181 | return Response[JobStatusResponse](
|
175 | 182 | data=JobStatusResponse(status=Status.CANCELED)
|
176 | 183 | )
|
| 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