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.

79 lines
1.8 KiB

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