Skip to content

Commit b1aabeb

Browse files
committed
Added method to force custom task_id.
Signed-off-by: Pavel Kirilin <[email protected]>
1 parent e22e7d1 commit b1aabeb

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

taskiq/kicker.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from dataclasses import asdict, is_dataclass
22
from logging import getLogger
3-
from typing import (
3+
from typing import ( # noqa: WPS235
44
TYPE_CHECKING,
55
Any,
66
Coroutine,
77
Dict,
88
Generic,
9+
Optional,
910
TypeVar,
1011
Union,
1112
overload,
@@ -42,6 +43,7 @@ def __init__(
4243
self.task_name = task_name
4344
self.broker = broker
4445
self.labels = labels
46+
self.custom_task_id: Optional[str] = None
4547

4648
def with_labels(
4749
self,
@@ -56,6 +58,19 @@ def with_labels(
5658
self.labels.update(labels)
5759
return self
5860

61+
def with_task_id(self, task_id: str) -> "AsyncKicker[_FuncParams, _ReturnType]":
62+
"""
63+
Set task_id for current execution.
64+
65+
Please use this method with caution,
66+
because it may brake the logic of getting results.
67+
68+
:param task_id: custom task id.
69+
:return: kicker with custom task id.
70+
"""
71+
self.custom_task_id = task_id
72+
return self
73+
5974
def with_broker(
6075
self,
6176
broker: "AsyncBroker",
@@ -200,8 +215,12 @@ def _prepare_message( # noqa: WPS210
200215
for label, label_val in self.labels.items():
201216
labels[label] = str(label_val)
202217

218+
task_id = self.custom_task_id
219+
if task_id is None:
220+
task_id = self.broker.id_generator()
221+
203222
return TaskiqMessage(
204-
task_id=self.broker.id_generator(),
223+
task_id=task_id,
205224
task_name=self.task_name,
206225
labels=labels,
207226
args=formatted_args,

0 commit comments

Comments
 (0)