-
Notifications
You must be signed in to change notification settings - Fork 32
✨ Expose long running task endpoints in the api server #8037
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
bisgaard-itis
merged 33 commits into
ITISFoundation:master
from
bisgaard-itis:114-expose-function-job-logs-via-api-server
Jul 7, 2025
Merged
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
55ee32e
add initial rpc endpoints for celery
bisgaard-itis 0db7aa2
follow-up
bisgaard-itis b26466d
cleanups
bisgaard-itis 10fbffd
start adding unit tests
bisgaard-itis 8d3b509
first test
bisgaard-itis b397f97
ensure operations match google api guideline
bisgaard-itis fa701a9
fix unit test of get status endpoint
bisgaard-itis ec6e408
cleanup
bisgaard-itis 5cd3085
add unit tests for additional endpoints
bisgaard-itis f93631d
inital implementation of rpc client to convert exceptions
bisgaard-itis b18792d
further improvements
bisgaard-itis bc7ce89
clean up error mapping
bisgaard-itis 9542bc2
cover exceptions in tests
bisgaard-itis 2ae921b
add status codes to openapi specs
bisgaard-itis 56f01ff
update openapi specs
bisgaard-itis 97345ab
@giancarloromeo absolute import -> relative import
bisgaard-itis c539146
Merge branch 'master' into 114-expose-function-job-logs-via-api-server
bisgaard-itis c5809c2
pylint
bisgaard-itis 3c0d2a3
fix indirect import
bisgaard-itis cdd7f2f
pylint
bisgaard-itis 9e947a4
Merge branch 'master' into 114-expose-function-job-logs-via-api-server
bisgaard-itis c607e35
@pcrespov add support_id to error model
bisgaard-itis 21d8b1a
add support_id to error model in api-server
bisgaard-itis 5c8c1cd
services/api-server version: 0.9.0 → 0.9.1
bisgaard-itis 77d3283
update openapi specs
bisgaard-itis 5222258
hide task endpoints from api
bisgaard-itis aaac3a2
update openapi specs
bisgaard-itis fa695f2
fix import
bisgaard-itis 8d9ba17
fix typecheck
bisgaard-itis 8ffe3fe
Merge branch 'master' into 114-expose-function-job-logs-via-api-server
bisgaard-itis 7d11165
@pcrespov remove default values
bisgaard-itis 088ad73
use BaseBackendError as type hint
bisgaard-itis fc20bab
fix typecheck
bisgaard-itis 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
88 changes: 88 additions & 0 deletions
88
packages/pytest-simcore/src/pytest_simcore/helpers/async_jobs_server.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,88 @@ | ||
| # pylint: disable=unused-argument | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
| from models_library.api_schemas_rpc_async_jobs.async_jobs import ( | ||
| AsyncJobGet, | ||
| AsyncJobId, | ||
| AsyncJobNameData, | ||
| AsyncJobResult, | ||
| AsyncJobStatus, | ||
| ) | ||
| from models_library.api_schemas_rpc_async_jobs.exceptions import BaseAsyncjobRpcError | ||
| from models_library.progress_bar import ProgressReport | ||
| from models_library.rabbitmq_basic_types import RPCNamespace | ||
| from pydantic import validate_call | ||
| from pytest_mock import MockType | ||
| from servicelib.rabbitmq._client_rpc import RabbitMQRPCClient | ||
|
|
||
|
|
||
| @dataclass | ||
| class AsyncJobSideEffects: | ||
| exception: BaseAsyncjobRpcError | None = None | ||
|
|
||
| @validate_call(config={"arbitrary_types_allowed": True}) | ||
| async def cancel( | ||
| self, | ||
| rabbitmq_rpc_client: RabbitMQRPCClient | MockType, | ||
| *, | ||
| rpc_namespace: RPCNamespace, | ||
| job_id: AsyncJobId, | ||
| job_id_data: AsyncJobNameData, | ||
| ) -> None: | ||
| if self.exception is not None: | ||
| raise self.exception | ||
| return None | ||
|
|
||
| @validate_call(config={"arbitrary_types_allowed": True}) | ||
| async def status( | ||
| self, | ||
| rabbitmq_rpc_client: RabbitMQRPCClient | MockType, | ||
| *, | ||
| rpc_namespace: RPCNamespace, | ||
| job_id: AsyncJobId, | ||
| job_id_data: AsyncJobNameData, | ||
| ) -> AsyncJobStatus: | ||
| if self.exception is not None: | ||
| raise self.exception | ||
|
|
||
| return AsyncJobStatus( | ||
| job_id=job_id, | ||
| progress=ProgressReport( | ||
| actual_value=50.0, | ||
| total=100.0, | ||
| attempt=1, | ||
| ), | ||
| done=False, | ||
| ) | ||
|
|
||
| @validate_call(config={"arbitrary_types_allowed": True}) | ||
| async def result( | ||
| self, | ||
| rabbitmq_rpc_client: RabbitMQRPCClient | MockType, | ||
| *, | ||
| rpc_namespace: RPCNamespace, | ||
| job_id: AsyncJobId, | ||
| job_id_data: AsyncJobNameData, | ||
| ) -> AsyncJobResult: | ||
| if self.exception is not None: | ||
| raise self.exception | ||
| return AsyncJobResult(result="Success") | ||
|
|
||
| @validate_call(config={"arbitrary_types_allowed": True}) | ||
| async def list_jobs( | ||
| self, | ||
| rabbitmq_rpc_client: RabbitMQRPCClient | MockType, | ||
| *, | ||
| rpc_namespace: RPCNamespace, | ||
| job_id_data: AsyncJobNameData, | ||
| filter_: str = "", | ||
| ) -> list[AsyncJobGet]: | ||
| if self.exception is not None: | ||
| raise self.exception | ||
| return [ | ||
| AsyncJobGet( | ||
| job_id=AsyncJobId("123e4567-e89b-12d3-a456-426614174000"), | ||
| job_name="Example Job", | ||
| ) | ||
| ] |
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| 0.9.0 | ||
| 0.9.1 |
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
13 changes: 13 additions & 0 deletions
13
services/api-server/src/simcore_service_api_server/api/dependencies/tasks.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,13 @@ | ||
| from typing import Annotated | ||
|
|
||
| from fastapi import Depends | ||
| from servicelib.rabbitmq._client_rpc import RabbitMQRPCClient | ||
|
|
||
| from ...services_rpc.async_jobs import AsyncJobClient | ||
| from .rabbitmq import get_rabbitmq_rpc_client | ||
|
|
||
|
|
||
| def get_async_jobs_client( | ||
| rabbitmq_rpc_client: Annotated[RabbitMQRPCClient, Depends(get_rabbitmq_rpc_client)], | ||
| ) -> AsyncJobClient: | ||
| return AsyncJobClient(_rabbitmq_rpc_client=rabbitmq_rpc_client) |
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
180 changes: 180 additions & 0 deletions
180
services/api-server/src/simcore_service_api_server/api/routes/tasks.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,180 @@ | ||
| import logging | ||
| from typing import Annotated, Any | ||
|
|
||
| from fastapi import APIRouter, Depends, FastAPI, status | ||
| from models_library.api_schemas_long_running_tasks.base import TaskProgress | ||
| from models_library.api_schemas_long_running_tasks.tasks import ( | ||
| TaskGet, | ||
| TaskResult, | ||
| TaskStatus, | ||
| ) | ||
| from models_library.api_schemas_rpc_async_jobs.async_jobs import ( | ||
| AsyncJobId, | ||
| AsyncJobNameData, | ||
| ) | ||
| from models_library.products import ProductName | ||
| from models_library.users import UserID | ||
| from servicelib.fastapi.dependencies import get_app | ||
|
|
||
| from ...models.schemas.base import ApiServerEnvelope | ||
| from ...models.schemas.errors import ErrorGet | ||
| from ...services_rpc.async_jobs import AsyncJobClient | ||
| from ..dependencies.authentication import get_current_user_id, get_product_name | ||
| from ..dependencies.tasks import get_async_jobs_client | ||
| from ._constants import ( | ||
| FMSG_CHANGELOG_NEW_IN_VERSION, | ||
| create_route_description, | ||
| ) | ||
|
|
||
| router = APIRouter() | ||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _get_job_id_data(user_id: UserID, product_name: ProductName) -> AsyncJobNameData: | ||
| return AsyncJobNameData(user_id=user_id, product_name=product_name) | ||
|
|
||
|
|
||
| _DEFAULT_TASK_STATUS_CODES: dict[int | str, dict[str, Any]] = { | ||
| status.HTTP_500_INTERNAL_SERVER_ERROR: { | ||
| "description": "Internal server error", | ||
| "model": ErrorGet, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| @router.get( | ||
| "", | ||
| response_model=ApiServerEnvelope[list[TaskGet]], | ||
| responses=_DEFAULT_TASK_STATUS_CODES, | ||
| status_code=status.HTTP_200_OK, | ||
| name="list_tasks", | ||
| description=create_route_description( | ||
| base="List all tasks", | ||
| changelog=[ | ||
| FMSG_CHANGELOG_NEW_IN_VERSION.format("0.10-rc1"), | ||
| ], | ||
| ), | ||
| include_in_schema=False, # TO BE RELEASED in 0.10-rc1 | ||
| ) | ||
| async def list_tasks( | ||
| app: Annotated[FastAPI, Depends(get_app)], | ||
| user_id: Annotated[UserID, Depends(get_current_user_id)], | ||
| product_name: Annotated[ProductName, Depends(get_product_name)], | ||
| async_jobs: Annotated[AsyncJobClient, Depends(get_async_jobs_client)], | ||
| ): | ||
| user_async_jobs = await async_jobs.list_jobs( | ||
| job_id_data=_get_job_id_data(user_id, product_name), | ||
| filter_="", | ||
| ) | ||
| app_router = app.router | ||
| data = [ | ||
| TaskGet( | ||
| task_id=f"{job.job_id}", | ||
| task_name=job.job_name, | ||
| status_href=app_router.url_path_for( | ||
| "get_task_status", task_id=f"{job.job_id}" | ||
| ), | ||
| abort_href=app_router.url_path_for("cancel_task", task_id=f"{job.job_id}"), | ||
| result_href=app_router.url_path_for( | ||
| "get_task_result", task_id=f"{job.job_id}" | ||
| ), | ||
| ) | ||
| for job in user_async_jobs | ||
| ] | ||
| return ApiServerEnvelope(data=data) | ||
|
|
||
|
|
||
| @router.get( | ||
| "/{task_id}", | ||
| response_model=TaskStatus, | ||
| name="get_task_status", | ||
| responses=_DEFAULT_TASK_STATUS_CODES, | ||
| status_code=status.HTTP_200_OK, | ||
| description=create_route_description( | ||
| base="Get task status", | ||
| changelog=[ | ||
| FMSG_CHANGELOG_NEW_IN_VERSION.format("0.10-rc1"), | ||
| ], | ||
| ), | ||
| include_in_schema=False, # TO BE RELEASED in 0.10-rc1 | ||
| ) | ||
| async def get_task_status( | ||
| task_id: AsyncJobId, | ||
| user_id: Annotated[UserID, Depends(get_current_user_id)], | ||
| product_name: Annotated[ProductName, Depends(get_product_name)], | ||
| async_jobs: Annotated[AsyncJobClient, Depends(get_async_jobs_client)], | ||
| ): | ||
| async_job_rpc_status = await async_jobs.status( | ||
| job_id=task_id, | ||
| job_id_data=_get_job_id_data(user_id, product_name), | ||
| ) | ||
| _task_id = f"{async_job_rpc_status.job_id}" | ||
| return TaskStatus( | ||
| task_progress=TaskProgress( | ||
| task_id=_task_id, percent=async_job_rpc_status.progress.percent_value | ||
| ), | ||
| done=async_job_rpc_status.done, | ||
| started=None, | ||
| ) | ||
|
|
||
|
|
||
| @router.post( | ||
| "/{task_id}:cancel", | ||
| status_code=status.HTTP_204_NO_CONTENT, | ||
| name="cancel_task", | ||
| responses=_DEFAULT_TASK_STATUS_CODES, | ||
| description=create_route_description( | ||
| base="Cancel task", | ||
| changelog=[ | ||
| FMSG_CHANGELOG_NEW_IN_VERSION.format("0.10-rc1"), | ||
| ], | ||
| ), | ||
| include_in_schema=False, # TO BE RELEASED in 0.10-rc1 | ||
| ) | ||
| async def cancel_task( | ||
| task_id: AsyncJobId, | ||
| user_id: Annotated[UserID, Depends(get_current_user_id)], | ||
| product_name: Annotated[ProductName, Depends(get_product_name)], | ||
| async_jobs: Annotated[AsyncJobClient, Depends(get_async_jobs_client)], | ||
| ): | ||
| await async_jobs.cancel( | ||
| job_id=task_id, | ||
| job_id_data=_get_job_id_data(user_id, product_name), | ||
| ) | ||
|
|
||
|
|
||
| @router.get( | ||
| "/{task_id}/result", | ||
| response_model=TaskResult, | ||
| name="get_task_result", | ||
| responses={ | ||
| status.HTTP_404_NOT_FOUND: { | ||
| "description": "Task result not found", | ||
| "model": ErrorGet, | ||
| }, | ||
| status.HTTP_409_CONFLICT: { | ||
| "description": "Task is cancelled", | ||
| "model": ErrorGet, | ||
| }, | ||
| **_DEFAULT_TASK_STATUS_CODES, | ||
| }, | ||
| status_code=status.HTTP_200_OK, | ||
| description=create_route_description( | ||
| base="Get task result", | ||
| changelog=[ | ||
| FMSG_CHANGELOG_NEW_IN_VERSION.format("0.10-rc1"), | ||
| ], | ||
| ), | ||
| include_in_schema=False, # TO BE RELEASED in 0.10-rc1 | ||
| ) | ||
| async def get_task_result( | ||
| task_id: AsyncJobId, | ||
| user_id: Annotated[UserID, Depends(get_current_user_id)], | ||
| product_name: Annotated[ProductName, Depends(get_product_name)], | ||
| async_jobs: Annotated[AsyncJobClient, Depends(get_async_jobs_client)], | ||
| ): | ||
| async_job_rpc_result = await async_jobs.result( | ||
| job_id=task_id, | ||
| job_id_data=_get_job_id_data(user_id, product_name), | ||
| ) | ||
| return TaskResult(result=async_job_rpc_result.result, error=None) | ||
24 changes: 22 additions & 2 deletions
24
...api-server/src/simcore_service_api_server/exceptions/handlers/_handlers_backend_errors.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 |
|---|---|---|
| @@ -1,12 +1,32 @@ | ||
| import logging | ||
|
|
||
| from common_library.error_codes import create_error_code | ||
| from servicelib.logging_errors import create_troubleshootting_log_kwargs | ||
| from servicelib.status_codes_utils import is_5xx_server_error | ||
| from starlette.requests import Request | ||
| from starlette.responses import JSONResponse | ||
|
|
||
| from ...exceptions.backend_errors import BaseBackEndError | ||
| from ._utils import create_error_json_response | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def backend_error_handler(request: Request, exc: Exception) -> JSONResponse: | ||
| assert request # nosec | ||
| assert isinstance(exc, BaseBackEndError) | ||
|
|
||
| return create_error_json_response(f"{exc}", status_code=exc.status_code) | ||
| user_error_msg = f"{exc}" | ||
bisgaard-itis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| support_id = None | ||
| if is_5xx_server_error(exc.status_code): | ||
| support_id = create_error_code(exc) | ||
| _logger.exception( | ||
| **create_troubleshootting_log_kwargs( | ||
| user_error_msg, | ||
| error=exc, | ||
| error_code=support_id, | ||
| tip="Unexpected error", | ||
| ) | ||
| ) | ||
| return create_error_json_response( | ||
| user_error_msg, status_code=exc.status_code, support_id=support_id | ||
| ) | ||
Oops, something went wrong.
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.