|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # pylint:disable=too-many-branches, unused-argument, too-many-return-statements |
3 | | - |
| 3 | +# pylint:disable=protected-access |
4 | 4 |
|
5 | 5 | import asyncio |
6 | 6 | import inspect |
7 | 7 | import json |
| 8 | +import logging |
8 | 9 | from contextlib import asynccontextmanager |
9 | 10 | from typing import Optional, Callable, Type, Any, List, Dict |
10 | 11 |
|
|
18 | 19 | from ..deployment_modes import DeploymentMode |
19 | 20 | from ...adapter.protocol_adapter import ProtocolAdapter |
20 | 21 |
|
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
21 | 24 |
|
22 | 25 | async def error_stream(e): |
23 | 26 | yield ( |
@@ -122,9 +125,9 @@ async def lifespan(app: FastAPI): |
122 | 125 | app.state.deployment_mode = mode |
123 | 126 | app.state.services_config = services_config |
124 | 127 | app.state.stream_enabled = stream |
125 | | - app.state.response_type = response_type |
126 | 128 | app.state.custom_func = func |
127 | | - app.state.external_runner = runner |
| 129 | + app.state.runner = runner |
| 130 | + app.state.response_type = response_type |
128 | 131 | app.state.endpoint_path = endpoint_path |
129 | 132 | app.state.protocol_adapters = protocol_adapters # Store for later use |
130 | 133 | app.state.custom_endpoints = ( |
@@ -170,6 +173,28 @@ async def _handle_startup( |
170 | 173 | app.state.runner = external_runner |
171 | 174 | app.state.runner_managed_externally = True |
172 | 175 |
|
| 176 | + # in case no runner |
| 177 | + if app.state.runner: |
| 178 | + app_service_instances = ( |
| 179 | + app.state.runner._context_manager.service_instances |
| 180 | + ) |
| 181 | + for instance in app_service_instances.values(): |
| 182 | + # If any instance was not ready, reset runner. |
| 183 | + if not await instance.health(): |
| 184 | + app.state.runner_managed_externally = False |
| 185 | + break |
| 186 | + |
| 187 | + if not app.state.runner_managed_externally: |
| 188 | + try: |
| 189 | + # aexit any possible running instances before set up |
| 190 | + # runner |
| 191 | + await app.state.runner.__aexit__(None, None, None) |
| 192 | + await app.state.runner.__aenter__() |
| 193 | + except Exception as e: |
| 194 | + logger.error( |
| 195 | + f"Warning: Error during runner setup: {e}", |
| 196 | + ) |
| 197 | + |
173 | 198 | elif mode in [ |
174 | 199 | DeploymentMode.DETACHED_PROCESS, |
175 | 200 | DeploymentMode.STANDALONE, |
@@ -243,9 +268,6 @@ def start_celery_worker(): |
243 | 268 | queues=queues, |
244 | 269 | ) |
245 | 270 | except Exception as e: |
246 | | - import logging |
247 | | - |
248 | | - logger = logging.getLogger(__name__) |
249 | 271 | logger.error(f"Failed to start Celery worker: {e}") |
250 | 272 |
|
251 | 273 | worker_thread = threading.Thread( |
@@ -279,7 +301,7 @@ async def _handle_shutdown( |
279 | 301 | # Clean up runner |
280 | 302 | await runner.__aexit__(None, None, None) |
281 | 303 | except Exception as e: |
282 | | - print(f"Warning: Error during runner cleanup: {e}") |
| 304 | + logger.error(f"Warning: Error during runner cleanup: {e}") |
283 | 305 |
|
284 | 306 | @staticmethod |
285 | 307 | async def _create_internal_runner(services_config: ServicesConfig): |
|
0 commit comments