11import asyncio
22import inspect
3- from typing import Awaitable , Callable
3+ from typing import Any , Awaitable , Callable
44
55import yarl
66from aiohttp import web
1616def startup_event_generator (
1717 broker : AsyncBroker ,
1818 app_path : str ,
19+ app : Any ,
1920) -> Callable [[TaskiqState ], Awaitable [None ]]:
2021 """
2122 Creates an event to run on broker's startup.
@@ -27,24 +28,25 @@ def startup_event_generator(
2728 act the same as the real application.
2829
2930 :param broker: current broker.
30- :param app_path: string with a path to an application or a factory.
31+ :param app_path: path to the application.
32+ :param app: current application or a fractory.
3133
3234 :returns: a function that is called on startup.
3335 """
3436
3537 async def startup (state : TaskiqState ) -> None :
3638 loop = asyncio .get_event_loop ()
3739
38- app = import_object ( app_path )
40+ local_app = app
3941
40- if not isinstance (app , web .Application ):
41- app = app ()
42+ if not isinstance (local_app , web .Application ):
43+ local_app = local_app ()
4244
43- if inspect .iscoroutine (app ):
44- app = await app
45+ if inspect .iscoroutine (local_app ):
46+ local_app = await local_app
4547
46- if not isinstance (app , web .Application ):
47- raise ValueError (f"' { app_path } ' is not an AioHTTP application." )
48+ if not isinstance (local_app , web .Application ):
49+ raise ValueError (f"{ app_path } is not an AioHTTP application." )
4850
4951 handler = RequestHandler (app ._make_handler (), loop = loop )
5052 handler .transport = asyncio .Transport ()
@@ -121,9 +123,11 @@ def init(broker: AsyncBroker, app_path: str) -> None:
121123 if not broker .is_worker_process :
122124 return
123125
126+ app = import_object (app_path )
127+
124128 broker .add_event_handler (
125129 TaskiqEvents .WORKER_STARTUP ,
126- startup_event_generator (broker , app_path ),
130+ startup_event_generator (broker , app_path , app ),
127131 )
128132 broker .add_event_handler (
129133 TaskiqEvents .WORKER_SHUTDOWN ,
0 commit comments