aboutsummaryrefslogtreecommitdiff
path: root/weed/shell/command_fs_meta_notify.go
blob: e2b2d22ccf3eca02b969258baa51fb4cb1adc678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package shell

import (
	"context"
	"fmt"
	"io"

	"github.com/chrislusf/seaweedfs/weed/filer2"
	"github.com/chrislusf/seaweedfs/weed/notification"
	"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
	"github.com/chrislusf/seaweedfs/weed/util"
)

func init() {
	Commands = append(Commands, &commandFsMetaNotify{})
}

type commandFsMetaNotify struct {
}

func (c *commandFsMetaNotify) Name() string {
	return "fs.meta.notify"
}

func (c *commandFsMetaNotify) Help() string {
	return `recursively send directory and file meta data to notifiction message queue

	fs.meta.notify	# send meta data from current directory to notification message queue

	The message queue will use it to trigger replication from this filer.

`
}

func (c *commandFsMetaNotify) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {

	filerServer, filerPort, path, err := commandEnv.parseUrl(findInputDirectory(args))
	if err != nil {
		return err
	}

	util.LoadConfiguration("notification", true)
	v := util.GetViper()
	notification.LoadConfiguration(v, "notification.")

	ctx := context.Background()

	var dirCount, fileCount uint64

	err = doTraverseBFS(ctx, writer, commandEnv.getFilerClient(filerServer, filerPort), filer2.FullPath(path), func(parentPath filer2.FullPath, entry *filer_pb.Entry) {

		if entry.IsDirectory {
			dirCount++
		} else {
			fileCount++
		}

		notifyErr := notification.Queue.SendMessage(
			string(parentPath.Child(entry.Name)),
			&filer_pb.EventNotification{
				NewEntry: entry,
			},
		)

		if notifyErr != nil {
			fmt.Fprintf(writer, "fail to notify new entry event for %s: %v\n", parentPath.Child(entry.Name), notifyErr)
		}

	})

	if err == nil {
		fmt.Fprintf(writer, "\ntotal notified %d directories, %d files\n", dirCount, fileCount)
	}

	return err

}