-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathprocesstree.go
105 lines (90 loc) · 2.24 KB
/
processtree.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package processtree
import (
"sync"
)
type ProcessTree struct {
Root *SlaveNode
ExecCommand string
SlavesByName map[string]*SlaveNode
Commands []*CommandNode
StateChanged chan bool
}
type ProcessTreeNode struct {
mu sync.RWMutex
Parent *SlaveNode
Name string
}
type CommandNode struct {
ProcessTreeNode
booting sync.RWMutex
Aliases []string
}
func (tree *ProcessTree) NewCommandNode(name string, aliases []string, parent *SlaveNode) *CommandNode {
x := &CommandNode{}
x.Parent = parent
x.Name = name
x.Aliases = aliases
tree.Commands = append(tree.Commands, x)
return x
}
func (tree *ProcessTree) FindSlaveByName(name string) *SlaveNode {
if name == "" {
return tree.Root
}
return tree.SlavesByName[name]
}
func (tree *ProcessTree) FindCommand(requested string) *CommandNode {
for _, command := range tree.Commands {
if command.Name == requested {
return command
}
for _, alias := range command.Aliases {
if alias == requested {
return command
}
}
}
return nil
}
func (tree *ProcessTree) AllCommandsAndAliases() []string {
var values []string
for _, command := range tree.Commands {
values = append(values, command.Name)
for _, alias := range command.Aliases {
values = append(values, alias)
}
}
return values
}
var restartMutex sync.Mutex
func (tree *ProcessTree) RestartNodesWithFeatures(files []string) {
restartMutex.Lock()
defer restartMutex.Unlock()
tree.Root.trace("%d files changed, beginning with %q", len(files), files[0])
tree.Root.restartNodesWithFeatures(tree, files)
}
// Serialized: restartMutex is always held when this is called.
func (node *SlaveNode) restartNodesWithFeatures(tree *ProcessTree, files []string) {
for _, file := range files {
if node.HasFeature(file) {
node.trace("restarting for %q", file)
node.RequestRestart()
return
}
}
for _, s := range node.Slaves {
s.restartNodesWithFeatures(tree, files)
}
}
// We implement sort.Interface - Len, Less, and Swap - on list of commands so
// we can use the sort package’s generic Sort function.
type Commands []*CommandNode
func (c Commands) Len() int {
return len(c)
}
func (c Commands) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c Commands) Less(i, j int) bool {
return c[i].Name < c[j].Name
}