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,26 +28,27 @@ 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
49- handler = RequestHandler (app ._make_handler (), loop = loop )
51+ handler = RequestHandler (local_app ._make_handler (), loop = loop )
5052 handler .transport = asyncio .Transport ()
5153 request = web .Request (
5254 RawRequestMessage (
@@ -76,19 +78,19 @@ async def startup(state: TaskiqState) -> None:
7678 match_dict = {},
7779 route = SystemRoute (web .HTTPBadRequest ()),
7880 )
79- request ._match_info ._apps = app ._subapps
80- request ._match_info ._current_app = app
81+ request ._match_info ._apps = local_app ._subapps
82+ request ._match_info ._current_app = local_app
8183
8284 broker .add_dependency_context (
8385 {
84- web .Application : app ,
86+ web .Application : local_app ,
8587 web .Request : request ,
8688 },
8789 )
8890
89- state .aiohttp_app = app
90- app .router ._resources = []
91- await app .startup ()
91+ state .aiohttp_app = local_app
92+ local_app .router ._resources = []
93+ await local_app .startup ()
9294
9395 return startup
9496
@@ -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