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.

65 lines
1.7 KiB

5 years ago
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/security"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. func init() {
  13. cmdWatch.Run = runWatch // break init cycle
  14. }
  15. var cmdWatch = &Command{
  16. UsageLine: "watch <wip> [-filer=localhost:8888] [-target=/]",
  17. Short: "see recent changes on a filer",
  18. Long: `See recent changes on a filer.
  19. `,
  20. }
  21. var (
  22. watchFiler = cmdWatch.Flag.String("filer", "localhost:8888", "filer hostname:port")
  23. watchTarget = cmdWatch.Flag.String("pathPrefix", "/", "path to a folder or file, or common prefix for the folders or files on filer")
  24. watchStart = cmdWatch.Flag.Duration("timeAgo", 0, "start time before now. \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"")
  25. )
  26. func runWatch(cmd *Command, args []string) bool {
  27. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  28. watchErr := pb.WithFilerClient(*watchFiler, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  29. stream, err := client.SubscribeMetadata(context.Background(), &filer_pb.SubscribeMetadataRequest{
  30. ClientName: "watch",
  31. PathPrefix: *watchTarget,
  32. SinceNs: time.Now().Add(-*watchStart).UnixNano(),
  33. })
  34. if err != nil {
  35. return fmt.Errorf("listen: %v", err)
  36. }
  37. for {
  38. resp, listenErr := stream.Recv()
  39. if listenErr == io.EOF {
  40. return nil
  41. }
  42. if listenErr != nil {
  43. return listenErr
  44. }
  45. fmt.Printf("events: %+v\n", resp.EventNotification)
  46. }
  47. })
  48. if watchErr != nil {
  49. fmt.Printf("watch %s: %v\n", *watchFiler, watchErr)
  50. }
  51. return true
  52. }