Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add populate_dependency_context to fix bug when using InMemoryBroker #2

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ async def app_shutdown():
taskiq_fastapi.init(broker, "test_script:app")


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

Expand Down Expand Up @@ -120,3 +120,19 @@ async def getval_endpoint(
return await redis.get(key)

```

## Manually update dependency context

When using `InMemoryBroker` it may be required to update the dependency context manually. This may also be useful when setting up tests.

```py
import taskiq_fastapi
from taskiq import InMemoryBroker

broker = InMemoryBroker()

app = FastAPI()

taskiq_fastapi.init(broker, "test_script:app")
taskiq_fastapi.populate_dependency_context(broker, app)
```
4 changes: 2 additions & 2 deletions taskiq_fastapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""FastAPI integration for Taskiq project."""
from taskiq_fastapi.initializator import init
from taskiq_fastapi.initializator import init, populate_dependency_context

__all__ = ["init"]
__all__ = ["init", "populate_dependency_context"]
31 changes: 24 additions & 7 deletions taskiq_fastapi/initializator.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,8 @@ def init(broker: AsyncBroker, app_path: str) -> None:

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

broker.add_dependency_context(
{
Request: Request(scope=scope),
HTTPConnection: HTTPConnection(scope=scope),
},
)
populate_dependency_context(broker, app)

broker.add_event_handler(
TaskiqEvents.WORKER_STARTUP,
Expand All @@ -85,3 +79,26 @@ def init(broker: AsyncBroker, app_path: str) -> None:
TaskiqEvents.WORKER_SHUTDOWN,
shutdown_event_generator(app),
)


def populate_dependency_context(broker: AsyncBroker, app: FastAPI) -> None:
"""
Populate dependency context.

This function injects the Request and HTTPConnection
into the broker's dependency context.

It may be need to be called manually if you are using InMemoryBroker.

:param broker: current broker to use.
:param app: current application.
"""

scope = {"app": app, "type": "http"}

broker.add_dependency_context(
{
Request: Request(scope=scope),
HTTPConnection: HTTPConnection(scope=scope),
},
)