seaweedfs/weed/command/watch.go

66 lines
1.7 KiB
Go
Raw Normal View History

2020-04-05 15:51:16 +08:00
package command
import (
"context"
"fmt"
"io"
2020-04-28 14:49:46 +08:00
"time"
2020-04-05 15:51:16 +08:00
"github.com/chrislusf/seaweedfs/weed/pb"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/security"
"github.com/chrislusf/seaweedfs/weed/util"
)
func init() {
cmdWatch.Run = runWatch // break init cycle
2020-04-05 15:51:16 +08:00
}
var cmdWatch = &Command{
UsageLine: "watch <wip> [-filer=localhost:8888] [-target=/]",
2020-04-05 15:51:16 +08:00
Short: "see recent changes on a filer",
Long: `See recent changes on a filer.
`,
}
var (
watchFiler = cmdWatch.Flag.String("filer", "localhost:8888", "filer hostname:port")
2020-04-06 03:51:21 +08:00
watchTarget = cmdWatch.Flag.String("pathPrefix", "/", "path to a folder or file, or common prefix for the folders or files on filer")
2020-04-28 14:49:46 +08:00
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\"")
2020-04-05 15:51:16 +08:00
)
func runWatch(cmd *Command, args []string) bool {
2020-04-05 15:51:16 +08:00
grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
watchErr := pb.WithFilerClient(*watchFiler, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
2020-04-05 15:51:16 +08:00
2020-04-13 12:00:55 +08:00
stream, err := client.SubscribeMetadata(context.Background(), &filer_pb.SubscribeMetadataRequest{
ClientName: "watch",
2020-04-06 03:51:21 +08:00
PathPrefix: *watchTarget,
2020-04-28 14:49:46 +08:00
SinceNs: time.Now().Add(-*watchStart).UnixNano(),
2020-04-05 15:51:16 +08:00
})
if err != nil {
return fmt.Errorf("listen: %v", err)
}
for {
resp, listenErr := stream.Recv()
if listenErr == io.EOF {
return nil
}
if listenErr != nil {
return listenErr
}
fmt.Printf("events: %+v\n", resp.EventNotification)
}
})
if watchErr != nil {
fmt.Printf("watch %s: %v\n", *watchFiler, watchErr)
2020-04-05 15:51:16 +08:00
}
return true
}