Open NonnRa opened 2 months ago
do you get any notifications on opts.notificationhandler?
var receivedMessages = consumer.FetchAsync<byte[]>(new NatsJSFetchOpts(){ Expires = TimeSpan.FromSeconds(10), MaxMsgs = 100, NotificationHandler = PrintNotifications });
await foreach (var msg in receivedMessages)
{
Console.WriteLine(msg);
}
Task PrintNotifications(INatsJSNotification ns, CancellationToken ct)
{
Console.WriteLine(ns.Name);
if(ns is NatsJSProtocolNotification protocolNotification)
{
Console.WriteLine($"{protocolNotification.Name}, {protocolNotification.HeaderCode}, {protocolNotification.HeaderMessageText}");
}
return Task.CompletedTask;
}
Start
Create connection
Create stream
Create consumer
Wait for consumer InactiveThreshold
Fetch on non existent consumer
Timeout
Adapted my example to print the notifications. The only notification i got is the Timeout
at the end.
Thank you for this report @NonnRa it's a good question. unfortunately server doesn't return any errors on requests in this case. you have to explicitly check for the consumer using consumer.Refresh()
:
await consumer.RefreshAsync(); // this will create an additional consumer info API call to JetStream server
var receivedMessages = consumer.FetchAsync<byte[]>(new NatsJSFetchOpts(){ Expires = TimeSpan.FromSeconds(10), MaxMsgs = 100, NotificationHandler = PrintNotifications });
await foreach (var msg in receivedMessages)
{
Console.WriteLine(msg);
}
... or check for the NatsJSTimeoutNotification
then refresh to avoid the additional consumer info call every time:
if (ns is NatsJSTimeoutNotification)
{
Console.WriteLine("Timeout");
try
{
await consumer.RefreshAsync(ct);
}
catch (NatsJSApiException e) when (e.Error.Code == 404)
{
Console.WriteLine("Consumer not found");
await myCancellationTokenSource.CancelAsync(); // somehow signal the fetch loop
}
catch (Exception e)
{
Console.WriteLine("Log other exceptions");
}
}
Thanks for the response. I will try to build a safty net around it with you snippets. A exception at that point would have helped us a lot to find an issue on our code, as it was not intended to have a delay between the consumer creation and the first fetch.
Observed behavior
When i create a pull consumer and the consumer is deleted on the server before i call
FetchAsync(..)
. I will never get any message from that consumer and i will never get an exception. So that i stuck in an endless loop callingFetchAsync
again and again.Expected behavior
I expected to get a Exception like described on the FetchAsync method.
NatsJSException — There is an error sending the message or this consumer object isn't valid anymore because it was deleted earlier
Server and client version
Client version: NATS.Net 2.3.3 Server verison: 2.10.20
Host environment
No response
Steps to reproduce
On the
FetchAsync(..)
i would expect to get an exception as the consumer is not valid anymore