-
Notifications
You must be signed in to change notification settings - Fork 160
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
Bug: NATS subscriber retry seems not working #1888
Comments
@Atanusaha143 thank you for the report! |
You can take a look at draft - #1869 |
@Lancetnik Thank you for the prompt reply and the heads-up. Are there alternative methods to implement a retry mechanism using |
With Nast Core it can be implemented via re-publish only. |
@Lancetnik The code provided in the description raises a |
It works only for Nats JS. Nats core has no "requeue" functional. You should setup a stream |
# publisher
from nats.aio.client import Client as NATS
from nats.js import JetStreamContext
from nats.js.api import StreamConfig
class TestPublisher:
def __init__(self):
self._conn = None
@staticmethod
async def _get_jetstream_conn() -> JetStreamContext:
nats: NATS = NATS()
await nats.connect("nats://localhost:4222")
return nats.jetstream()
@staticmethod
async def _add_stream(conn: JetStreamContext, subjects: list[str], name: str = "test_stream") -> JetStreamContext:
try:
await conn.add_stream(StreamConfig(name=name, subjects=subjects))
print("Stream created successfully.")
return conn
except Exception as e:
print(f"Failed to create stream: {str(e)}")
async def _setup_publisher(self, stream_name: str, subjects: list[str]) -> None:
self._conn = await self._get_jetstream_conn()
self._conn = await self._add_stream(conn=self._conn, subjects=subjects, name=stream_name)
async def publish(self, stream_name: str, subjects: list[str], data: bytes) -> None:
try:
if self._conn is None:
await self._setup_publisher(stream_name=stream_name, subjects=subjects)
for subject in subjects:
print(f"Published into Stream: {stream_name}")
ack = await self._conn.publish(subject=subject, payload=data)
print(f'Ack: stream={ack.stream}, sequence={ack.seq}')
except Exception as e:
print(f"Failed to publish to '{subjects}': {str(e)}") # subscriber
from faststream import FastStream
from faststream.nats import NatsBroker
broker = NatsBroker("nats://localhost:4222")
app = FastStream(broker)
@broker.subscriber(subject="test_subject", retry=True)
async def handle_test(data: str):
print(data)
a = 4
b = 0
c = a / b I intentionally performed the division operation to test whether the await test_publisher.publish(stream_name="test_stream", subjects=["test_subject"], data=b"started testing") @Lancetnik When calling the test_publisher's |
@Atanusaha143 you should setup stream in subscriber, not in publisher to consume messages from Jetstream |
As a solution, pull consumer is required, and a pull consumer must have a stream |
Describe the bug
According to NATS ack it should retry, whenever an error occurs. But not getting the expected behavior.
How to reproduce
And/Or steps to reproduce the behavior:
Expected behavior
Observed behavior
Even if the retry flag is set to True, the message is not retrying
The text was updated successfully, but these errors were encountered: