Skip to content

Commit e0e3a06

Browse files
committed
Merge branch 'release/0.1.2'
2 parents 2e60447 + f26789c commit e0e3a06

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: Testing package
22

3-
on: push
3+
on:
4+
- push
5+
- pull_request
46

57
jobs:
68
lint:

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ async def app_shutdown():
7777
taskiq_fastapi.init(broker, "test_script:app")
7878

7979

80-
# We use TaskiqDepends here, becuase if we use FastAPIDepends fastapi
81-
# initilization will fail.
80+
# We use TaskiqDepends here, because if we use FastAPIDepends fastapi
81+
# initialization will fail.
8282
def get_redis_pool(request: Request = TaskiqDepends()) -> ConnectionPool:
8383
return request.app.state.redis_pool
8484

@@ -120,3 +120,19 @@ async def getval_endpoint(
120120
return await redis.get(key)
121121

122122
```
123+
124+
## Manually update dependency context
125+
126+
When using `InMemoryBroker` it may be required to update the dependency context manually. This may also be useful when setting up tests.
127+
128+
```py
129+
import taskiq_fastapi
130+
from taskiq import InMemoryBroker
131+
132+
broker = InMemoryBroker()
133+
134+
app = FastAPI()
135+
136+
taskiq_fastapi.init(broker, "test_script:app")
137+
taskiq_fastapi.populate_dependency_context(broker, app)
138+
```

pyproject.toml

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

taskiq_fastapi/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""FastAPI integration for Taskiq project."""
2-
from taskiq_fastapi.initializator import init
2+
from taskiq_fastapi.initializator import init, populate_dependency_context
33

4-
__all__ = ["init"]
4+
__all__ = ["init", "populate_dependency_context"]

taskiq_fastapi/initializator.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,8 @@ def init(broker: AsyncBroker, app_path: str) -> None:
6767

6868
if not isinstance(app, FastAPI):
6969
raise ValueError(f"'{app_path}' is not a FastAPI application.")
70-
scope = {"app": app, "type": "http"}
7170

72-
broker.add_dependency_context(
73-
{
74-
Request: Request(scope=scope),
75-
HTTPConnection: HTTPConnection(scope=scope),
76-
},
77-
)
71+
populate_dependency_context(broker, app)
7872

7973
broker.add_event_handler(
8074
TaskiqEvents.WORKER_STARTUP,
@@ -85,3 +79,25 @@ def init(broker: AsyncBroker, app_path: str) -> None:
8579
TaskiqEvents.WORKER_SHUTDOWN,
8680
shutdown_event_generator(app),
8781
)
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+
scope = {"app": app, "type": "http"}
97+
98+
broker.add_dependency_context(
99+
{
100+
Request: Request(scope=scope),
101+
HTTPConnection: HTTPConnection(scope=scope),
102+
},
103+
)

0 commit comments

Comments
 (0)