-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
163 lines (130 loc) · 5.15 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from typing import Optional, Final
from contextvars import ContextVar
from fastapi import FastAPI, status
from fastapi.responses import JSONResponse
from pydantic import ValidationError
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import Response, StreamingResponse
from sqlalchemy.orm import sessionmaker
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, JSONResponse
import uvicorn
from logging import getLogger, Filter
from src.queue.core import TaskQueue
from src.auth.views import auth_router
from src.repo.views import repo_router
from src.test_modules.views import tm_router
from src.queue.views import task_queue_router
from src.test_gen.views import test_gen_router
from src.target_code.views import tgtcode_router
from src.experiments.views import exp_router
from src.health.views import health_router
from src.exceptions import CowboyRunTimeException
from src.extensions import init_sentry
from src.config import PORT
import uuid
from src.sync_repos import start_sync_thread
from src.database.core import engine
from src.token_registry import token_registry
log = getLogger(__name__)
async def not_found(request, exc):
return JSONResponse(
status_code=status.HTTP_404_NOT_FOUND,
content={"detail": [{"msg": "Not Found."}]},
)
class ExceptionMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
try:
response = await call_next(request)
return response
except CowboyRunTimeException as e:
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={"detail": [{"msg": str(e)}]},
)
except ValidationError as e:
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content={"detail": e.errors()},
)
except Exception as e:
log.exception(f"Unexpected error: {str(e)}")
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"detail": [{"msg": "Internal server error"}]},
)
def create_app(task_queue: TaskQueue, engine) -> FastAPI:
exception_handlers = {404: not_found}
app = FastAPI(exception_handlers=exception_handlers, openapi_url="/docs/openapi.json")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class DBMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
try:
request.state.session_id = str(uuid.uuid4())
task_auth_token = request.headers.get("x-task-auth", None)
if not task_auth_token or not task_auth_token in token_registry:
session = sessionmaker(bind=engine)
request.state.db = session()
request.state.db.id = str(uuid.uuid4())
response = await call_next(request)
except Exception as e:
raise e from None
finally:
db = getattr(request.state, "db", None)
if db:
db.close()
return response
class AddTaskQueueMiddleware(BaseHTTPMiddleware):
async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
request.state.task_queue = task_queue
response = await call_next(request)
return response
app.add_middleware(ExceptionMiddleware)
app.add_middleware(DBMiddleware)
app.add_middleware(AddTaskQueueMiddleware)
app.include_router(auth_router)
app.include_router(repo_router)
app.include_router(tm_router)
app.include_router(task_queue_router)
app.include_router(test_gen_router)
app.include_router(tgtcode_router)
app.include_router(exp_router)
app.include_router(health_router)
return app
def create_server(app: FastAPI, host, port, workers = 2):
# init_sentry()
# start the repo sync thread
# Session = sessionmaker(bind=engine)
# db_session = Session()
# start_sync_thread(db_session, task_queue)
class CustomFilter(Filter):
def filter(self, record):
return "GET /task/get" not in record.getMessage()
# Move the logger configuration into a function that will be called for each worker
async def configure_worker_logger():
uvicorn_logger = getLogger("uvicorn.access")
uvicorn_logger.addFilter(CustomFilter())
app = create_app(TaskQueue(), engine)
# Configure the logging when the worker starts
config = uvicorn.Config(
app,
host=host,
port=port,
workers=workers,
callback_notify=configure_worker_logger # This will run for each worker
)
server = uvicorn.Server(config)
server.run()
if __name__ == "__main__":
app = create_app(TaskQueue(), engine)
create_server(app, "0.0.0.0", int(PORT))