nats-io / jsm.go

JetStream Management Library for Golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consumer Delete returns NotFoundError

ianic opened this issue · comments

I made small util to tail messages in the stream. Just to monitor if the messages in the stream are expected.
That util creates new consumer and deletes it at the exit.

Most of the calls (not all) to consumer delete return "consumer not found" error. Although consumer was existing before the call to the delete and deleted after.
Not a big deal I just skip that type of the errors:

if err := cs.Delete(); err != nil {
	if jserr, ok := err.(api.ApiError); ok && jserr.NotFoundError() {
		return nil
	}
	return fmt.Errorf("delete consumer failed %w", err)
}

But it is unexpected behavior.

Can you show me how you make it?

FWIW if you give no durable name when creating it a ephemeral consumer gets created and willl be auto deleted a bit after you unsubscribe or disconnect.

Yes I'm using durable name, creating pull based consumer:

nc, err := nats.Connect(nats.DefaultURL)
if err != nil {
	return err
}
st, err := jsm.LoadStream(stream, jsm.WithConnection(nc))
if err != nil {
	return err
}
cs, err := st.NewConsumer(jsm.ConsumerConnection(jsm.WithContext(ctx)), jsm.StartWithLastReceived(), jsm.DurableName(name))
if err != nil {
	return err
}

Will try with push based...

Weird, this code seems to work every time for me which seems close enough to your code:

package main

import (
	"log"

	"github.com/nats-io/nats.go"

	"github.com/nats-io/jsm.go"
)

func panicIfErr(err error) {
	if err == nil {
		return
	}

	panic(err)
}

func main() {
	nc, err := nats.Connect("dev1:4222")
	panicIfErr(err)

	st, err := jsm.LoadStream("126", jsm.WithConnection(nc))
	panicIfErr(err)

	cs, err := st.NewConsumer(jsm.StartWithLastReceived(), jsm.DurableName("126c"))
	panicIfErr(err)

	msg, err := cs.NextMsg()
	panicIfErr(err)
	log.Printf("msg: %v", msg)

	err = cs.Delete()
	panicIfErr(err)

	log.Printf("deleted consumer")
}

small fiy: if you use a stream object like st to do NewConsumer() you dont need to again specify the connection stuff, its passed down for you, shouldnt affect this issue but just a hint about making using this a bit easier

Your are right, it is working properly.
I was calling Delete in a function which was called two times.
Feel so stupid. I'm so sorry for wasting your time R.I.Pienaar.

No problem! happy to help :)