-
Notifications
You must be signed in to change notification settings - Fork 0
/
z.go
201 lines (177 loc) · 3.49 KB
/
z.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/vanng822/gopid"
)
type Goz struct {
gods []*God
sigc chan os.Signal
ticker *time.Ticker
args Args
gracefulWait time.Duration
}
func NewGoz() *Goz {
z := new(Goz)
z.gods = make([]*God, 0)
z.gracefulWait = 3 * time.Second
return z
}
func (z *Goz) Add(d *God) {
z.gods = append(z.gods, d)
}
func (z *Goz) Start() {
defer func() {
if r := recover(); r != nil {
// print error first
log.Println(r)
for _, d := range z.gods {
if !d.started.IsZero() {
log.Printf("Stopping '%s' before exit", d.name)
d.Stop()
}
}
}
}()
args := Args{}
z.args = args
if err := args.Parse(os.Args[1:]); err != nil {
usage()
return
}
if args.version {
version()
return
}
if args.help || len(args.programs) == 0 {
usage()
return
}
if args.pidFile != "" {
gopid.CheckPid(args.pidFile, args.force)
gopid.CreatePid(args.pidFile)
defer gopid.CleanPid(args.pidFile)
}
if args.logFile != "" {
logwritter, err := os.OpenFile(args.logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
os.Stdout = logwritter
}
if args.logFileErr != "" {
errlogwritter, err := os.OpenFile(args.logFileErr, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
os.Stderr = errlogwritter
}
for i, p := range args.programs {
pargs := args.programArgs[i]
pargs = append(pargs, args.args...)
z.Add(NewGod(p, pargs))
}
for _, d := range z.gods {
d.Start(nil)
}
if args.fileWatched {
go func() {
restartTimer := time.NewTimer(24 * 365 * 10 * time.Hour)
shouldRestart := make(chan bool)
go watchDirs(args.fileWatchedDirs, args.fileWatchedExts, shouldRestart)
for {
select {
case <-restartTimer.C:
z.Restart(args.graceful)
case restart := <-shouldRestart:
if restart {
restartTimer.Reset(time.Duration(1000 * 1000 * 1000 * args.delaySecs))
}
}
}
}()
}
z.startInterval(args.interval)
z.sigc = make(chan os.Signal, 1)
// stop
signals := []os.Signal{os.Kill, os.Interrupt, syscall.SIGTERM}
// restart
signals = append(signals, syscall.SIGHUP)
// generic signal to send to processes
signals = append(signals, syscall.SIGUSR1, syscall.SIGUSR2)
signal.Notify(z.sigc, signals...)
for {
sig := <-z.sigc
log.Printf("Got signal %v", sig)
switch sig {
case syscall.SIGHUP:
z.Restart(args.graceful)
z.startInterval(args.interval)
case syscall.SIGUSR1:
z.Signal(sig)
case syscall.SIGUSR2:
z.Restart(true)
z.startInterval(args.interval)
default:
log.Println("Stop program")
z.Stop()
return
}
}
}
func (z *Goz) stopInterval() {
if z.ticker != nil {
z.ticker.Stop()
}
}
func (z *Goz) startInterval(secs int) {
if float64(secs) <= MIMIMUM_AGE {
return
}
z.stopInterval()
z.ticker = time.NewTicker(time.Duration(secs) * time.Second)
go func() {
for {
select {
case <-z.ticker.C:
z.Restart(z.args.graceful)
}
}
}()
}
func (z *Goz) Signal(s os.Signal) {
for _, d := range z.gods {
d.Signal(s)
}
}
func (z *Goz) Stop() {
for _, d := range z.gods {
d.Stop()
}
}
func waitDone(done chan bool) {
for {
select {
case <-done:
return
default:
time.Sleep(50 * time.Millisecond)
}
}
}
func (z *Goz) Restart(graceful bool) {
for _, d := range z.gods {
if graceful {
done := make(chan bool)
d.Restart(done)
waitDone(done)
close(done)
time.Sleep(z.gracefulWait)
} else {
d.Restart(nil)
}
}
}