@@ -67,14 +67,8 @@ def init(broker: AsyncBroker, app_path: str) -> None:
67
67
68
68
if not isinstance (app , FastAPI ):
69
69
raise ValueError (f"'{ app_path } ' is not a FastAPI application." )
70
- scope = {"app" : app , "type" : "http" }
71
70
72
- broker .add_dependency_context (
73
- {
74
- Request : Request (scope = scope ),
75
- HTTPConnection : HTTPConnection (scope = scope ),
76
- },
77
- )
71
+ populate_dependency_context (broker , app )
78
72
79
73
broker .add_event_handler (
80
74
TaskiqEvents .WORKER_STARTUP ,
@@ -85,3 +79,26 @@ def init(broker: AsyncBroker, app_path: str) -> None:
85
79
TaskiqEvents .WORKER_SHUTDOWN ,
86
80
shutdown_event_generator (app ),
87
81
)
82
+
83
+
84
+ def populate_dependency_context (broker : AsyncBroker , app : FastAPI ) -> None :
85
+ """
86
+ Populate dependency context.
87
+
88
+ This function injects the Request and HTTPConnection
89
+ into the broker's dependency context.
90
+
91
+ It may be need to be called manually if you are using InMemoryBroker.
92
+
93
+ :param broker: current broker to use.
94
+ :param app: current application.
95
+ """
96
+
97
+ scope = {"app" : app , "type" : "http" }
98
+
99
+ broker .add_dependency_context (
100
+ {
101
+ Request : Request (scope = scope ),
102
+ HTTPConnection : HTTPConnection (scope = scope ),
103
+ },
104
+ )
0 commit comments