|
4 | 4 | from .task import RegisteredTaskName, TaskContext, TasksManager, TrackedTask |
5 | 5 |
|
6 | 6 |
|
7 | | -def list_tasks( |
8 | | - tasks_manager: TasksManager, task_context: TaskContext | None |
9 | | -) -> list[TaskBase]: |
10 | | - tracked_tasks: list[TrackedTask] = tasks_manager.list_tasks( |
11 | | - with_task_context=task_context |
12 | | - ) |
13 | | - return [TaskBase(task_id=t.task_id) for t in tracked_tasks] |
14 | | - |
15 | | - |
16 | | -def get_task_status( |
17 | | - tasks_manager: TasksManager, task_context: TaskContext | None, task_id: TaskId |
18 | | -) -> TaskStatus: |
19 | | - """returns the status of a task""" |
20 | | - return tasks_manager.get_task_status( |
21 | | - task_id=task_id, with_task_context=task_context |
22 | | - ) |
23 | | - |
24 | | - |
25 | | -async def get_task_result( |
26 | | - tasks_manager: TasksManager, task_context: TaskContext | None, task_id: TaskId |
27 | | -) -> Any: |
28 | | - """retruns the result of a task, which is directly whatever the remove hanlder returned""" |
29 | | - try: |
30 | | - return tasks_manager.get_task_result( |
31 | | - task_id=task_id, with_task_context=task_context |
32 | | - ) |
33 | | - finally: |
34 | | - # the task is always removed even if an error occurs |
35 | | - await tasks_manager.remove_task( |
36 | | - task_id, with_task_context=task_context, reraise_errors=False |
37 | | - ) |
38 | | - |
39 | | - |
40 | | -async def remove_task( |
41 | | - tasks_manager: TasksManager, task_context: TaskContext | None, task_id: TaskId |
42 | | -) -> None: |
43 | | - """removes / cancels a task""" |
44 | | - await tasks_manager.remove_task(task_id, with_task_context=task_context) |
45 | | - |
46 | | - |
47 | 7 | async def start_task( |
48 | 8 | tasks_manager: TasksManager, |
49 | 9 | registered_task_name: RegisteredTaskName, |
@@ -87,3 +47,43 @@ async def start_task( |
87 | 47 | fire_and_forget=fire_and_forget, |
88 | 48 | **task_kwargs, |
89 | 49 | ) |
| 50 | + |
| 51 | + |
| 52 | +def list_tasks( |
| 53 | + tasks_manager: TasksManager, task_context: TaskContext | None |
| 54 | +) -> list[TaskBase]: |
| 55 | + tracked_tasks: list[TrackedTask] = tasks_manager.list_tasks( |
| 56 | + with_task_context=task_context |
| 57 | + ) |
| 58 | + return [TaskBase(task_id=t.task_id) for t in tracked_tasks] |
| 59 | + |
| 60 | + |
| 61 | +def get_task_status( |
| 62 | + tasks_manager: TasksManager, task_context: TaskContext | None, task_id: TaskId |
| 63 | +) -> TaskStatus: |
| 64 | + """returns the status of a task""" |
| 65 | + return tasks_manager.get_task_status( |
| 66 | + task_id=task_id, with_task_context=task_context |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +async def get_task_result( |
| 71 | + tasks_manager: TasksManager, task_context: TaskContext | None, task_id: TaskId |
| 72 | +) -> Any: |
| 73 | + """retruns the result of a task, which is directly whatever the remove hanlder returned""" |
| 74 | + try: |
| 75 | + return tasks_manager.get_task_result( |
| 76 | + task_id=task_id, with_task_context=task_context |
| 77 | + ) |
| 78 | + finally: |
| 79 | + # the task is always removed even if an error occurs |
| 80 | + await tasks_manager.remove_task( |
| 81 | + task_id, with_task_context=task_context, reraise_errors=False |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +async def remove_task( |
| 86 | + tasks_manager: TasksManager, task_context: TaskContext | None, task_id: TaskId |
| 87 | +) -> None: |
| 88 | + """removes / cancels a task""" |
| 89 | + await tasks_manager.remove_task(task_id, with_task_context=task_context) |
0 commit comments