|
17 | 17 | from models_library.services_types import ServiceKey, ServiceVersion |
18 | 18 | from pydantic import NonNegativeInt, TypeAdapter |
19 | 19 | from servicelib.fastapi.tracing import setup_httpx_client_tracing |
20 | | -from servicelib.logging_utils import log_context |
| 20 | +from servicelib.logging_utils import log_catch, log_context |
21 | 21 | from starlette import status |
22 | 22 | from tenacity.asyncio import AsyncRetrying |
23 | 23 | from tenacity.before_sleep import before_sleep_log |
24 | 24 | from tenacity.stop import stop_after_delay |
25 | 25 | from tenacity.wait import wait_random |
26 | 26 |
|
27 | | -from ..core.settings import ApplicationSettings |
| 27 | +from ..core.settings import ApplicationSettings, DirectorSettings |
28 | 28 | from ..errors import DirectorUnresponsiveError |
29 | 29 |
|
30 | 30 | _logger = logging.getLogger(__name__) |
@@ -291,30 +291,32 @@ async def get_service_extras( |
291 | 291 |
|
292 | 292 | async def director_lifespan(app: FastAPI) -> AsyncIterator[State]: |
293 | 293 | client: DirectorApi | None = None |
| 294 | + settings = app.state.settings.CATALOG_DIRECTOR |
294 | 295 |
|
295 | | - if settings := app.state.settings.CATALOG_DIRECTOR: |
296 | | - with log_context( |
297 | | - _logger, logging.DEBUG, "Setup director at %s", f"{settings.base_url=}" |
298 | | - ): |
299 | | - async for attempt in AsyncRetrying(**_director_startup_retry_policy): |
300 | | - with attempt: |
301 | | - client = DirectorApi(base_url=settings.base_url, app=app) |
302 | | - if not await client.is_responsive(): |
303 | | - with suppress(Exception): |
304 | | - await client.close() |
305 | | - raise DirectorUnresponsiveError |
306 | | - |
307 | | - _logger.info( |
308 | | - "Connection to director-v0 succeeded [%s]", |
309 | | - json_dumps(attempt.retry_state.retry_object.statistics), |
310 | | - ) |
311 | | - |
312 | | - # set when connected |
313 | | - app.state.director_api = client |
| 296 | + assert isinstance(settings, DirectorSettings) # nosec |
| 297 | + |
| 298 | + with log_context( |
| 299 | + _logger, logging.DEBUG, "Setup director at %s", f"{settings.base_url=}" |
| 300 | + ): |
| 301 | + async for attempt in AsyncRetrying(**_director_startup_retry_policy): |
| 302 | + with attempt: |
| 303 | + client = DirectorApi(base_url=settings.base_url, app=app) |
| 304 | + if not await client.is_responsive(): |
| 305 | + with suppress(Exception): |
| 306 | + await client.close() |
| 307 | + raise DirectorUnresponsiveError |
| 308 | + |
| 309 | + _logger.info( |
| 310 | + "Connection to director-v0 succeeded [%s]", |
| 311 | + json_dumps(attempt.retry_state.retry_object.statistics), |
| 312 | + ) |
| 313 | + |
| 314 | + # set when connected |
| 315 | + app.state.director_api = client |
314 | 316 |
|
315 | 317 | try: |
316 | 318 | yield {} |
317 | 319 | finally: |
318 | 320 | if client: |
319 | | - await client.close() |
320 | | - _logger.debug("Director client closed successfully") |
| 321 | + with log_catch(_logger, reraise=False): |
| 322 | + await asyncio.wait_for(client.close(), timeout=10) |
0 commit comments