-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nsq_tail: an example reader to make it really easy to 'tail' a topic …
…with an ephemeral channel
- Loading branch information
1 parent
62eeb9d
commit 23b58e2
Showing
6 changed files
with
111 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"../../nsq" | ||
"../../util" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
var ( | ||
showVersion = flag.Bool("version", false, "print version string") | ||
topic = flag.String("topic", "", "nsq topic") | ||
channel = flag.String("channel", "nsq_tail#ephemeral", "nsq channel") | ||
maxInFlight = flag.Int("max-in-flight", 200, "max number of messages to allow in flight") | ||
nsqdTCPAddrs = util.StringArray{} | ||
lookupdHTTPAddrs = util.StringArray{} | ||
) | ||
|
||
func init() { | ||
flag.Var(&nsqdTCPAddrs, "nsqd-tcp-address", "nsqd TCP address (may be given multiple times)") | ||
flag.Var(&lookupdHTTPAddrs, "lookupd-http-address", "lookupd HTTP address (may be given multiple times)") | ||
} | ||
|
||
type TailHandler struct{} | ||
|
||
func (th *TailHandler) HandleMessage(m *nsq.Message) error { | ||
_, err := os.Stdout.Write(m.Body) | ||
if err != nil { | ||
log.Fatalf("ERROR: failed to write to os.Stdout - %s", err.Error()) | ||
} | ||
_, err = os.Stdout.WriteString("\n") | ||
if err != nil { | ||
log.Fatalf("ERROR: failed to write to os.Stdout - %s", err.Error()) | ||
} | ||
return nil | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if *showVersion { | ||
fmt.Printf("nsq_tail v%s\n", util.BINARY_VERSION) | ||
return | ||
} | ||
|
||
if *topic == "" || *channel == "" { | ||
log.Fatalf("--topic and --channel are required") | ||
} | ||
|
||
if *maxInFlight < 0 { | ||
log.Fatalf("--max-in-flight must be > 0") | ||
} | ||
|
||
if len(nsqdTCPAddrs) == 0 && len(lookupdHTTPAddrs) == 0 { | ||
log.Fatalf("--nsqd-tcp-address or --lookupd-http-address required") | ||
} | ||
if len(nsqdTCPAddrs) > 0 && len(lookupdHTTPAddrs) > 0 { | ||
log.Fatalf("use --nsqd-tcp-address or --lookupd-http-address not both") | ||
} | ||
|
||
sigChan := make(chan os.Signal, 1) | ||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) | ||
|
||
r, err := nsq.NewReader(*topic, *channel) | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
r.SetMaxInFlight(*maxInFlight) | ||
r.AddHandler(&TailHandler{}) | ||
|
||
for _, addrString := range nsqdTCPAddrs { | ||
err := r.ConnectToNSQ(addrString) | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
} | ||
|
||
for _, addrString := range lookupdHTTPAddrs { | ||
log.Printf("lookupd addr %s", addrString) | ||
err := r.ConnectToLookupd(addrString) | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
} | ||
|
||
select { | ||
case <-r.ExitChan: | ||
case <-sigChan: | ||
r.Stop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters