|
| 1 | +import logging |
1 | 2 | from pathlib import Path |
| 3 | +from typing import Annotated, Any |
2 | 4 | from uuid import UUID |
3 | 5 |
|
4 | | -import arrow |
5 | | -from fastapi import APIRouter |
| 6 | +from fastapi import APIRouter, Depends, FastAPI, Header, HTTPException, status |
| 7 | +from models_library.generics import Envelope |
6 | 8 | from models_library.projects import ProjectID |
7 | 9 | from models_library.services_types import ServiceKey, ServiceVersion |
8 | 10 | from models_library.users import UserID |
| 11 | +from servicelib.fastapi.dependencies import get_app |
| 12 | +from simcore_service_director import exceptions |
| 13 | + |
| 14 | +from ... import producer |
9 | 15 |
|
10 | 16 | router = APIRouter() |
11 | 17 |
|
| 18 | +log = logging.getLogger(__name__) |
| 19 | + |
12 | 20 |
|
13 | 21 | @router.get("/running_interactive_services") |
14 | | -async def list_running_services(user_id: UserID, project_id: ProjectID): |
15 | | - # NOTE: sync url in docker/healthcheck.py with this entrypoint! |
16 | | - return f"{__name__}.health_check@{arrow.utcnow().isoformat()}" |
| 22 | +async def list_running_services( |
| 23 | + the_app: Annotated[FastAPI, Depends(get_app)], |
| 24 | + user_id: UserID | None, |
| 25 | + project_id: ProjectID | None, |
| 26 | +): |
| 27 | + log.debug( |
| 28 | + "Client does list_running_services request user_id %s, project_id %s", |
| 29 | + user_id, |
| 30 | + project_id, |
| 31 | + ) |
| 32 | + try: |
| 33 | + services = await producer.get_services_details( |
| 34 | + the_app, |
| 35 | + f"{user_id}" if user_id else None, |
| 36 | + f"{project_id}" if project_id else None, |
| 37 | + ) |
| 38 | + return Envelope[list[dict[str, Any]]](data=services) |
| 39 | + except Exception as err: |
| 40 | + raise HTTPException( |
| 41 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"{err}" |
| 42 | + ) from err |
17 | 43 |
|
18 | 44 |
|
19 | | -@router.post("/running_interactive_services") |
| 45 | +@router.post( |
| 46 | + "/running_interactive_services", |
| 47 | + status_code=status.HTTP_201_CREATED, |
| 48 | +) |
20 | 49 | async def start_service( |
| 50 | + the_app: Annotated[FastAPI, Depends(get_app)], |
21 | 51 | user_id: UserID, |
22 | 52 | project_id: ProjectID, |
23 | 53 | service_key: ServiceKey, |
24 | 54 | service_uuid: UUID, |
25 | 55 | service_basepath: Path, |
26 | 56 | service_tag: ServiceVersion | None = None, |
27 | | -): |
28 | | - # NOTE: sync url in docker/healthcheck.py with this entrypoint! |
29 | | - return f"{__name__}.health_check@{arrow.utcnow().isoformat()}" |
| 57 | + x_simcore_user_agent: str = Header(...), |
| 58 | +) -> Envelope[dict[str, Any]]: |
| 59 | + log.debug( |
| 60 | + "Client does start_service with user_id %s, project_id %s, service %s:%s, service_uuid %s, service_basepath %s, request_simcore_user_agent %s", |
| 61 | + user_id, |
| 62 | + project_id, |
| 63 | + service_key, |
| 64 | + service_tag, |
| 65 | + service_uuid, |
| 66 | + service_basepath, |
| 67 | + x_simcore_user_agent, |
| 68 | + ) |
| 69 | + try: |
| 70 | + service = await producer.start_service( |
| 71 | + the_app, |
| 72 | + f"{user_id}", |
| 73 | + f"{project_id}", |
| 74 | + service_key, |
| 75 | + service_tag, |
| 76 | + f"{service_uuid}", |
| 77 | + f"{service_basepath}", |
| 78 | + x_simcore_user_agent, |
| 79 | + ) |
| 80 | + return Envelope[dict[str, Any]](data=service) |
| 81 | + except exceptions.ServiceStartTimeoutError as err: |
| 82 | + |
| 83 | + raise HTTPException( |
| 84 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"{err}" |
| 85 | + ) from err |
| 86 | + except exceptions.ServiceNotAvailableError as err: |
| 87 | + raise HTTPException( |
| 88 | + status_code=status.HTTP_404_NOT_FOUND, detail=f"{err}" |
| 89 | + ) from err |
| 90 | + except exceptions.ServiceUUIDInUseError as err: |
| 91 | + raise HTTPException( |
| 92 | + status_code=status.HTTP_409_CONFLICT, detail=f"{err}" |
| 93 | + ) from err |
| 94 | + except exceptions.RegistryConnectionError as err: |
| 95 | + raise HTTPException( |
| 96 | + status_code=status.HTTP_401_UNAUTHORIZED, detail=f"{err}" |
| 97 | + ) from err |
| 98 | + except Exception as err: |
| 99 | + raise HTTPException( |
| 100 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"{err}" |
| 101 | + ) from err |
30 | 102 |
|
31 | 103 |
|
32 | 104 | @router.get("/running_interactive_services/{service_uuid}") |
33 | | -async def get_running_service(service_uuid: UUID): |
34 | | - # NOTE: sync url in docker/healthcheck.py with this entrypoint! |
35 | | - return f"{__name__}.health_check@{arrow.utcnow().isoformat()}" |
| 105 | +async def get_running_service( |
| 106 | + the_app: Annotated[FastAPI, Depends(get_app)], |
| 107 | + service_uuid: UUID, |
| 108 | +) -> Envelope[dict[str, Any]]: |
| 109 | + log.debug( |
| 110 | + "Client does get_running_service with service_uuid %s", |
| 111 | + service_uuid, |
| 112 | + ) |
| 113 | + try: |
| 114 | + service = await producer.get_service_details(the_app, f"{service_uuid}") |
| 115 | + return Envelope[dict[str, Any]](data=service) |
| 116 | + except exceptions.ServiceUUIDNotFoundError as err: |
| 117 | + raise HTTPException( |
| 118 | + status_code=status.HTTP_404_NOT_FOUND, detail=f"{err}" |
| 119 | + ) from err |
| 120 | + except Exception as err: |
| 121 | + raise HTTPException( |
| 122 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"{err}" |
| 123 | + ) from err |
| 124 | + |
36 | 125 |
|
| 126 | +@router.delete( |
| 127 | + "/running_interactive_services/{service_uuid}", |
| 128 | + status_code=status.HTTP_204_NO_CONTENT, |
| 129 | +) |
| 130 | +async def stop_service( |
| 131 | + the_app: Annotated[FastAPI, Depends(get_app)], |
| 132 | + service_uuid: UUID, |
| 133 | + save_state: bool = True, |
| 134 | +): |
| 135 | + log.debug( |
| 136 | + "Client does stop_service with service_uuid %s", |
| 137 | + service_uuid, |
| 138 | + ) |
| 139 | + try: |
| 140 | + await producer.stop_service(the_app, f"{service_uuid}", save_state) |
37 | 141 |
|
38 | | -@router.delete("/running_interactive_services/{service_uuid}") |
39 | | -async def stop_service(service_uuid: UUID): |
40 | | - # NOTE: sync url in docker/healthcheck.py with this entrypoint! |
41 | | - return f"{__name__}.health_check@{arrow.utcnow().isoformat()}" |
| 142 | + except exceptions.ServiceUUIDNotFoundError as err: |
| 143 | + raise HTTPException( |
| 144 | + status_code=status.HTTP_404_NOT_FOUND, detail=f"{err}" |
| 145 | + ) from err |
| 146 | + except Exception as err: |
| 147 | + raise HTTPException( |
| 148 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"{err}" |
| 149 | + ) from err |
0 commit comments