|
20 | 20 |
|
21 | 21 | import asyncio |
22 | 22 | import logging |
| 23 | +from collections.abc import Awaitable, Callable |
23 | 24 | from datetime import timedelta |
24 | 25 | from typing import cast |
25 | 26 |
|
| 27 | +from fastapi import FastAPI |
26 | 28 | from servicelib.async_utils import cancel_wait_task |
27 | 29 | from servicelib.background_task_utils import exclusive_periodic |
28 | | -from servicelib.logging_utils import log_catch, log_context |
29 | 30 |
|
30 | | -from .constants import APP_CONFIG_KEY, APP_DSM_KEY |
31 | | -from .core.settings import ApplicationSettings |
32 | | -from .dsm_factory import DataManagerProvider |
| 31 | +from .core.settings import get_application_settings |
| 32 | +from .dsm import get_dsm_provider |
33 | 33 | from .modules.redis import get_redis_client |
34 | 34 | from .simcore_s3_dsm import SimcoreS3DataManager |
35 | 35 |
|
|
40 | 40 |
|
41 | 41 | async def dsm_cleaner_task(app: FastAPI) -> None: |
42 | 42 | _logger.info("starting dsm cleaner task...") |
43 | | - dsm: DataManagerProvider = app[APP_DSM_KEY] |
| 43 | + dsm = get_dsm_provider(app) |
44 | 44 | simcore_s3_dsm: SimcoreS3DataManager = cast( |
45 | 45 | SimcoreS3DataManager, dsm.get(SimcoreS3DataManager.get_location_id()) |
46 | 46 | ) |
47 | 47 | await simcore_s3_dsm.clean_expired_uploads() |
48 | 48 |
|
49 | 49 |
|
50 | | -def setup_dsm_cleaner(app: FastAPI): |
51 | | - async def _setup(app: FastAPI): |
52 | | - with ( |
53 | | - log_context(_logger, logging.INFO, msg="setup dsm cleaner"), |
54 | | - log_catch(_logger, reraise=False), |
55 | | - ): |
56 | | - cfg: ApplicationSettings = app[APP_CONFIG_KEY] |
57 | | - assert cfg.STORAGE_CLEANER_INTERVAL_S # nosec |
| 50 | +def setup_dsm_cleaner(app: FastAPI) -> None: |
| 51 | + async def _on_startup(app: FastAPI) -> None: |
| 52 | + cfg = get_application_settings(app) |
| 53 | + assert cfg.STORAGE_CLEANER_INTERVAL_S # nosec |
58 | 54 |
|
59 | | - @exclusive_periodic( |
60 | | - get_redis_client(app), |
61 | | - task_interval=timedelta(seconds=cfg.STORAGE_CLEANER_INTERVAL_S), |
62 | | - retry_after=timedelta(minutes=5), |
63 | | - ) |
64 | | - async def _periodic_dsm_clean() -> None: |
65 | | - await dsm_cleaner_task(app) |
| 55 | + @exclusive_periodic( |
| 56 | + get_redis_client(app), |
| 57 | + task_interval=timedelta(seconds=cfg.STORAGE_CLEANER_INTERVAL_S), |
| 58 | + retry_after=timedelta(minutes=5), |
| 59 | + ) |
| 60 | + async def _periodic_dsm_clean() -> None: |
| 61 | + await dsm_cleaner_task(app) |
66 | 62 |
|
67 | | - storage_background_task = asyncio.create_task( |
68 | | - _periodic_dsm_clean(), name=_TASK_NAME_PERIODICALY_CLEAN_DSM |
69 | | - ) |
70 | | - yield |
| 63 | + app.state.dsm_cleaner_task = asyncio.create_task( |
| 64 | + _periodic_dsm_clean(), name=_TASK_NAME_PERIODICALY_CLEAN_DSM |
| 65 | + ) |
71 | 66 |
|
72 | | - await cancel_wait_task(storage_background_task) |
| 67 | + def _on_shutdown(app: FastAPI) -> Callable[[], Awaitable[None]]: |
| 68 | + async def _stop() -> None: |
| 69 | + assert isinstance(app.state.dsm_cleaner_task, asyncio.Task) # nosec |
| 70 | + await cancel_wait_task(app.state.dsm_cleaner_task) |
73 | 71 |
|
74 | | - app.cleanup_ctx.append(_setup) |
| 72 | + return _stop |
| 73 | + |
| 74 | + app.add_event_handler("startup", _on_startup) |
| 75 | + app.add_event_handler("shutdown", _on_shutdown(app)) |
0 commit comments