diff --git a/packages/models-library/src/models_library/api_schemas_directorv2/dynamic_services.py b/packages/models-library/src/models_library/api_schemas_directorv2/dynamic_services.py index 151611271a4..d26acac0490 100644 --- a/packages/models-library/src/models_library/api_schemas_directorv2/dynamic_services.py +++ b/packages/models-library/src/models_library/api_schemas_directorv2/dynamic_services.py @@ -79,3 +79,5 @@ class DynamicServiceCreate(ServiceDetails): class GetProjectInactivityResponse(BaseModel): is_inactive: bool + + model_config = ConfigDict(json_schema_extra={"example": {"is_inactive": "false"}}) diff --git a/packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/dynamic_scheduler/services.py b/packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/dynamic_scheduler/services.py index f58500d771e..fb3276ae670 100644 --- a/packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/dynamic_scheduler/services.py +++ b/packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/dynamic_scheduler/services.py @@ -3,6 +3,7 @@ from models_library.api_schemas_directorv2.dynamic_services import ( DynamicServiceGet, + GetProjectInactivityResponse, RetrieveDataOutEnveloped, ) from models_library.api_schemas_dynamic_scheduler import DYNAMIC_SCHEDULER_RPC_NAMESPACE @@ -99,6 +100,24 @@ async def stop_dynamic_service( assert result is None # nosec +@log_decorator(_logger, level=logging.DEBUG) +async def get_project_inactivity( + rabbitmq_rpc_client: RabbitMQRPCClient, + *, + project_id: ProjectID, + max_inactivity_seconds: NonNegativeInt, +) -> GetProjectInactivityResponse: + result = await rabbitmq_rpc_client.request( + DYNAMIC_SCHEDULER_RPC_NAMESPACE, + _RPC_METHOD_NAME_ADAPTER.validate_python("get_project_inactivity"), + project_id=project_id, + max_inactivity_seconds=max_inactivity_seconds, + timeout_s=_RPC_DEFAULT_TIMEOUT_S, + ) + assert isinstance(result, GetProjectInactivityResponse) # nosec + return result + + @log_decorator(_logger, level=logging.DEBUG) async def restart_user_services( rabbitmq_rpc_client: RabbitMQRPCClient, diff --git a/services/director-v2/openapi.json b/services/director-v2/openapi.json index 51e5f191b27..c769aff191a 100644 --- a/services/director-v2/openapi.json +++ b/services/director-v2/openapi.json @@ -2058,7 +2058,10 @@ "required": [ "is_inactive" ], - "title": "GetProjectInactivityResponse" + "title": "GetProjectInactivityResponse", + "example": { + "is_inactive": "false" + } }, "HTTPValidationError": { "properties": { diff --git a/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/api/rpc/_services.py b/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/api/rpc/_services.py index a0bbed6b110..b90ed821bfa 100644 --- a/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/api/rpc/_services.py +++ b/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/api/rpc/_services.py @@ -1,6 +1,7 @@ from fastapi import FastAPI from models_library.api_schemas_directorv2.dynamic_services import ( DynamicServiceGet, + GetProjectInactivityResponse, RetrieveDataOutEnveloped, ) from models_library.api_schemas_dynamic_scheduler.dynamic_services import ( @@ -12,6 +13,7 @@ from models_library.projects_nodes_io import NodeID from models_library.services_types import ServicePortKey from models_library.users import UserID +from pydantic import NonNegativeInt from servicelib.rabbitmq import RPCRouter from servicelib.rabbitmq.rpc_interfaces.dynamic_scheduler.errors import ( ServiceWaitingForManualInterventionError, @@ -62,6 +64,15 @@ async def stop_dynamic_service( ) +@router.expose() +async def get_project_inactivity( + app: FastAPI, *, project_id: ProjectID, max_inactivity_seconds: NonNegativeInt +) -> GetProjectInactivityResponse: + return await scheduler_interface.get_project_inactivity( + app, project_id=project_id, max_inactivity_seconds=max_inactivity_seconds + ) + + @router.expose() async def restart_user_services(app: FastAPI, *, node_id: NodeID) -> None: await scheduler_interface.restart_user_services(app, node_id=node_id) diff --git a/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/director_v2/_public_client.py b/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/director_v2/_public_client.py index 2841eb5f467..cc1e8851684 100644 --- a/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/director_v2/_public_client.py +++ b/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/director_v2/_public_client.py @@ -4,6 +4,7 @@ from fastapi import FastAPI, status from models_library.api_schemas_directorv2.dynamic_services import ( DynamicServiceGet, + GetProjectInactivityResponse, RetrieveDataOutEnveloped, ) from models_library.api_schemas_dynamic_scheduler.dynamic_services import ( @@ -14,7 +15,7 @@ from models_library.projects_nodes_io import NodeID from models_library.services_types import ServicePortKey from models_library.users import UserID -from pydantic import TypeAdapter +from pydantic import NonNegativeInt, TypeAdapter from servicelib.fastapi.app_state import SingletonInAppStateMixin from servicelib.fastapi.http_client import AttachLifespanMixin, HasClientSetupInterface from servicelib.fastapi.http_client_thin import UnexpectedStatusError @@ -125,6 +126,16 @@ async def list_tracked_dynamic_services( ) return TypeAdapter(list[DynamicServiceGet]).validate_python(response.json()) + async def get_project_inactivity( + self, *, project_id: ProjectID, max_inactivity_seconds: NonNegativeInt + ) -> GetProjectInactivityResponse: + response = await self.thin_client.get_projects_inactivity( + project_id=project_id, max_inactivity_seconds=max_inactivity_seconds + ) + return TypeAdapter(GetProjectInactivityResponse).validate_python( + response.json() + ) + async def restart_user_services(self, *, node_id: NodeID) -> None: await self.thin_client.post_restart(node_id=node_id) diff --git a/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/director_v2/_thin_client.py b/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/director_v2/_thin_client.py index 1b86d604148..412e7377d19 100644 --- a/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/director_v2/_thin_client.py +++ b/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/director_v2/_thin_client.py @@ -13,6 +13,7 @@ from models_library.services_resources import ServiceResourcesDictHelpers from models_library.services_types import ServicePortKey from models_library.users import UserID +from pydantic import NonNegativeInt from servicelib.common_headers import ( X_DYNAMIC_SIDECAR_REQUEST_DNS, X_DYNAMIC_SIDECAR_REQUEST_SCHEME, @@ -143,6 +144,15 @@ async def get_dynamic_services( ) @retry_on_errors() + @expect_status(status.HTTP_200_OK) + async def get_projects_inactivity( + self, *, project_id: ProjectID, max_inactivity_seconds: NonNegativeInt + ) -> Response: + return await self.client.get( + f"/dynamic_services/projects/{project_id}/inactivity", + params={"max_inactivity_seconds": max_inactivity_seconds}, + ) + @expect_status(status.HTTP_204_NO_CONTENT) async def post_restart(self, *, node_id: NodeID) -> Response: return await self.client.post(f"/dynamic_services/{node_id}:restart") diff --git a/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/scheduler_interface.py b/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/scheduler_interface.py index 27e854ae6db..ff279fb75c9 100644 --- a/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/scheduler_interface.py +++ b/services/dynamic-scheduler/src/simcore_service_dynamic_scheduler/services/scheduler_interface.py @@ -1,6 +1,7 @@ from fastapi import FastAPI from models_library.api_schemas_directorv2.dynamic_services import ( DynamicServiceGet, + GetProjectInactivityResponse, RetrieveDataOutEnveloped, ) from models_library.api_schemas_dynamic_scheduler.dynamic_services import ( @@ -12,6 +13,7 @@ from models_library.projects_nodes_io import NodeID from models_library.services_types import ServicePortKey from models_library.users import UserID +from pydantic import NonNegativeInt from ..core.settings import ApplicationSettings from .director_v2 import DirectorV2Client @@ -79,6 +81,22 @@ async def stop_dynamic_service( await set_request_as_stopped(app, dynamic_service_stop) +async def get_project_inactivity( + app: FastAPI, *, project_id: ProjectID, max_inactivity_seconds: NonNegativeInt +) -> GetProjectInactivityResponse: + settings: ApplicationSettings = app.state.settings + if settings.DYNAMIC_SCHEDULER_USE_INTERNAL_SCHEDULER: + raise NotImplementedError + + director_v2_client = DirectorV2Client.get_from_app_state(app) + response: GetProjectInactivityResponse = ( + await director_v2_client.get_project_inactivity( + project_id=project_id, max_inactivity_seconds=max_inactivity_seconds + ) + ) + return response + + async def restart_user_services(app: FastAPI, *, node_id: NodeID) -> None: settings: ApplicationSettings = app.state.settings if settings.DYNAMIC_SCHEDULER_USE_INTERNAL_SCHEDULER: diff --git a/services/dynamic-scheduler/tests/unit/api_rpc/test_api_rpc__services.py b/services/dynamic-scheduler/tests/unit/api_rpc/test_api_rpc__services.py index 6455ca367f5..f3380bbb2f5 100644 --- a/services/dynamic-scheduler/tests/unit/api_rpc/test_api_rpc__services.py +++ b/services/dynamic-scheduler/tests/unit/api_rpc/test_api_rpc__services.py @@ -11,6 +11,7 @@ from fastapi.encoders import jsonable_encoder from models_library.api_schemas_directorv2.dynamic_services import ( DynamicServiceGet, + GetProjectInactivityResponse, RetrieveDataOutEnveloped, ) from models_library.api_schemas_dynamic_scheduler.dynamic_services import ( @@ -493,6 +494,40 @@ async def test_stop_dynamic_service_serializes_generic_errors( ) +@pytest.fixture +def inactivity_response() -> GetProjectInactivityResponse: + return TypeAdapter(GetProjectInactivityResponse).validate_python( + GetProjectInactivityResponse.model_json_schema()["example"] + ) + + +@pytest.fixture +def mock_director_v2_get_project_inactivity( + project_id: ProjectID, inactivity_response: GetProjectInactivityResponse +) -> Iterator[None]: + with respx.mock( + base_url="http://director-v2:8000/v2", + assert_all_called=False, + assert_all_mocked=True, # IMPORTANT: KEEP always True! + ) as mock: + mock.get(f"/dynamic_services/projects/{project_id}/inactivity").respond( + status.HTTP_200_OK, text=inactivity_response.model_dump_json() + ) + yield None + + +async def test_get_project_inactivity( + mock_director_v2_get_project_inactivity: None, + rpc_client: RabbitMQRPCClient, + project_id: ProjectID, + inactivity_response: GetProjectInactivityResponse, +): + result = await services.get_project_inactivity( + rpc_client, project_id=project_id, max_inactivity_seconds=5 + ) + assert result == inactivity_response + + @pytest.fixture def mock_director_v2_restart_user_services(node_id: NodeID) -> Iterator[None]: with respx.mock( diff --git a/services/web/server/src/simcore_service_webserver/api/v0/openapi.yaml b/services/web/server/src/simcore_service_webserver/api/v0/openapi.yaml index 27bdb777e6f..5e20d12a5ab 100644 --- a/services/web/server/src/simcore_service_webserver/api/v0/openapi.yaml +++ b/services/web/server/src/simcore_service_webserver/api/v0/openapi.yaml @@ -9952,6 +9952,8 @@ components: required: - is_inactive title: GetProjectInactivityResponse + example: + is_inactive: 'false' GetWalletAutoRecharge: properties: enabled: diff --git a/services/web/server/src/simcore_service_webserver/director_v2/_core_dynamic_services.py b/services/web/server/src/simcore_service_webserver/director_v2/_core_dynamic_services.py deleted file mode 100644 index 5f9f3f3aad8..00000000000 --- a/services/web/server/src/simcore_service_webserver/director_v2/_core_dynamic_services.py +++ /dev/null @@ -1,35 +0,0 @@ -""" Operations on dynamic-services - -- This interface HIDES request/responses/exceptions to the director-v2 API service - -""" - -import logging - -from aiohttp import web -from models_library.projects import ProjectID -from pydantic import NonNegativeInt -from servicelib.logging_utils import log_decorator -from yarl import URL - -from ._core_base import DataType, request_director_v2 -from .settings import DirectorV2Settings, get_plugin_settings - -_log = logging.getLogger(__name__) - - -@log_decorator(logger=_log) -async def get_project_inactivity( - app: web.Application, - project_id: ProjectID, - max_inactivity_seconds: NonNegativeInt, -) -> DataType: - settings: DirectorV2Settings = get_plugin_settings(app) - backend_url = ( - URL(settings.base_url) / f"dynamic_services/projects/{project_id}/inactivity" - ).update_query(max_inactivity_seconds=max_inactivity_seconds) - result = await request_director_v2( - app, "GET", backend_url, expected_status=web.HTTPOk - ) - assert isinstance(result, dict) # nosec - return result diff --git a/services/web/server/src/simcore_service_webserver/director_v2/api.py b/services/web/server/src/simcore_service_webserver/director_v2/api.py index 9c070e7cae9..2ecbb1446fd 100644 --- a/services/web/server/src/simcore_service_webserver/director_v2/api.py +++ b/services/web/server/src/simcore_service_webserver/director_v2/api.py @@ -16,7 +16,6 @@ is_pipeline_running, stop_pipeline, ) -from ._core_dynamic_services import get_project_inactivity from ._core_utils import is_healthy from .exceptions import DirectorServiceError @@ -28,7 +27,6 @@ "DirectorServiceError", "get_batch_tasks_outputs", "get_computation_task", - "get_project_inactivity", "get_project_run_policy", "is_healthy", "is_pipeline_running", diff --git a/services/web/server/src/simcore_service_webserver/dynamic_scheduler/api.py b/services/web/server/src/simcore_service_webserver/dynamic_scheduler/api.py index bdfd22c3c1d..5773052010b 100644 --- a/services/web/server/src/simcore_service_webserver/dynamic_scheduler/api.py +++ b/services/web/server/src/simcore_service_webserver/dynamic_scheduler/api.py @@ -5,6 +5,7 @@ from aiohttp import web from models_library.api_schemas_directorv2.dynamic_services import ( DynamicServiceGet, + GetProjectInactivityResponse, RetrieveDataOutEnveloped, ) from models_library.api_schemas_dynamic_scheduler.dynamic_services import ( @@ -23,6 +24,7 @@ from models_library.rabbitmq_messages import ProgressRabbitMessageProject, ProgressType from models_library.services import ServicePortKey from models_library.users import UserID +from pydantic import NonNegativeInt from pydantic.types import PositiveInt from servicelib.progress_bar import ProgressBarData from servicelib.rabbitmq import RabbitMQClient, RPCServerError @@ -154,6 +156,19 @@ async def stop_dynamic_services_in_project( await logged_gather(*services_to_stop) +async def get_project_inactivity( + app: web.Application, + *, + project_id: ProjectID, + max_inactivity_seconds: NonNegativeInt, +) -> GetProjectInactivityResponse: + return await services.get_project_inactivity( + get_rabbitmq_rpc_client(app), + project_id=project_id, + max_inactivity_seconds=max_inactivity_seconds, + ) + + async def restart_user_services(app: web.Application, *, node_id: NodeID) -> None: """Restarts the user service(s) started by the the node_uuid's sidecar diff --git a/services/web/server/src/simcore_service_webserver/projects/projects_api.py b/services/web/server/src/simcore_service_webserver/projects/projects_api.py index fc41203434a..3edd4c50e39 100644 --- a/services/web/server/src/simcore_service_webserver/projects/projects_api.py +++ b/services/web/server/src/simcore_service_webserver/projects/projects_api.py @@ -1895,13 +1895,12 @@ async def get_project_inactivity( app: web.Application, project_id: ProjectID ) -> GetProjectInactivityResponse: project_settings: ProjectsSettings = get_plugin_settings(app) - project_inactivity = await director_v2_api.get_project_inactivity( + return await dynamic_scheduler_api.get_project_inactivity( app, - project_id, + project_id=project_id, # NOTE: project is considered inactive if all services exposing an /inactivity # endpoint were inactive since at least PROJECTS_INACTIVITY_INTERVAL max_inactivity_seconds=int( project_settings.PROJECTS_INACTIVITY_INTERVAL.total_seconds() ), ) - return GetProjectInactivityResponse.model_validate(project_inactivity)