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

Fix InMemoryBroker to use startup and shutdown middleware hooks #358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 16 additions & 6 deletions taskiq/brokers/inmemory_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,23 @@ def listen(self) -> AsyncGenerator[bytes, None]:

async def startup(self) -> None:
"""Runs startup events for client and worker side."""
for event in (TaskiqEvents.CLIENT_STARTUP, TaskiqEvents.WORKER_STARTUP):
for handler in self.event_handlers.get(event, []):
await maybe_awaitable(handler(self.state))
for handler in self.event_handlers.get(
TaskiqEvents.CLIENT_STARTUP
if self.is_worker_process
else TaskiqEvents.WORKER_STARTUP,
[],
):
Comment on lines +176 to +181
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be reversed?

Suggested change
for handler in self.event_handlers.get(
TaskiqEvents.CLIENT_STARTUP
if self.is_worker_process
else TaskiqEvents.WORKER_STARTUP,
[],
):
for handler in self.event_handlers.get(
TaskiqEvents.WORKER_STARTUP
if self.is_worker_process
else TaskiqEvents.CLIENT_STARTUP,
[],
):

await maybe_awaitable(handler(self.state))
await super().startup()

async def shutdown(self) -> None:
"""Runs shutdown events for client and worker side."""
for event in (TaskiqEvents.CLIENT_SHUTDOWN, TaskiqEvents.WORKER_SHUTDOWN):
for handler in self.event_handlers.get(event, []):
await maybe_awaitable(handler(self.state))
for handler in self.event_handlers.get(
TaskiqEvents.CLIENT_SHUTDOWN
if self.is_worker_process
else TaskiqEvents.WORKER_SHUTDOWN,
Comment on lines +188 to +190
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one as well.

Suggested change
TaskiqEvents.CLIENT_SHUTDOWN
if self.is_worker_process
else TaskiqEvents.WORKER_SHUTDOWN,
TaskiqEvents.WORKER_SHUTDOWN
if self.is_worker_process
else TaskiqEvents.CLIENT_SHUTDOWN,

[],
):
await maybe_awaitable(handler(self.state))
self.executor.shutdown()
await super().shutdown()