|
2 | 2 | import logging |
3 | 3 |
|
4 | 4 | from fastapi import FastAPI |
5 | | -from servicelib.utils import logged_gather |
| 5 | +from servicelib.logging_utils import log_context |
| 6 | +from servicelib.utils import limited_gather |
6 | 7 |
|
7 | 8 | from . import exceptions, registry_proxy |
8 | 9 | from .core.settings import ApplicationSettings, get_application_settings |
|
15 | 16 | async def registry_caching_task(app: FastAPI) -> None: |
16 | 17 | app_settings = get_application_settings(app) |
17 | 18 | try: |
| 19 | + with log_context(_logger, logging.INFO, msg=f"{TASK_NAME}: starting"): |
| 20 | + assert hasattr(app.state, "registry_cache") # nosec |
| 21 | + assert isinstance(app.state.registry_cache, dict) # nosec |
| 22 | + app.state.registry_cache.clear() |
18 | 23 |
|
19 | | - _logger.info("%s: initializing cache...", TASK_NAME) |
20 | | - assert hasattr(app.state, "registry_cache") # nosec |
21 | | - assert isinstance(app.state.registry_cache, dict) # nosec |
22 | | - app.state.registry_cache.clear() |
23 | 24 | await registry_proxy.list_services(app, registry_proxy.ServiceType.ALL) |
24 | | - _logger.info("%s: initialisation completed", TASK_NAME) |
25 | 25 | while True: |
26 | 26 | _logger.info("%s: waking up, refreshing cache...", TASK_NAME) |
27 | 27 | try: |
28 | | - keys = [] |
29 | | - refresh_tasks = [] |
30 | | - for key in app.state.registry_cache: |
31 | | - path, method = key.split(":") |
32 | | - _logger.debug("refresh %s:%s", method, path) |
33 | | - refresh_tasks.append( |
34 | | - registry_proxy.registry_request( |
35 | | - app, path, method, no_cache=True |
36 | | - ) |
37 | | - ) |
| 28 | + refresh_tasks = [ |
| 29 | + registry_proxy.registry_request(app, key.split(":"), no_cache=True) |
| 30 | + for key in app.state.registry_cache |
| 31 | + ] |
38 | 32 | keys = list(app.state.registry_cache.keys()) |
39 | | - results = await logged_gather(*refresh_tasks) |
| 33 | + results = await limited_gather(*refresh_tasks, log=_logger, limit=50) |
40 | 34 |
|
41 | 35 | for key, result in zip(keys, results, strict=False): |
42 | 36 | app.state.registry_cache[key] = result |
|
0 commit comments