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

docs: add exit-on-signal and more comments to example consumer #307

Merged
merged 1 commit into from
Aug 15, 2020
Merged
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
16 changes: 15 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ Consumer

Consuming messages from NSQ can be done by creating an instance of a Consumer and supplying it a handler.

package main
import (
"log"
"os/signal"
"github.com/nsqio/go-nsq"
)

type myMessageHandler struct {}

// HandleMessage implements the Handler interface.
func (h *myMessageHandler) HandleMessage(m *nsq.Message) error {
if len(m.Body) == 0 {
// Returning nil will automatically send a FIN command to NSQ to mark the message as processed.
// In this case, a message with an empty body is simply ignored/discarded.
return nil
}

// do whatever actual message processing is desired
err := processMessage(m.Body)

// Returning a non-nil error will automatically send a REQ command to NSQ to re-queue the message.
Expand All @@ -42,6 +51,11 @@ Consuming messages from NSQ can be done by creating an instance of a Consumer an
log.Fatal(err)
}

// wait for signal to exit
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan

// Gracefully stop the consumer.
consumer.Stop()
}
Expand All @@ -67,7 +81,7 @@ Producing messages can be done by creating an instance of a Producer.
log.Fatal(err)
}

// Gracefully stop the producer.
// Gracefully stop the producer when appropriate (e.g. before shutting down the service)
producer.Stop()

*/
Expand Down