-
Notifications
You must be signed in to change notification settings - Fork 32
✨ Adding catalog client to dynamic-scheduler ⚠️
#7162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
GitHK
merged 9 commits into
ITISFoundation:master
from
GitHK:pr-osparc-add-catalog-client-to-dynamic-scheduler
Feb 12, 2025
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
554c1a7
added catalog client to dynamic-scheduler
ea15aea
remove print
e4ddd3b
remove comments
03e0eca
add existing env vars
c4906b8
Merge remote-tracking branch 'upstream/master' into pr-osparc-add-cat…
c26ef7d
using lifespan
78372bf
Merge remote-tracking branch 'upstream/master' into pr-osparc-add-cat…
d17020d
refactored tests
522ff8c
Merge branch 'master' into pr-osparc-add-catalog-client-to-dynamic-sc…
GitHK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...ices/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/catalog/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from ._public_client import CatalogPublicClient | ||
| from ._setup import lifespan_catalog | ||
|
|
||
| __all__: tuple[str, ...] = ( | ||
| "CatalogPublicClient", | ||
| "lifespan_catalog", | ||
| ) |
34 changes: 34 additions & 0 deletions
34
...ynamic-scheduler/src/simcore_service_dynamic_scheduler/services/catalog/_public_client.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from fastapi import FastAPI | ||
| from models_library.api_schemas_catalog.services_specifications import ( | ||
| ServiceSpecifications, | ||
| ) | ||
| from models_library.service_settings_labels import SimcoreServiceLabels | ||
| from models_library.services import ServiceKey, ServiceVersion | ||
| from models_library.users import UserID | ||
| from pydantic import TypeAdapter | ||
| from servicelib.fastapi.app_state import SingletonInAppStateMixin | ||
|
|
||
| from ._thin_client import CatalogThinClient | ||
|
|
||
|
|
||
| class CatalogPublicClient(SingletonInAppStateMixin): | ||
| app_state_name: str = "catalog_public_client" | ||
|
|
||
| def __init__(self, app: FastAPI) -> None: | ||
| self.app = app | ||
|
|
||
| async def get_services_labels( | ||
| self, service_key: ServiceKey, service_version: ServiceVersion | ||
| ) -> SimcoreServiceLabels: | ||
| response = await CatalogThinClient.get_from_app_state( | ||
| self.app | ||
| ).get_services_labels(service_key, service_version) | ||
| return TypeAdapter(SimcoreServiceLabels).validate_python(response.json()) | ||
|
|
||
| async def get_services_specifications( | ||
| self, user_id: UserID, service_key: ServiceKey, service_version: ServiceVersion | ||
| ) -> ServiceSpecifications: | ||
| response = await CatalogThinClient.get_from_app_state( | ||
| self.app | ||
| ).get_services_specifications(user_id, service_key, service_version) | ||
| return TypeAdapter(ServiceSpecifications).validate_python(response.json()) |
21 changes: 21 additions & 0 deletions
21
services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/catalog/_setup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from collections.abc import AsyncIterator | ||
|
|
||
| from fastapi import FastAPI | ||
| from fastapi_lifespan_manager import State | ||
|
|
||
| from ._public_client import CatalogPublicClient | ||
| from ._thin_client import CatalogThinClient | ||
|
|
||
|
|
||
| async def lifespan_catalog(app: FastAPI) -> AsyncIterator[State]: | ||
| thin_client = CatalogThinClient(app) | ||
| thin_client.set_to_app_state(app) | ||
| thin_client.attach_lifespan_to(app) | ||
|
|
||
| public_client = CatalogPublicClient(app) | ||
| public_client.set_to_app_state(app) | ||
|
|
||
| yield {} | ||
|
|
||
| CatalogPublicClient.pop_from_app_state(app) | ||
| CatalogThinClient.pop_from_app_state(app) |
57 changes: 57 additions & 0 deletions
57
.../dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/catalog/_thin_client.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import urllib.parse | ||
|
|
||
| from fastapi import FastAPI, status | ||
| from httpx import Response | ||
| from models_library.services import ServiceKey, ServiceVersion | ||
| from models_library.users import UserID | ||
| from servicelib.fastapi.app_state import SingletonInAppStateMixin | ||
| from servicelib.fastapi.http_client import AttachLifespanMixin | ||
| from servicelib.fastapi.http_client_thin import ( | ||
| BaseThinClient, | ||
| expect_status, | ||
| retry_on_errors, | ||
| ) | ||
| from yarl import URL | ||
|
|
||
| from ...core.settings import ApplicationSettings | ||
|
|
||
|
|
||
| class CatalogThinClient(SingletonInAppStateMixin, BaseThinClient, AttachLifespanMixin): | ||
| app_state_name: str = "catalog_thin_client" | ||
|
|
||
| def __init__(self, app: FastAPI) -> None: | ||
| settings: ApplicationSettings = app.state.settings | ||
| assert settings.CLIENT_REQUEST.HTTP_CLIENT_REQUEST_TOTAL_TIMEOUT # nosec | ||
|
|
||
| super().__init__( | ||
| total_retry_interval=int( | ||
| settings.CLIENT_REQUEST.HTTP_CLIENT_REQUEST_TOTAL_TIMEOUT | ||
| ), | ||
| extra_allowed_method_names={ | ||
| "attach_lifespan_to", | ||
| "get_from_app_state", | ||
| "pop_from_app_state", | ||
| "set_to_app_state", | ||
| }, | ||
| base_url=settings.DYNAMIC_SCHEDULER_CATALOG_SETTINGS.api_base_url, | ||
| tracing_settings=settings.DYNAMIC_SCHEDULER_TRACING, | ||
| ) | ||
|
|
||
| @retry_on_errors() | ||
| @expect_status(status.HTTP_200_OK) | ||
| async def get_services_labels( | ||
| self, service_key: ServiceKey, service_version: ServiceVersion | ||
| ) -> Response: | ||
| return await self.client.get( | ||
| f"/services/{urllib.parse.quote(service_key, safe='')}/{service_version}/labels" | ||
| ) | ||
|
|
||
| @retry_on_errors() | ||
| @expect_status(status.HTTP_200_OK) | ||
| async def get_services_specifications( | ||
| self, user_id: UserID, service_key: ServiceKey, service_version: ServiceVersion | ||
| ) -> Response: | ||
| request_url = URL( | ||
| f"/services/{urllib.parse.quote(service_key, safe='')}/{service_version}/specifications", | ||
| ).with_query(user_id=user_id) | ||
| return await self.client.get(f"{request_url}") |
123 changes: 123 additions & 0 deletions
123
services/dynamic-scheduler/tests/unit/test_services_catalog.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| # pylint:disable=redefined-outer-name | ||
| # pylint:disable=unused-argument | ||
|
|
||
|
|
||
| import urllib.parse | ||
| from collections.abc import Iterator | ||
|
|
||
| import pytest | ||
| import respx | ||
| from fastapi import FastAPI | ||
| from models_library.api_schemas_catalog.services_specifications import ( | ||
| ServiceSpecifications, | ||
| ) | ||
| from models_library.service_settings_labels import SimcoreServiceLabels | ||
| from models_library.services import ServiceKey, ServiceVersion | ||
| from models_library.users import UserID | ||
| from pydantic import TypeAdapter | ||
| from pytest_simcore.helpers.typing_env import EnvVarsDict | ||
| from simcore_service_dynamic_scheduler.services.catalog import CatalogPublicClient | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def app_environment( | ||
| disable_redis_setup: None, | ||
| disable_rabbitmq_setup: None, | ||
| disable_service_tracker_setup: None, | ||
| disable_deferred_manager_setup: None, | ||
| disable_notifier_setup: None, | ||
| disable_status_monitor_setup: None, | ||
| app_environment: EnvVarsDict, | ||
| ) -> EnvVarsDict: | ||
| return app_environment | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def simcore_service_labels() -> SimcoreServiceLabels: | ||
| return TypeAdapter(SimcoreServiceLabels).validate_python( | ||
| SimcoreServiceLabels.model_json_schema()["examples"][1] | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def service_specifications() -> ServiceSpecifications: | ||
| return TypeAdapter(ServiceSpecifications).validate_python({}) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def user_id() -> UserID: | ||
| return 1 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def service_version() -> ServiceVersion: | ||
| return "1.0.0" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def service_key() -> ServiceKey: | ||
| return "simcore/services/dynamic/test" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_catalog( | ||
| app: FastAPI, | ||
| user_id: UserID, | ||
| service_key: ServiceKey, | ||
| service_version: ServiceVersion, | ||
| simcore_service_labels: SimcoreServiceLabels, | ||
| service_specifications: ServiceSpecifications, | ||
| ) -> Iterator[None]: | ||
| with respx.mock( | ||
| base_url=app.state.settings.DYNAMIC_SCHEDULER_CATALOG_SETTINGS.api_base_url, | ||
| assert_all_called=False, | ||
| assert_all_mocked=True, # IMPORTANT: KEEP always True! | ||
| ) as respx_mock: | ||
| respx_mock.get( | ||
| f"/services/{urllib.parse.quote_plus(service_key)}/{service_version}/labels", | ||
| name="service labels", | ||
| ).respond( | ||
| status_code=200, | ||
| json=simcore_service_labels.model_dump(mode="json"), | ||
| ) | ||
|
|
||
| respx_mock.get( | ||
| f"/services/{urllib.parse.quote_plus(service_key)}/{service_version}/specifications?user_id={user_id}", | ||
| name="service specifications", | ||
| ).respond( | ||
| status_code=200, | ||
| json=service_specifications.model_dump(mode="json"), | ||
| ) | ||
|
|
||
| yield | ||
|
|
||
|
|
||
| async def test_get_services_labels( | ||
| mock_catalog: None, | ||
| app: FastAPI, | ||
| service_key: ServiceKey, | ||
| service_version: ServiceVersion, | ||
| simcore_service_labels: SimcoreServiceLabels, | ||
| ): | ||
| client = CatalogPublicClient.get_from_app_state(app) | ||
| result = await client.get_services_labels(service_key, service_version) | ||
| assert result.model_dump(mode="json") == simcore_service_labels.model_dump( | ||
| mode="json" | ||
| ) | ||
|
|
||
|
|
||
| async def test_get_services_specifications( | ||
| mock_catalog: None, | ||
| app: FastAPI, | ||
| user_id: UserID, | ||
| service_key: ServiceKey, | ||
| service_version: ServiceVersion, | ||
| service_specifications: ServiceSpecifications, | ||
| ): | ||
| client = CatalogPublicClient.get_from_app_state(app) | ||
| result = await client.get_services_specifications( | ||
| user_id, service_key, service_version | ||
| ) | ||
| assert result.model_dump(mode="json") == service_specifications.model_dump( | ||
| mode="json" | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.