|
| 1 | +import asyncio |
| 2 | +from typing import Any, Dict, Iterable, List, Optional, Union |
| 3 | + |
| 4 | +import pydantic |
| 5 | +from taskiq import AsyncBroker, TaskiqError, TaskiqResult |
| 6 | +from taskiq.brokers.shared_broker import async_shared_broker |
| 7 | +from taskiq.context import Context, default_context |
| 8 | +from taskiq.decor import AsyncTaskiqDecoratedTask |
| 9 | +from taskiq.kicker import AsyncKicker |
| 10 | + |
| 11 | +from taskiq_pipelines.abc import AbstractStep |
| 12 | +from taskiq_pipelines.constants import CURRENT_STEP, PIPELINE_DATA |
| 13 | +from taskiq_pipelines.exceptions import AbortPipeline |
| 14 | + |
| 15 | + |
| 16 | +@async_shared_broker.task(task_name="taskiq_pipelines.shared.filter_tasks") |
| 17 | +async def filter_tasks( # noqa: C901, WPS210, WPS231 |
| 18 | + task_ids: List[str], |
| 19 | + parent_task_id: str, |
| 20 | + check_interval: float, |
| 21 | + context: Context = default_context, |
| 22 | + skip_errors: bool = False, |
| 23 | +) -> List[Any]: |
| 24 | + """ |
| 25 | + Filter resulted tasks. |
| 26 | +
|
| 27 | + It takes list of task ids, |
| 28 | + and parent task id. |
| 29 | +
|
| 30 | + After all subtasks are completed it gets |
| 31 | + result of a parent task, and |
| 32 | + if subtask's result of execution can be |
| 33 | + converted to True, the item from the original |
| 34 | + tasks is added to the resulting array. |
| 35 | +
|
| 36 | + :param task_ids: ordered list of task ids. |
| 37 | + :param parent_task_id: task id of a parent task. |
| 38 | + :param check_interval: how often checks are performed. |
| 39 | + :param context: context of the execution, defaults to default_context |
| 40 | + :param skip_errors: skip errors of subtasks, defaults to False |
| 41 | + :raises TaskiqError: if any subtask has returned error. |
| 42 | + :return: fitlered results. |
| 43 | + """ |
| 44 | + ordered_ids = task_ids[:] |
| 45 | + tasks_set = set(task_ids) |
| 46 | + while tasks_set: |
| 47 | + for task_id in task_ids: # noqa: WPS327 |
| 48 | + if await context.broker.result_backend.is_result_ready(task_id): |
| 49 | + try: |
| 50 | + tasks_set.remove(task_id) |
| 51 | + except LookupError: |
| 52 | + continue |
| 53 | + await asyncio.sleep(check_interval) |
| 54 | + |
| 55 | + results = await context.broker.result_backend.get_result(parent_task_id) |
| 56 | + filtered_results = [] |
| 57 | + for task_id, value in zip( # type: ignore # noqa: WPS352, WPS440 |
| 58 | + ordered_ids, |
| 59 | + results.return_value, |
| 60 | + ): |
| 61 | + result = await context.broker.result_backend.get_result(task_id) |
| 62 | + if result.is_err: |
| 63 | + if skip_errors: |
| 64 | + continue |
| 65 | + raise TaskiqError(f"Task {task_id} returned error. Filtering failed.") |
| 66 | + if result.return_value: |
| 67 | + filtered_results.append(value) |
| 68 | + return filtered_results |
| 69 | + |
| 70 | + |
| 71 | +class FilterStep(pydantic.BaseModel, AbstractStep, step_name="filter"): |
| 72 | + """Task to filter results.""" |
| 73 | + |
| 74 | + task_name: str |
| 75 | + labels: Dict[str, str] |
| 76 | + param_name: Optional[str] |
| 77 | + additional_kwargs: Dict[str, Any] |
| 78 | + skip_errors: bool |
| 79 | + check_interval: float |
| 80 | + |
| 81 | + def dumps(self) -> str: |
| 82 | + """ |
| 83 | + Dumps step as string. |
| 84 | +
|
| 85 | + :return: returns json. |
| 86 | + """ |
| 87 | + return self.json() |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def loads(cls, data: str) -> "FilterStep": |
| 91 | + """ |
| 92 | + Parses mapper step from string. |
| 93 | +
|
| 94 | + :param data: dumped data. |
| 95 | + :return: parsed step. |
| 96 | + """ |
| 97 | + return pydantic.parse_raw_as(FilterStep, data) |
| 98 | + |
| 99 | + async def act( |
| 100 | + self, |
| 101 | + broker: AsyncBroker, |
| 102 | + step_number: int, |
| 103 | + parent_task_id: str, |
| 104 | + task_id: str, |
| 105 | + pipe_data: str, |
| 106 | + result: "TaskiqResult[Any]", |
| 107 | + ) -> None: |
| 108 | + """ |
| 109 | + Run filter action. |
| 110 | +
|
| 111 | + This function creates many small filter steps, |
| 112 | + and then collects all results in one big filtered array, |
| 113 | + using 'filter_tasks' shared task. |
| 114 | +
|
| 115 | + :param broker: current broker. |
| 116 | + :param step_number: current step number. |
| 117 | + :param parent_task_id: task_id of the previous step. |
| 118 | + :param task_id: task_id to use in this step. |
| 119 | + :param pipe_data: serialized pipeline. |
| 120 | + :param result: result of the previous task. |
| 121 | + :raises AbortPipeline: if result is not iterable. |
| 122 | + """ |
| 123 | + if not isinstance(result.return_value, Iterable): |
| 124 | + raise AbortPipeline("Result of the previous task is not iterable.") |
| 125 | + sub_task_ids = [] |
| 126 | + for item in result.return_value: |
| 127 | + kicker: "AsyncKicker[Any, Any]" = AsyncKicker( |
| 128 | + task_name=self.task_name, |
| 129 | + broker=broker, |
| 130 | + labels=self.labels, |
| 131 | + ) |
| 132 | + if self.param_name: |
| 133 | + self.additional_kwargs[self.param_name] = item |
| 134 | + task = await kicker.kiq(**self.additional_kwargs) |
| 135 | + else: |
| 136 | + task = await kicker.kiq(item, **self.additional_kwargs) |
| 137 | + sub_task_ids.append(task.task_id) |
| 138 | + |
| 139 | + await filter_tasks.kicker().with_task_id(task_id).with_broker( |
| 140 | + broker, |
| 141 | + ).with_labels( |
| 142 | + **{CURRENT_STEP: step_number, PIPELINE_DATA: pipe_data}, # type: ignore |
| 143 | + ).kiq( |
| 144 | + sub_task_ids, |
| 145 | + parent_task_id, |
| 146 | + check_interval=self.check_interval, |
| 147 | + skip_errors=self.skip_errors, |
| 148 | + ) |
| 149 | + |
| 150 | + @classmethod |
| 151 | + def from_task( |
| 152 | + cls, |
| 153 | + task: Union[ |
| 154 | + AsyncKicker[Any, Any], |
| 155 | + AsyncTaskiqDecoratedTask[Any, Any], |
| 156 | + ], |
| 157 | + param_name: Optional[str], |
| 158 | + skip_errors: bool, |
| 159 | + check_interval: float, |
| 160 | + **additional_kwargs: Any, |
| 161 | + ) -> "FilterStep": |
| 162 | + """ |
| 163 | + Create new filter step from task. |
| 164 | +
|
| 165 | + :param task: task to execute. |
| 166 | + :param param_name: parameter name. |
| 167 | + :param skip_errors: don't fail collector |
| 168 | + task on errors. |
| 169 | + :param check_interval: how often tasks are checked. |
| 170 | + :param additional_kwargs: additional function's kwargs. |
| 171 | + :return: new mapper step. |
| 172 | + """ |
| 173 | + if isinstance(task, AsyncTaskiqDecoratedTask): |
| 174 | + kicker = task.kicker() |
| 175 | + else: |
| 176 | + kicker = task |
| 177 | + message = kicker._prepare_message() # noqa: WPS437 |
| 178 | + return FilterStep( |
| 179 | + task_name=message.task_name, |
| 180 | + labels=message.labels, |
| 181 | + param_name=param_name, |
| 182 | + additional_kwargs=additional_kwargs, |
| 183 | + skip_errors=skip_errors, |
| 184 | + check_interval=check_interval, |
| 185 | + ) |
0 commit comments