Skip to content

Commit

Permalink
Merge pull request #307 from ploxiln/doc_example_more
Browse files Browse the repository at this point in the history
docs: add exit-on-signal and more comments to example consumer
  • Loading branch information
mreiferson committed Aug 15, 2020
2 parents bf9a770 + ce3a707 commit d71da5b
Showing 1 changed file with 15 additions and 1 deletion.
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

0 comments on commit d71da5b

Please sign in to comment.