-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpipeliner.py
More file actions
401 lines (354 loc) · 12.2 KB
/
pipeliner.py
File metadata and controls
401 lines (354 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from types import CoroutineType
from typing import (
Any,
Coroutine,
Dict,
Generic,
List,
Literal,
Optional,
TypeVar,
Union,
overload,
)
import pydantic
from taskiq import AsyncBroker, AsyncTaskiqTask
from taskiq.decor import AsyncTaskiqDecoratedTask
from taskiq.kicker import AsyncKicker
from typing_extensions import ParamSpec
from taskiq_pipelines.constants import CURRENT_STEP, EMPTY_PARAM_NAME, PIPELINE_DATA
from taskiq_pipelines.steps import FilterStep, MapperStep, SequentialStep, parse_step
_ReturnType = TypeVar("_ReturnType")
_FuncParams = ParamSpec("_FuncParams")
_T2 = TypeVar("_T2")
class DumpedStep(pydantic.BaseModel):
"""Dumped state model."""
step_type: str
step_data: Dict[str, Any]
task_id: str
DumpedSteps = pydantic.RootModel[List[DumpedStep]]
class Pipeline(Generic[_FuncParams, _ReturnType]):
"""
Pipeline constructor.
This class helps you to build pipelines.
It creates all needed data and manages
task ids. Also it has helper methods,
to easily add new pipeline steps.
Of course it can be done manually,
but it's nice to have.
"""
def __init__(
self,
broker: AsyncBroker,
task: Optional[
Union[
AsyncKicker[_FuncParams, _ReturnType],
AsyncTaskiqDecoratedTask[_FuncParams, _ReturnType],
]
] = None,
) -> None:
self.broker = broker
self.steps: "List[DumpedStep]" = []
if task:
self.call_next(task)
@overload
def call_next(
self: "Pipeline[_FuncParams, _ReturnType]",
task: Union[
AsyncKicker[Any, Coroutine[Any, Any, _T2]],
AsyncKicker[Any, "CoroutineType[Any, Any, _T2]"],
AsyncTaskiqDecoratedTask[Any, Coroutine[Any, Any, _T2]],
AsyncTaskiqDecoratedTask[Any, "CoroutineType[Any, Any, _T2]"],
],
param_name: Union[Optional[str], Literal[-1]] = None,
**additional_kwargs: Any,
) -> "Pipeline[_FuncParams, _T2]": ...
@overload
def call_next(
self: "Pipeline[_FuncParams, _ReturnType]",
task: Union[
AsyncKicker[Any, _T2],
AsyncTaskiqDecoratedTask[Any, _T2],
],
param_name: Union[Optional[str], Literal[-1]] = None,
**additional_kwargs: Any,
) -> "Pipeline[_FuncParams, _T2]": ...
def call_next(
self,
task: Union[
AsyncKicker[Any, Any],
AsyncTaskiqDecoratedTask[Any, Any],
],
param_name: Union[Optional[str], Literal[-1]] = None,
**additional_kwargs: Any,
) -> Any:
"""
Adds sequential step.
This task will be executed right after
the previous and result of the previous task
will be passed as the first argument,
or it will be passed as key word argument,
if param_name is specified.
:param task: task to execute.
:param param_name: kwarg param name, defaults to None.
If set to -1 (EMPTY_PARAM_NAME), result is not passed.
:param additional_kwargs: additional kwargs to task.
:return: updated pipeline.
"""
self.steps.append(
DumpedStep(
step_type=SequentialStep._step_name,
step_data=SequentialStep.from_task(
task=task,
param_name=param_name,
**additional_kwargs,
).model_dump(),
task_id="",
),
)
return self
@overload
def call_after(
self: "Pipeline[_FuncParams, _ReturnType]",
task: Union[
AsyncKicker[Any, Coroutine[Any, Any, _T2]],
AsyncKicker[Any, "CoroutineType[Any, Any, _T2]"],
AsyncTaskiqDecoratedTask[Any, Coroutine[Any, Any, _T2]],
AsyncTaskiqDecoratedTask[Any, "CoroutineType[Any, Any, _T2]"],
],
**additional_kwargs: Any,
) -> "Pipeline[_FuncParams, _T2]": ...
@overload
def call_after(
self: "Pipeline[_FuncParams, _ReturnType]",
task: Union[
AsyncKicker[Any, _T2],
AsyncTaskiqDecoratedTask[Any, _T2],
],
**additional_kwargs: Any,
) -> "Pipeline[_FuncParams, _T2]": ...
def call_after(
self,
task: Union[
AsyncKicker[Any, Any],
AsyncTaskiqDecoratedTask[Any, Any],
],
**additional_kwargs: Any,
) -> Any:
"""
Adds sequential step.
This task will be executed right after
the previous and result of the previous task
is not passed to the next task.
This is equivalent to call_next(task, param_name=-1).
:param task: task to execute.
:param additional_kwargs: additional kwargs to task.
:return: updated pipeline.
"""
self.steps.append(
DumpedStep(
step_type=SequentialStep._step_name,
step_data=SequentialStep.from_task(
task=task,
param_name=EMPTY_PARAM_NAME,
**additional_kwargs,
).model_dump(),
task_id="",
),
)
return self
@overload
def map(
self: "Pipeline[_FuncParams, _ReturnType]",
task: Union[
AsyncKicker[Any, Coroutine[Any, Any, _T2]],
AsyncKicker[Any, "CoroutineType[Any, Any, _T2]"],
AsyncTaskiqDecoratedTask[Any, Coroutine[Any, Any, _T2]],
AsyncTaskiqDecoratedTask[Any, "CoroutineType[Any, Any, _T2]"],
],
param_name: Optional[str] = None,
skip_errors: bool = False,
check_interval: float = 0.5,
**additional_kwargs: Any,
) -> "Pipeline[_FuncParams, List[_T2]]": ...
@overload
def map(
self: "Pipeline[_FuncParams, _ReturnType]",
task: Union[
AsyncKicker[Any, _T2],
AsyncTaskiqDecoratedTask[Any, _T2],
],
param_name: Optional[str] = None,
skip_errors: bool = False,
check_interval: float = 0.5,
**additional_kwargs: Any,
) -> "Pipeline[_FuncParams, List[_T2]]": ...
def map(
self,
task: Union[
AsyncKicker[Any, Any],
AsyncTaskiqDecoratedTask[Any, Any],
],
param_name: Optional[str] = None,
skip_errors: bool = False,
check_interval: float = 0.5,
**additional_kwargs: Any,
) -> Any:
"""
Create new map task.
This task is used to map values of an
iterable.
It creates many subtasks and then collects
all results.
:param task: task to execute on each value of an iterable.
:param param_name: param name to use to inject the result of
the previous task. If none, result injected as the first argument.
:param skip_errors: skip error results, defaults to False.
:param check_interval: how often task completion is checked.
:param additional_kwargs: additional function's kwargs.
:return: pipeline.
"""
self.steps.append(
DumpedStep(
step_type=MapperStep._step_name,
step_data=MapperStep.from_task(
task=task,
param_name=param_name,
skip_errors=skip_errors,
check_interval=check_interval,
**additional_kwargs,
).model_dump(),
task_id="",
),
)
return self
@overload
def filter(
self: "Pipeline[_FuncParams, _ReturnType]",
task: Union[
AsyncKicker[Any, Coroutine[Any, Any, bool]],
AsyncKicker[Any, "CoroutineType[Any, Any, bool]"],
AsyncTaskiqDecoratedTask[Any, Coroutine[Any, Any, bool]],
AsyncTaskiqDecoratedTask[Any, "CoroutineType[Any, Any, bool]"],
],
param_name: Optional[str] = None,
skip_errors: bool = False,
check_interval: float = 0.5,
**additional_kwargs: Any,
) -> "Pipeline[_FuncParams, _ReturnType]": ...
@overload
def filter(
self: "Pipeline[_FuncParams, _ReturnType]",
task: Union[
AsyncKicker[Any, bool],
AsyncTaskiqDecoratedTask[Any, bool],
],
param_name: Optional[str] = None,
skip_errors: bool = False,
check_interval: float = 0.5,
**additional_kwargs: Any,
) -> "Pipeline[_FuncParams, _ReturnType]": ...
def filter(
self,
task: Union[
AsyncKicker[Any, Any],
AsyncTaskiqDecoratedTask[Any, Any],
],
param_name: Optional[str] = None,
skip_errors: bool = False,
check_interval: float = 0.5,
**additional_kwargs: Any,
) -> Any:
"""
Add filter step.
This step is executed on a list of items,
like map.
It runs many small subtasks for each item
in sequence and if task returns true,
the result is added to the final list.
:param task: task to execute on every item.
:param param_name: parameter name to pass item into, defaults to None
:param skip_errors: skip errors if any, defaults to False
:param check_interval: how often the result of all subtasks is checked,
defaults to 0.5
:param additional_kwargs: additional function's kwargs.
:return: pipeline with filtering step.
"""
self.steps.append(
DumpedStep(
step_type=FilterStep._step_name,
step_data=FilterStep.from_task(
task=task,
param_name=param_name,
skip_errors=skip_errors,
check_interval=check_interval,
**additional_kwargs,
).model_dump(),
task_id="",
),
)
return self
def dumpb(self) -> bytes:
"""
Dumps current pipeline as string.
:returns: serialized pipeline.
"""
return self.broker.serializer.dumpb(
DumpedSteps.model_validate(self.steps).model_dump(),
)
@classmethod
def loadb(cls, broker: AsyncBroker, pipe_data: bytes) -> "Pipeline[Any, Any]":
"""
Parses serialized pipeline.
This method requires broker,
to make pipeline kickable.
:param broker: broker to use when call kiq.
:param pipe_data: serialized pipeline data.
:return: new
"""
pipe: "Pipeline[Any, Any]" = Pipeline(broker)
data = broker.serializer.loadb(pipe_data)
pipe.steps = DumpedSteps.model_validate(data) # type: ignore[assignment]
return pipe
async def kiq(
self,
*args: _FuncParams.args,
**kwargs: _FuncParams.kwargs,
) -> AsyncTaskiqTask[_ReturnType]:
"""
Kiq pipeline.
This function is used as kiq in functions,
but it saves current pipeline as
custom label, so worker can understand,
what to do next.
:param args: first function's args.
:param kwargs: first function's kwargs.
:raises ValueError: if pipe is empty, or
first step isn't sequential.
:return: TaskqTask for the final function.
"""
if not self.steps:
raise ValueError("Pipeline is empty.")
self._update_task_ids()
step = self.steps[0]
parsed_step = parse_step(step.step_type, step.step_data)
if not isinstance(parsed_step, SequentialStep):
raise ValueError("First step must be sequential.")
kicker = (
AsyncKicker(
parsed_step.task_name,
broker=self.broker,
labels=parsed_step.labels,
)
.with_task_id(step.task_id)
.with_labels(
**{CURRENT_STEP: 0, PIPELINE_DATA: self.dumpb()}, # type: ignore
)
)
taskiq_task = await kicker.kiq(*args, **kwargs)
taskiq_task.task_id = self.steps[-1].task_id
return taskiq_task
def _update_task_ids(self) -> None:
"""Calculates task ids for each step in the pipeline."""
for step in self.steps:
step.task_id = self.broker.id_generator()