-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
proc.go
181 lines (164 loc) · 3.57 KB
/
proc.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"sync"
"time"
)
// spawnProc starts the specified proc, and returns any error from running it.
func spawnProc(name string, errCh chan<- error) {
proc := findProc(name)
logger := createLogger(name, proc.colorIndex)
cs := append(cmdStart, proc.cmdline)
cmd := exec.Command(cs[0], cs[1:]...)
cmd.Stdin = nil
cmd.Stdout = logger
cmd.Stderr = logger
cmd.SysProcAttr = procAttrs
if proc.setPort {
cmd.Env = append(os.Environ(), fmt.Sprintf("PORT=%d", proc.port))
fmt.Fprintf(logger, "Starting %s on port %d\n", name, proc.port)
}
if err := cmd.Start(); err != nil {
select {
case errCh <- err:
default:
}
fmt.Fprintf(logger, "Failed to start %s: %s\n", name, err)
return
}
proc.cmd = cmd
proc.stoppedBySupervisor = false
proc.mu.Unlock()
err := cmd.Wait()
proc.mu.Lock()
proc.cond.Broadcast()
if err != nil && !proc.stoppedBySupervisor {
select {
case errCh <- err:
default:
}
}
proc.waitErr = err
proc.cmd = nil
fmt.Fprintf(logger, "Terminating %s\n", name)
}
// Stop the specified proc, issuing os.Kill if it does not terminate within 10
// seconds. If signal is nil, os.Interrupt is used.
func stopProc(name string, signal os.Signal) error {
if signal == nil {
signal = os.Interrupt
}
proc := findProc(name)
if proc == nil {
return errors.New("unknown proc: " + name)
}
proc.mu.Lock()
defer proc.mu.Unlock()
if proc.cmd == nil {
return nil
}
proc.stoppedBySupervisor = true
err := terminateProc(proc, signal)
if err != nil {
return err
}
timeout := time.AfterFunc(10*time.Second, func() {
proc.mu.Lock()
defer proc.mu.Unlock()
if proc.cmd != nil {
err = killProc(proc.cmd.Process)
}
})
proc.cond.Wait()
timeout.Stop()
return err
}
// start specified proc. if proc is started already, return nil.
func startProc(name string, wg *sync.WaitGroup, errCh chan<- error) error {
proc := findProc(name)
if proc == nil {
return errors.New("unknown name: " + name)
}
proc.mu.Lock()
if proc.cmd != nil {
proc.mu.Unlock()
return nil
}
if wg != nil {
wg.Add(1)
}
go func() {
spawnProc(name, errCh)
if wg != nil {
wg.Done()
}
proc.mu.Unlock()
}()
return nil
}
// restart specified proc.
func restartProc(name string) error {
err := stopProc(name, nil)
if err != nil {
return err
}
return startProc(name, nil, nil)
}
// stopProcs attempts to stop every running process and returns any non-nil
// error, if one exists. stopProcs will wait until all procs have had an
// opportunity to stop.
func stopProcs(sig os.Signal) error {
var err error
for _, proc := range procs {
stopErr := stopProc(proc.name, sig)
if stopErr != nil {
err = stopErr
}
}
return err
}
// spawn all procs.
func startProcs(sc <-chan os.Signal, rpcCh <-chan *rpcMessage, exitOnError bool) error {
var wg sync.WaitGroup
errCh := make(chan error, 1)
for _, proc := range procs {
startProc(proc.name, &wg, errCh)
}
allProcsDone := make(chan struct{}, 1)
if *exitOnStop {
go func() {
wg.Wait()
allProcsDone <- struct{}{}
}()
}
for {
select {
case rpcMsg := <-rpcCh:
switch rpcMsg.Msg {
// TODO: add more events here.
case "stop":
for _, proc := range rpcMsg.Args {
if err := stopProc(proc, nil); err != nil {
rpcMsg.ErrCh <- err
break
}
}
close(rpcMsg.ErrCh)
default:
panic("unimplemented rpc message type " + rpcMsg.Msg)
}
case err := <-errCh:
if exitOnError {
stopProcs(os.Interrupt)
return err
}
case <-allProcsDone:
return stopProcs(os.Interrupt)
case sig := <-sc:
return stopProcs(sig)
}
}
}