-
I'm looking for a way to prevent handler execution based on some internal logic. Logic is fairly simple: make a simple comparison and decide whether handler should be called or we should immediately ack/terminate task. from faststream import BaseMiddleware
from faststream.types import DecodedMessage
from faststream.utils.context.repository import context
from faststream.exceptions import RejectMessage
from .utils import SessionManager
class MaybeCancelFeedTaskMiddleware(BaseMiddleware):
"""A middleware to cancel feed tasks if needed."""
async def on_consume(self, msg: DecodedMessage) -> DecodedMessage:
session_manger: SessionManager = context.get("session_manager")
if await session_manger.is_cancelled(msg["session_id"]):
raise RejectMessage()
return await super().on_consume(msg) Is this correct approach? |
Beta Was this translation helpful? Give feedback.
Answered by
Lancetnik
Apr 3, 2024
Replies: 1 comment
-
Sure, it is a way. One of. You can raise |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
pySilver
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sure, it is a way. One of. You can raise
SkipMessage
/AckMessage
/NackMessage
/RejectMessage
depends what you want