1- from typing import Any
1+ from typing import Annotated , Any
22
33from aiohttp import web
4- from pydantic import BaseModel
4+ from models_library .rest_base import RequestParameters
5+ from pydantic import BaseModel , Field
56
67from ...aiohttp import status
78from ...long_running_tasks import lrt_api
89from ...long_running_tasks .models import TaskGet , TaskId
9- from ..requests_validation import parse_request_path_parameters_as
10+ from ..requests_validation import (
11+ parse_request_path_parameters_as ,
12+ parse_request_query_parameters_as ,
13+ )
1014from ..rest_responses import create_data_response
1115from ._manager import get_long_running_manager
1216
@@ -26,10 +30,11 @@ async def list_tasks(request: web.Request) -> web.Response:
2630 task_id = t .task_id ,
2731 status_href = f"{ request .app .router ['get_task_status' ].url_for (task_id = t .task_id )} " ,
2832 result_href = f"{ request .app .router ['get_task_result' ].url_for (task_id = t .task_id )} " ,
29- abort_href = f"{ request .app .router ['cancel_and_delete_task ' ].url_for (task_id = t .task_id )} " ,
33+ abort_href = f"{ request .app .router ['remove_task ' ].url_for (task_id = t .task_id )} " ,
3034 )
3135 for t in await lrt_api .list_tasks (
32- long_running_manager .tasks_manager ,
36+ long_running_manager .rpc_client ,
37+ long_running_manager .lrt_namespace ,
3338 long_running_manager .get_task_context (request ),
3439 )
3540 ]
@@ -42,7 +47,8 @@ async def get_task_status(request: web.Request) -> web.Response:
4247 long_running_manager = get_long_running_manager (request .app )
4348
4449 task_status = await lrt_api .get_task_status (
45- long_running_manager .tasks_manager ,
50+ long_running_manager .rpc_client ,
51+ long_running_manager .lrt_namespace ,
4652 long_running_manager .get_task_context (request ),
4753 path_params .task_id ,
4854 )
@@ -56,20 +62,36 @@ async def get_task_result(request: web.Request) -> web.Response | Any:
5662
5763 # NOTE: this might raise an exception that will be catched by the _error_handlers
5864 return await lrt_api .get_task_result (
59- long_running_manager .tasks_manager ,
65+ long_running_manager .rpc_client ,
66+ long_running_manager .lrt_namespace ,
6067 long_running_manager .get_task_context (request ),
6168 path_params .task_id ,
6269 )
6370
6471
65- @routes .delete ("/{task_id}" , name = "cancel_and_delete_task" )
66- async def cancel_and_delete_task (request : web .Request ) -> web .Response :
72+ class _RemoveTaskQueryParams (RequestParameters ):
73+ wait_for_removal : Annotated [
74+ bool ,
75+ Field (
76+ description = (
77+ "when True waits for the task to be removed "
78+ "completly instead of returning immediately"
79+ )
80+ ),
81+ ] = True
82+
83+
84+ @routes .delete ("/{task_id}" , name = "remove_task" )
85+ async def remove_task (request : web .Request ) -> web .Response :
6786 path_params = parse_request_path_parameters_as (_PathParam , request )
87+ query_params = parse_request_query_parameters_as (_RemoveTaskQueryParams , request )
6888 long_running_manager = get_long_running_manager (request .app )
6989
7090 await lrt_api .remove_task (
71- long_running_manager .tasks_manager ,
91+ long_running_manager .rpc_client ,
92+ long_running_manager .lrt_namespace ,
7293 long_running_manager .get_task_context (request ),
7394 path_params .task_id ,
95+ wait_for_removal = query_params .wait_for_removal ,
7496 )
7597 return web .json_response (status = status .HTTP_204_NO_CONTENT )
0 commit comments