|
| 1 | +from contextlib import asynccontextmanager |
1 | 2 | import os
|
| 3 | +import signal |
2 | 4 | import sys
|
3 | 5 | import dotenv
|
4 | 6 |
|
|
20 | 22 | from pydantic import BaseModel, ConfigDict, Field, ValidationError
|
21 | 23 | from mangum import Mangum
|
22 | 24 |
|
23 |
| -app = FastAPI() |
| 25 | + |
| 26 | +def handle_sigterm(signum, frame): |
| 27 | + print("Received SIGTERM") |
| 28 | + raise SystemExit(0) |
| 29 | + |
| 30 | + |
| 31 | +@asynccontextmanager |
| 32 | +async def lifespan(app: FastAPI): |
| 33 | + signal.signal(signal.SIGTERM, handle_sigterm) |
| 34 | + signal.signal(signal.SIGINT, handle_sigterm) |
| 35 | + yield |
| 36 | + |
| 37 | + |
| 38 | +app = FastAPI(lifespan=lifespan) |
24 | 39 |
|
25 | 40 | original_env = os.environ.copy()
|
26 | 41 |
|
@@ -52,7 +67,8 @@ class Request(BaseModel):
|
52 | 67 | json_schema_extra={"example": {}},
|
53 | 68 | )
|
54 | 69 |
|
55 |
| - evaluator_cls.preload() |
| 70 | + if not os.getenv("DISABLE_EVALUATORS_PRELOAD"): |
| 71 | + evaluator_cls.preload() |
56 | 72 |
|
57 | 73 | @app.post(
|
58 | 74 | f"/{module_name}/{evaluator_name}/evaluate",
|
@@ -115,27 +131,29 @@ def __init__(self, app, options=None):
|
115 | 131 | super().__init__()
|
116 | 132 |
|
117 | 133 | def load_config(self):
|
118 |
| - config = {key: value for key, value in self.options.items() |
119 |
| - if key in self.cfg.settings and value is not None} # type: ignore |
| 134 | + config = { |
| 135 | + key: value |
| 136 | + for key, value in self.options.items() |
| 137 | + if key in self.cfg.settings and value is not None |
| 138 | + } # type: ignore |
120 | 139 | for key, value in config.items():
|
121 |
| - self.cfg.set(key.lower(), value) # type: ignore |
| 140 | + self.cfg.set(key.lower(), value) # type: ignore |
122 | 141 |
|
123 | 142 | def load(self):
|
124 | 143 | return self.application
|
125 | 144 |
|
126 |
| - |
127 | 145 | host = "0.0.0.0"
|
128 | 146 | port = int(os.getenv("PORT", 8000))
|
129 | 147 | workers = get_cpu_count()
|
130 | 148 |
|
131 | 149 | print(f"Starting server with {workers} workers")
|
132 | 150 |
|
133 | 151 | options = {
|
134 |
| - 'bind': f'{host}:{port}', |
135 |
| - 'workers': workers, |
136 |
| - 'worker_class': 'uvicorn.workers.UvicornWorker', |
137 |
| - 'preload_app': True, |
138 |
| - 'forwarded_allow_ips': '*', |
| 152 | + "bind": f"{host}:{port}", |
| 153 | + "workers": workers, |
| 154 | + "worker_class": "uvicorn.workers.UvicornWorker", |
| 155 | + "preload_app": True, |
| 156 | + "forwarded_allow_ips": "*", |
139 | 157 | }
|
140 | 158 |
|
141 | 159 | StandaloneApplication(app, options).run()
|
|
0 commit comments