command_fs_meta_notify.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/chrislusf/seaweedfs/weed/filer2"
  7. "github.com/chrislusf/seaweedfs/weed/notification"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandFsMetaNotify{})
  13. }
  14. type commandFsMetaNotify struct {
  15. }
  16. func (c *commandFsMetaNotify) Name() string {
  17. return "fs.meta.notify"
  18. }
  19. func (c *commandFsMetaNotify) Help() string {
  20. return `recursively send directory and file meta data to notifiction message queue
  21. fs.meta.notify # send meta data from current directory to notification message queue
  22. The message queue will use it to trigger replication from this filer.
  23. `
  24. }
  25. func (c *commandFsMetaNotify) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. filerServer, filerPort, path, err := commandEnv.parseUrl(findInputDirectory(args))
  27. if err != nil {
  28. return err
  29. }
  30. util.LoadConfiguration("notification", true)
  31. v := util.GetViper()
  32. notification.LoadConfiguration(v, "notification.")
  33. ctx := context.Background()
  34. var dirCount, fileCount uint64
  35. err = doTraverseBFS(ctx, writer, commandEnv.getFilerClient(filerServer, filerPort), filer2.FullPath(path), func(parentPath filer2.FullPath, entry *filer_pb.Entry) {
  36. if entry.IsDirectory {
  37. dirCount++
  38. } else {
  39. fileCount++
  40. }
  41. notifyErr := notification.Queue.SendMessage(
  42. string(parentPath.Child(entry.Name)),
  43. &filer_pb.EventNotification{
  44. NewEntry: entry,
  45. },
  46. )
  47. if notifyErr != nil {
  48. fmt.Fprintf(writer, "fail to notify new entry event for %s: %v\n", parentPath.Child(entry.Name), notifyErr)
  49. }
  50. })
  51. if err == nil {
  52. fmt.Fprintf(writer, "\ntotal notified %d directories, %d files\n", dirCount, fileCount)
  53. }
  54. return err
  55. }