11import logging
2- from typing import Any , Awaitable , Callable
2+ from typing import Any , Awaitable , Callable , List
33
44from aiogram import Bot , Dispatcher
55from taskiq import AsyncBroker , TaskiqEvents , TaskiqState
1212logger = 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