-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmanager.go
119 lines (96 loc) · 2.38 KB
/
manager.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package procs
import (
"fmt"
"sync"
)
// Manager manages a set of Processes.
type Manager struct {
Processes map[string]*Process
lock sync.Mutex
}
// NewManager creates a new *Manager.
func NewManager() *Manager {
return &Manager{
Processes: make(map[string]*Process),
}
}
// StdoutHandler returns an OutHandler that will ensure the underlying
// process has an empty stdout buffer and logs to stdout a prefixed value
// of "$name | $line".
func (m *Manager) StdoutHandler(name string) OutHandler {
return func(line string) string {
fmt.Printf("%s | %s\n", name, line)
return ""
}
}
// StderrHandler returns an OutHandler that will ensure the underlying
// process has an empty stderr buffer and logs to stdout a prefixed value
// of "$name | $line".
func (m *Manager) StderrHandler(name string) OutHandler {
return func(line string) string {
fmt.Printf("%s | %s\n", name, line)
return ""
}
}
// Start and managed a new process using the default handlers from a
// string.
func (m *Manager) Start(name, cmd string) error {
m.lock.Lock()
defer m.lock.Unlock()
p := NewProcess(cmd)
p.OutputHandler = m.StdoutHandler(name)
p.ErrHandler = m.StderrHandler(name)
err := p.Start()
if err != nil {
return err
}
m.Processes[name] = p
return nil
}
// StartProcess starts and manages a predifined process.
func (m *Manager) StartProcess(name string, p *Process) error {
m.lock.Lock()
defer m.lock.Unlock()
err := p.Start()
if err != nil {
return err
}
m.Processes[name] = p
return nil
}
// Stop will try to stop a managed process. If the process does not
// exist, no error is returned.
func (m *Manager) Stop(name string) error {
p, ok := m.Processes[name]
// We don't mind stopping a process that doesn't exist.
if !ok {
return nil
}
return p.Stop()
}
// Remove will try to stop and remove a managed process.
func (m *Manager) Remove(name string) error {
m.lock.Lock()
defer m.lock.Unlock()
err := m.Stop(name)
if err != nil {
return err
}
// Note that if the stop fails we don't remove it from the map of
// processes to avoid losing the reference.
delete(m.Processes, name)
return nil
}
// Wait will block until all managed processes have finished.
func (m *Manager) Wait() error {
wg := &sync.WaitGroup{}
wg.Add(len(m.Processes))
for _, p := range m.Processes {
go func(proc *Process) {
defer wg.Done()
proc.Wait()
}(p)
}
wg.Wait()
return nil
}