You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB

  1. // Program estream is a manual testing tool for polling the event stream
  2. // of a running Tendermint consensus node.
  3. package main
  4. import (
  5. "context"
  6. "encoding/json"
  7. "flag"
  8. "fmt"
  9. "log"
  10. "os"
  11. "os/signal"
  12. "path/filepath"
  13. "github.com/tendermint/tendermint/rpc/client/eventstream"
  14. rpcclient "github.com/tendermint/tendermint/rpc/client/http"
  15. "github.com/tendermint/tendermint/rpc/coretypes"
  16. )
  17. var (
  18. query = flag.String("query", "", "Filter query")
  19. batchSize = flag.Int("batch", 0, "Batch size")
  20. resumeFrom = flag.String("resume", "", "Resume cursor")
  21. numItems = flag.Int("count", 0, "Number of items to read (0 to stream)")
  22. waitTime = flag.Duration("poll", 0, "Long poll interval")
  23. rpcAddr = flag.String("addr", "http://localhost:26657", "RPC service address")
  24. )
  25. func init() {
  26. flag.Usage = func() {
  27. fmt.Fprintf(os.Stderr, `Usage: %[1]s [options]
  28. Connect to the Tendermint node whose RPC service is at -addr, and poll for events
  29. matching the specified -query. If no query is given, all events are fetched.
  30. The resulting event data are written to stdout as JSON.
  31. Use -resume to pick up polling from a previously-reported event cursor.
  32. Use -count to stop polling after a certain number of events has been reported.
  33. Use -batch to override the default request batch size.
  34. Use -poll to override the default long-polling interval.
  35. Options:
  36. `, filepath.Base(os.Args[0]))
  37. flag.PrintDefaults()
  38. }
  39. }
  40. func main() {
  41. flag.Parse()
  42. cli, err := rpcclient.New(*rpcAddr)
  43. if err != nil {
  44. log.Fatalf("RPC client: %v", err)
  45. }
  46. stream := eventstream.New(cli, *query, &eventstream.StreamOptions{
  47. BatchSize: *batchSize,
  48. ResumeFrom: *resumeFrom,
  49. WaitTime: *waitTime,
  50. })
  51. // Shut down cleanly on SIGINT. Don't attempt clean shutdown for other
  52. // fatal signals.
  53. ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
  54. defer cancel()
  55. var nr int
  56. if err := stream.Run(ctx, func(itm *coretypes.EventItem) error {
  57. nr++
  58. bits, err := json.Marshal(itm)
  59. if err != nil {
  60. return err
  61. }
  62. fmt.Println(string(bits))
  63. if *numItems > 0 && nr >= *numItems {
  64. return eventstream.ErrStopRunning
  65. }
  66. return nil
  67. }); err != nil {
  68. log.Fatalf("Stream failed: %v", err)
  69. }
  70. }