Skip to content

Commit f859617

Browse files
use generics
1 parent 0ad9d0a commit f859617

File tree

6 files changed

+18
-33
lines changed

6 files changed

+18
-33
lines changed

packages/celery-library/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def celery_settings(
7373

7474
@pytest.fixture
7575
def app_server() -> BaseAppServer:
76-
return FakeAppServer()
76+
return FakeAppServer(app=None)
7777

7878

7979
@pytest.fixture(scope="session")

packages/service-library/src/servicelib/fastapi/queued_tasks/app_server.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,15 @@
1010
_STARTUP_TIMEOUT: Final[float] = timedelta(minutes=1).total_seconds()
1111

1212

13-
class FastAPIAppServer(BaseAppServer):
13+
class FastAPIAppServer(BaseAppServer[FastAPI]):
1414
def __init__(self, app: FastAPI):
15-
super().__init__()
16-
self._app = app
15+
super().__init__(app)
1716
self._lifespan_manager = LifespanManager(
18-
self.fastapi_app,
17+
app,
1918
startup_timeout=_STARTUP_TIMEOUT,
2019
shutdown_timeout=_SHUTDOWN_TIMEOUT,
2120
)
2221

23-
@property
24-
def fastapi_app(self) -> FastAPI:
25-
assert isinstance(self._app, FastAPI) # nosec
26-
return self._app
27-
2822
async def on_startup(self) -> None:
2923
await self._lifespan_manager.__aenter__()
3024

packages/service-library/src/servicelib/queued_tasks/app_server.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,23 @@
33
import threading
44
from abc import ABC, abstractmethod
55
from asyncio import AbstractEventLoop
6-
from contextlib import suppress
7-
from typing import TYPE_CHECKING, Final
6+
from typing import Final, Generic, TypeVar
87

98
from servicelib.queued_tasks.task_manager import TaskManager
109

11-
if TYPE_CHECKING:
12-
with suppress(ImportError):
13-
from fastapi import FastAPI
14-
with suppress(ImportError):
15-
from aiohttp.web import Application
16-
17-
1810
STARTUP_TIMEOUT: Final[float] = datetime.timedelta(minutes=1).total_seconds()
1911

12+
AppType = TypeVar("AppType")
2013

21-
class BaseAppServer(ABC):
22-
def __init__(self) -> None:
23-
self._shutdown_event: asyncio.Event | None = None
2414

25-
@property
26-
def fastapi_app(self) -> "FastAPI":
27-
raise NotImplementedError
15+
class BaseAppServer(ABC, Generic[AppType]):
16+
def __init__(self, app: AppType) -> None:
17+
self._app: AppType = app
18+
self._shutdown_event: asyncio.Event | None = None
2819

2920
@property
30-
def aiohttp_app(self) -> "Application":
31-
raise NotImplementedError
21+
def app(self) -> AppType:
22+
return self._app
3223

3324
@property
3425
def event_loop(self) -> AbstractEventLoop:

services/storage/src/simcore_service_storage/api/_worker_tasks/_files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from celery import Task # type: ignore[import-untyped]
3+
from celery import Task
44
from celery_library.utils import get_app_server
55
from models_library.api_schemas_storage.storage_schemas import (
66
FileUploadCompletionBody,
@@ -30,7 +30,7 @@ async def complete_upload_file(
3030
logging.INFO,
3131
msg=f"completing upload of file {user_id=}, {location_id=}, {file_id=}",
3232
):
33-
dsm = get_dsm_provider(get_app_server(task.app).fastapi_app).get(location_id)
33+
dsm = get_dsm_provider(get_app_server(task.app).app).get(location_id)
3434
# NOTE: completing a multipart upload on AWS can take up to several minutes
3535
# if it returns slow we return a 202 - Accepted, the client will have to check later
3636
# for completeness

services/storage/src/simcore_service_storage/api/_worker_tasks/_paths.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def compute_path_size(
2525
logging.INFO,
2626
msg=f"computing path size {user_id=}, {location_id=}, {path=}",
2727
):
28-
dsm = get_dsm_provider(get_app_server(task.app).fastapi_app).get(location_id)
28+
dsm = get_dsm_provider(get_app_server(task.app).app).get(location_id)
2929
return await dsm.compute_path_size(user_id, path=Path(path))
3030

3131

@@ -42,7 +42,7 @@ async def delete_paths(
4242
logging.INFO,
4343
msg=f"delete {paths=} in {location_id=} for {user_id=}",
4444
):
45-
dsm = get_dsm_provider(get_app_server(task.app).fastapi_app).get(location_id)
45+
dsm = get_dsm_provider(get_app_server(task.app).app).get(location_id)
4646
files_ids: set[StorageFileID] = {
4747
TypeAdapter(StorageFileID).validate_python(f"{path}") for path in paths
4848
}

services/storage/src/simcore_service_storage/api/_worker_tasks/_simcore_s3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async def deep_copy_files_from_project(
4040
logging.INFO,
4141
msg=f"copying {body.source['uuid']} -> {body.destination['uuid']} with {task.request.id}",
4242
):
43-
dsm = get_dsm_provider(get_app_server(task.app).fastapi_app).get(
43+
dsm = get_dsm_provider(get_app_server(task.app).app).get(
4444
SimcoreS3DataManager.get_location_id()
4545
)
4646
assert isinstance(dsm, SimcoreS3DataManager) # nosec
@@ -75,7 +75,7 @@ async def export_data(
7575
logging.INFO,
7676
f"'{task_id}' export data (for {user_id=}) fom selection: {paths_to_export}",
7777
):
78-
dsm = get_dsm_provider(get_app_server(task.app).fastapi_app).get(
78+
dsm = get_dsm_provider(get_app_server(task.app).app).get(
7979
SimcoreS3DataManager.get_location_id()
8080
)
8181
assert isinstance(dsm, SimcoreS3DataManager) # nosec

0 commit comments

Comments
 (0)