|
| 1 | +from types import CoroutineType |
| 2 | +from typing import Any, Coroutine, Generic, Tuple, Union, overload |
| 3 | + |
| 4 | +from taskiq import AsyncTaskiqDecoratedTask |
| 5 | +from taskiq.kicker import AsyncKicker |
| 6 | +from typing_extensions import ParamSpec, TypeVar, TypeVarTuple, Unpack |
| 7 | + |
| 8 | +from taskiq_pipelines.steps.group import GroupStep, GroupStepItem |
| 9 | + |
| 10 | +_Tups = TypeVarTuple("_Tups") |
| 11 | +_T = TypeVar("_T") |
| 12 | +_TVal = TypeVar("_TVal") |
| 13 | +_Params = ParamSpec("_Params") |
| 14 | + |
| 15 | + |
| 16 | +class Group(Generic[_T]): |
| 17 | + """ |
| 18 | + Group of tasks. |
| 19 | +
|
| 20 | + This class gathers multiple tasks together. |
| 21 | + They will run in parallel |
| 22 | +
|
| 23 | + :param skip_errors: If True, errors in one task will not affect others. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self: "Group[Tuple[()]]", |
| 28 | + skip_errors: bool = False, |
| 29 | + check_interval: float = 0.1, |
| 30 | + ) -> None: |
| 31 | + self.tasks: Tuple[GroupStepItem, ...] = () |
| 32 | + self.skip_errors = skip_errors |
| 33 | + self.check_interval = check_interval |
| 34 | + |
| 35 | + @overload |
| 36 | + def add( |
| 37 | + self: "Group[Tuple[Unpack[_Tups]]]", |
| 38 | + task: Union[ |
| 39 | + AsyncKicker[_Params, Coroutine[Any, Any, _TVal]], |
| 40 | + AsyncKicker[_Params, "CoroutineType[Any, Any, _TVal]"], |
| 41 | + AsyncTaskiqDecoratedTask[_Params, Coroutine[Any, Any, _TVal]], |
| 42 | + AsyncTaskiqDecoratedTask[_Params, "CoroutineType[Any, Any, _TVal]"], |
| 43 | + ], |
| 44 | + *args: _Params.args, |
| 45 | + **kwargs: _Params.kwargs, |
| 46 | + ) -> "Group[Tuple[Unpack[_Tups], _TVal]]": ... |
| 47 | + |
| 48 | + @overload |
| 49 | + def add( |
| 50 | + self: "Group[Tuple[Unpack[_Tups]]]", |
| 51 | + task: Union[ |
| 52 | + AsyncKicker[_Params, _TVal], |
| 53 | + AsyncTaskiqDecoratedTask[_Params, _TVal], |
| 54 | + ], |
| 55 | + *args: _Params.args, |
| 56 | + **kwargs: _Params.kwargs, |
| 57 | + ) -> "Group[Tuple[Unpack[_Tups], _TVal]]": ... |
| 58 | + |
| 59 | + def add( |
| 60 | + self: "Group[Any]", |
| 61 | + task: Union[AsyncKicker[_Params, Any], AsyncTaskiqDecoratedTask[_Params, Any]], |
| 62 | + *args: _Params.args, |
| 63 | + **kwargs: _Params.kwargs, |
| 64 | + ) -> "Any": |
| 65 | + """Add task to a group.""" |
| 66 | + kicker = task.kicker() if isinstance(task, AsyncTaskiqDecoratedTask) else task |
| 67 | + message = kicker._prepare_message(*args, **kwargs) |
| 68 | + self.tasks = ( |
| 69 | + *self.tasks, |
| 70 | + GroupStepItem( |
| 71 | + task_name=message.task_name, |
| 72 | + labels=message.labels, |
| 73 | + labels_types=message.labels_types, |
| 74 | + args=message.args, |
| 75 | + kwargs=message.kwargs, |
| 76 | + ), |
| 77 | + ) |
| 78 | + return self |
| 79 | + |
| 80 | + def to_step(self) -> GroupStep: |
| 81 | + """Convert group definition to a step.""" |
| 82 | + return GroupStep( |
| 83 | + tasks=list(self.tasks), |
| 84 | + skip_errors=self.skip_errors, |
| 85 | + check_interval=self.check_interval, |
| 86 | + ) |
0 commit comments