forked from mattn/goreman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc.go
182 lines (164 loc) · 3.65 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
182
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"sync"
"time"
)
// spawnProc starts the specified proc, and returns any error from running it.
func spawnProc(proc string, errCh chan<- error) {
procObj := procs[proc]
logger := createLogger(proc, procObj.colorIndex)
cs := append(cmdStart, procObj.cmdline)
cmd := exec.Command(cs[0], cs[1:]...)
cmd.Stdin = nil
cmd.Stdout = logger
cmd.Stderr = logger
cmd.SysProcAttr = procAttrs
if procObj.setPort {
cmd.Env = append(os.Environ(), fmt.Sprintf("PORT=%d", procObj.port))
fmt.Fprintf(logger, "Starting %s on port %d\n", proc, procObj.port)
}
if err := cmd.Start(); err != nil {
select {
case errCh <- err:
default:
}
fmt.Fprintf(logger, "Failed to start %s: %s\n", proc, err)
return
}
procObj.cmd = cmd
procObj.stoppedBySupervisor = false
procObj.mu.Unlock()
err := cmd.Wait()
procObj.mu.Lock()
procObj.cond.Broadcast()
if err != nil && procObj.stoppedBySupervisor == false {
select {
case errCh <- err:
default:
}
}
procObj.waitErr = err
procObj.cmd = nil
fmt.Fprintf(logger, "Terminating %s\n", proc)
}
// 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(proc string, signal os.Signal) error {
if signal == nil {
signal = os.Interrupt
}
p, ok := procs[proc]
if !ok || p == nil {
return errors.New("unknown proc: " + proc)
}
p.mu.Lock()
defer p.mu.Unlock()
if p.cmd == nil {
return nil
}
p.stoppedBySupervisor = true
err := terminateProc(proc, signal)
if err != nil {
return err
}
timeout := time.AfterFunc(10*time.Second, func() {
p.mu.Lock()
defer p.mu.Unlock()
if p, ok := procs[proc]; ok && p.cmd != nil {
err = killProc(p.cmd.Process)
}
})
p.cond.Wait()
timeout.Stop()
return err
}
// start specified proc. if proc is started already, return nil.
func startProc(proc string, wg *sync.WaitGroup, errCh chan<- error) error {
p, ok := procs[proc]
if !ok || p == nil {
return errors.New("unknown proc: " + proc)
}
p.mu.Lock()
if procs[proc].cmd != nil {
p.mu.Unlock()
return nil
}
if wg != nil {
wg.Add(1)
}
go func() {
spawnProc(proc, errCh)
if wg != nil {
wg.Done()
}
p.mu.Unlock()
}()
return nil
}
// restart specified proc.
func restartProc(proc string) error {
p, ok := procs[proc]
if !ok || p == nil {
return errors.New("unknown proc: " + proc)
}
stopProc(proc, nil)
return startProc(proc, 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, 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, &wg, errCh)
}
allProcsDone := make(chan struct{}, 1)
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)
}
}
return nil
}