Skip to content

Commit ef92bf6

Browse files
committed
Merge branch 'release/0.3.1'
2 parents 6f77df9 + 6b5aeee commit ef92bf6

3 files changed

Lines changed: 27 additions & 17 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
This repo adds integration between your aiogram application and taskiq.
88

9-
It runs all startup and shutdown events of your application and adds 2 dependencies,
9+
It runs all startup and shutdown events of your application and adds 3 dependencies,
1010
that you can use in your tasks.
1111

12-
1. Dispatcher - that were used along with executor;
13-
2. Bot - your bot instance.
12+
1. `Dispatcher` - that were used along with executor;
13+
2. `Bot` - your main bot instance;
14+
3. `List[Bot]` - all your bots.
1415

1516
## Usage
1617

@@ -36,6 +37,7 @@ from your_project.tkq import broker
3637

3738
dp = Dispatcher()
3839
bot = Bot(token="TOKEN")
40+
bot2 = Bot(token="TOKEN")
3941

4042

4143
@dp.startup()
@@ -80,6 +82,7 @@ taskiq_aiogram.init(
8082
broker,
8183
"your_project.__main__:dp",
8284
"your_project.__main__:bot",
85+
"your_project.__main__:bot2",
8386
)
8487
```
8588

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "taskiq-aiogram"
33
description = "Taskiq integration with Aiogram"
44
authors = ["Taskiq team <taskiq@no-reply.com>"]
55
maintainers = ["Taskiq team <taskiq@no-reply.com>"]
6-
version = "0.3.0"
6+
version = "0.3.1"
77
readme = "README.md"
88
license = "LICENSE"
99
classifiers = [

taskiq_aiogram/initializer.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Any, Awaitable, Callable
2+
from typing import Any, Awaitable, Callable, List
33

44
from aiogram import Bot, Dispatcher
55
from taskiq import AsyncBroker, TaskiqEvents, TaskiqState
@@ -12,18 +12,18 @@
1212
logger = logging.getLogger("taskiq.taskiq_aiogram")
1313

1414

15-
def startup_event_generator(
15+
def startup_event_generator( # noqa: C901
1616
broker: AsyncBroker,
1717
dispatcher_path: str,
18-
bot_path: str,
18+
*bots_paths: str,
1919
**kwargs: str,
2020
) -> Callable[[TaskiqState], Awaitable[None]]:
2121
"""
2222
Generate startup event for broker.
2323
2424
:param broker: current broker.
2525
:param dispatcher_path: python-path to the dispatcher object.
26-
:param bot_path: python-path to the bot.
26+
:param bots_paths: python-paths to bots objects.
2727
:param kwargs: random key-word arguments.
2828
2929
:returns: startup event handler.
@@ -36,29 +36,33 @@ async def startup(state: TaskiqState) -> None:
3636
dispatcher = import_object(dispatcher_path)
3737
if not isinstance(dispatcher, Dispatcher):
3838
raise ValueError("Dispatcher should be an instance of dispatcher.")
39-
bot = import_object(bot_path)
40-
if not isinstance(bot, Bot):
41-
raise ValueError("Bots should be instances of Bot class.")
39+
bots = []
40+
for bot_path in bots_paths:
41+
bot = import_object(bot_path)
42+
if not isinstance(bot, Bot):
43+
raise ValueError("Bots should be instances of Bot class.")
44+
bots.append(bot)
4245

4346
workflow_data = {
4447
"dispatcher": dispatcher,
45-
"bots": [bot],
48+
"bots": bots,
4649
**dispatcher.workflow_data,
4750
**kwargs,
4851
}
4952
if "bot" in workflow_data:
5053
workflow_data.pop("bot")
5154

52-
state[BOT_KEY] = bot
55+
state[BOT_KEY] = bots
5356
state[WORKFLOW_KEY] = workflow_data
5457
state[DISPATCHER_KEY] = dispatcher
5558

56-
await dispatcher.emit_startup(bot=bot, **workflow_data)
59+
await dispatcher.emit_startup(bot=bots[-1], **workflow_data)
5760

5861
broker.add_dependency_context(
5962
{
6063
Dispatcher: dispatcher,
61-
Bot: bot,
64+
Bot: bots[-1],
65+
List[Bot]: bots,
6266
},
6367
)
6468

@@ -84,12 +88,12 @@ def shutdown_event_generator(
8488
async def shutdown(state: TaskiqState) -> None:
8589
if not broker.is_worker_process:
8690
return
87-
bot: Bot = state[BOT_KEY]
91+
bots: List[Bot] = state[BOT_KEY]
8892
workflow_data: dict[str, Any] = state[WORKFLOW_KEY]
8993
dispatcher: Dispatcher = state[DISPATCHER_KEY]
9094

9195
try:
92-
await dispatcher.emit_shutdown(bot, **workflow_data)
96+
await dispatcher.emit_shutdown(bots[-1], **workflow_data)
9397
except Exception as exc:
9498
logger.warn(f"Error found while shutting down: {exc}")
9599

@@ -100,6 +104,7 @@ def init(
100104
broker: AsyncBroker,
101105
dispatcher: str,
102106
bot: str,
107+
*other_bots: str,
103108
**kwargs: Any,
104109
) -> None:
105110
"""
@@ -116,6 +121,7 @@ def init(
116121
:param broker: current broker.
117122
:param dispatcher: python-path to the dispatcher.
118123
:param bot: bot to use.
124+
:param other_bots: python-paths to other bots.
119125
:param kwargs: random key-word arguments for shutdown and startup events.
120126
"""
121127
broker.add_event_handler(
@@ -124,6 +130,7 @@ def init(
124130
broker,
125131
dispatcher,
126132
bot,
133+
*other_bots,
127134
**kwargs,
128135
),
129136
)

0 commit comments

Comments
 (0)