-
Notifications
You must be signed in to change notification settings - Fork 21
/
starter.go
592 lines (524 loc) · 13.9 KB
/
starter.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
package starter
import (
"fmt"
"net"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
)
var niceSigNames map[syscall.Signal]string
var niceNameToSigs map[string]syscall.Signal
var successStatus syscall.WaitStatus
var failureStatus syscall.WaitStatus
func makeNiceSigNamesCommon() map[syscall.Signal]string {
return map[syscall.Signal]string{
syscall.SIGABRT: "ABRT",
syscall.SIGALRM: "ALRM",
syscall.SIGBUS: "BUS",
// syscall.SIGEMT: "EMT",
syscall.SIGFPE: "FPE",
syscall.SIGHUP: "HUP",
syscall.SIGILL: "ILL",
// syscall.SIGINFO: "INFO",
syscall.SIGINT: "INT",
// syscall.SIGIOT: "IOT",
syscall.SIGKILL: "KILL",
syscall.SIGPIPE: "PIPE",
syscall.SIGQUIT: "QUIT",
syscall.SIGSEGV: "SEGV",
syscall.SIGTERM: "TERM",
syscall.SIGTRAP: "TRAP",
}
}
func makeNiceSigNames() map[syscall.Signal]string {
return addPlatformDependentNiceSigNames(makeNiceSigNamesCommon())
}
func init() {
niceSigNames = makeNiceSigNames()
niceNameToSigs = make(map[string]syscall.Signal)
for sig, name := range niceSigNames {
niceNameToSigs[name] = sig
}
}
type listener struct {
listener net.Listener
spec string // path or port spec
}
type Config interface {
Args() []string
Command() string
Dir() string // Directory to chdir to before executing the command
Interval() time.Duration // Time between checks for liveness
PidFile() string
Ports() []string // Ports to bind to (addr:port or port, so it's a string)
Paths() []string // Paths (UNIX domain socket) to bind to
SignalOnHUP() os.Signal // Signal to send when HUP is received
SignalOnTERM() os.Signal // Signal to send when TERM is received
StatusFile() string
}
type Starter struct {
interval time.Duration
signalOnHUP os.Signal
signalOnTERM os.Signal
// you can't set this in go: backlog
statusFile string
pidFile string
dir string
ports []string
paths []string
listeners []listener
generation int
command string
args []string
mu sync.RWMutex
}
// NewStarter creates a new Starter object. Config parameter may NOT be
// nil, as `Ports` and/or `Paths`, and `Command` are required
func NewStarter(c Config) (*Starter, error) {
if c == nil {
return nil, fmt.Errorf("config argument must be non-nil")
}
var signalOnHUP os.Signal = syscall.SIGTERM
var signalOnTERM os.Signal = syscall.SIGTERM
if s := c.SignalOnHUP(); s != nil {
signalOnHUP = s
}
if s := c.SignalOnTERM(); s != nil {
signalOnTERM = s
}
if c.Command() == "" {
return nil, fmt.Errorf("argument Command must be specified")
}
if _, err := exec.LookPath(c.Command()); err != nil {
return nil, err
}
s := &Starter{
args: c.Args(),
command: c.Command(),
dir: c.Dir(),
interval: c.Interval(),
listeners: make([]listener, 0, len(c.Ports())+len(c.Paths())),
pidFile: c.PidFile(),
ports: c.Ports(),
paths: c.Paths(),
signalOnHUP: signalOnHUP,
signalOnTERM: signalOnTERM,
statusFile: c.StatusFile(),
}
return s, nil
}
func (s *Starter) Stop() {
p, _ := os.FindProcess(os.Getpid())
_ = p.Signal(syscall.SIGTERM)
}
func grabExitStatus(st processState) syscall.WaitStatus {
// Note: POSSIBLY non portable. seems to work on Unix/Windows
// When/if this blows up, we will look for a cure
exitSt, ok := st.Sys().(syscall.WaitStatus)
if !ok {
fmt.Fprintf(os.Stderr, "Oh no, you are running on a platform where ProcessState.Sys().(syscall.WaitStatus) doesn't work! We're doomed! Temporarily setting status to 255. Please contact the author about this\n")
exitSt = failureStatus
}
return exitSt
}
type processState interface {
Pid() int
Sys() interface{}
}
type dummyProcessState struct {
pid int
status syscall.WaitStatus
}
func (d dummyProcessState) Pid() int {
return d.pid
}
func (d dummyProcessState) Sys() interface{} {
return d.status
}
func signame(s os.Signal) string {
if ss, ok := s.(syscall.Signal); ok {
return niceSigNames[ss]
}
return "UNKNOWN"
}
// SigFromName returns the signal corresponding to the given signal name string.
// If the given name string is not defined, it returns nil.
func SigFromName(n string) os.Signal {
n = strings.TrimPrefix(strings.ToUpper(n), "SIG")
if sig, ok := niceNameToSigs[n]; ok {
return sig
}
return nil
}
func setEnv() {
if os.Getenv("ENVDIR") == "" {
return
}
m, err := reloadEnv()
if err != nil && err != errNoEnv {
// do something
fmt.Fprintf(os.Stderr, "failed to load from envdir: %s\n", err)
}
for k, v := range m {
os.Setenv(k, v)
}
}
func parsePortSpec(addr string) (string, int, error) {
i := strings.IndexByte(addr, ':')
portPart := ""
if i < 0 {
portPart = addr
addr = ""
} else {
portPart = addr[i+1:]
addr = addr[:i]
}
port, err := strconv.ParseInt(portPart, 10, 64)
if err != nil {
return "", -1, err
}
return addr, int(port), nil
}
func (s *Starter) Run() error {
// nolint:errcheck
defer s.Teardown()
if s.pidFile != "" {
f, err := os.OpenFile(s.pidFile, os.O_EXCL|os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
return err
}
fmt.Fprintf(f, "%d", os.Getpid())
defer func() {
os.Remove(f.Name())
f.Close()
}()
}
for _, addr := range s.ports {
var l net.Listener
host, port, err := parsePortSpec(addr)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse addr spec '%s': %s", addr, err)
return err
}
hostport := fmt.Sprintf("%s:%d", host, port)
l, err = net.Listen("tcp4", hostport)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to listen to %s:%s\n", hostport, err)
return err
}
spec := ""
if host == "" {
spec = fmt.Sprintf("%d", port)
} else {
spec = fmt.Sprintf("%s:%d", host, port)
}
s.mu.Lock()
s.listeners = append(s.listeners, listener{listener: l, spec: spec})
s.mu.Unlock()
}
for _, path := range s.paths {
var l net.Listener
if fl, err := os.Lstat(path); err == nil && fl.Mode()&os.ModeSocket == os.ModeSocket {
fmt.Fprintf(os.Stderr, "removing existing socket file:%s\n", path)
err = os.Remove(path)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to remove existing socket file:%s:%s\n", path, err)
return err
}
}
_ = os.Remove(path)
l, err := net.Listen("unix", path)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to listen file:%s:%s\n", path, err)
return err
}
s.mu.Lock()
s.listeners = append(s.listeners, listener{listener: l, spec: path})
s.mu.Unlock()
}
s.generation = 0
os.Setenv("SERVER_STARTER_GENERATION", fmt.Sprintf("%d", s.generation))
// XXX Not portable
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)
// Okay, ready to launch the program now...
setEnv()
workerCh := make(chan processState)
p := s.StartWorker(sigCh, workerCh)
oldWorkers := make(map[int]int)
var sigReceived os.Signal
var sigToSend os.Signal
statusCh := make(chan map[int]int)
go func(fn string, ch chan map[int]int) {
for wmap := range ch {
if fn == "" {
continue
}
f, err := os.OpenFile(fn, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
continue
}
for gen, pid := range wmap {
fmt.Fprintf(f, "%d:%d\n", gen, pid)
}
f.Close()
}
}(s.statusFile, statusCh)
defer func() {
if p != nil {
oldWorkers[p.Pid] = s.generation
}
fmt.Fprintf(os.Stderr, "received %s, sending %s to all workers:",
signame(sigReceived),
signame(sigToSend),
)
size := len(oldWorkers)
i := 0
for pid := range oldWorkers {
i++
fmt.Fprintf(os.Stderr, "%d", pid)
if i < size {
fmt.Fprintf(os.Stderr, ",")
}
}
fmt.Fprintf(os.Stderr, "\n")
for pid := range oldWorkers {
worker, err := os.FindProcess(pid)
if err != nil {
continue
}
_ = worker.Signal(sigToSend)
}
for len(oldWorkers) > 0 {
st := <-workerCh
fmt.Fprintf(os.Stderr, "worker %d died, status:%d\n", st.Pid(), grabExitStatus(st))
delete(oldWorkers, st.Pid())
}
fmt.Fprintf(os.Stderr, "exiting\n")
}()
// var lastRestartTime time.Time
for { // outer loop
setEnv()
// Just wait for the worker to exit, or for us to receive a signal
for {
status := make(map[int]int)
for pid, gen := range oldWorkers {
status[gen] = pid
}
status[s.generation] = p.Pid
statusCh <- status
// restart = 2: force restart
// restart = 1 and no workers: force restart
// restart = 0: no restart
restart := 0
select {
case st := <-workerCh:
// oops, the worker exited? check for its pid
if p.Pid == st.Pid() { // current worker
exitSt := grabExitStatus(st)
fmt.Fprintf(os.Stderr, "worker %d died unexpectedly with status %d, restarting\n", p.Pid, exitSt)
p = s.StartWorker(sigCh, workerCh)
// lastRestartTime = time.Now()
} else {
exitSt := grabExitStatus(st)
fmt.Fprintf(os.Stderr, "old worker %d died, status:%d\n", st.Pid(), exitSt)
delete(oldWorkers, st.Pid())
}
case sigReceived = <-sigCh:
// Temporary fix
switch sigReceived {
case syscall.SIGHUP:
// When we receive a HUP signal, we need to spawn a new worker
fmt.Fprintf(os.Stderr, "received HUP (num_old_workers=TODO)\n")
restart = 1
sigToSend = s.signalOnHUP
case syscall.SIGTERM:
sigToSend = s.signalOnTERM
return nil
default:
sigToSend = syscall.SIGTERM
return nil
}
}
if restart > 1 || restart > 0 && len(oldWorkers) == 0 {
fmt.Fprintf(os.Stderr, "spawning a new worker (num_old_workers=TODO)\n")
oldWorkers[p.Pid] = s.generation
p = s.StartWorker(sigCh, workerCh)
fmt.Fprintf(os.Stderr, "new worker is now running, sending %s to old workers:", signame(sigToSend))
size := len(oldWorkers)
if size == 0 {
fmt.Fprintf(os.Stderr, "none\n")
} else {
i := 0
for pid := range oldWorkers {
i++
fmt.Fprintf(os.Stderr, "%d", pid)
if i < size {
fmt.Fprintf(os.Stderr, ",")
}
}
fmt.Fprintf(os.Stderr, "\n")
killOldDelay := getKillOldDelay()
fmt.Fprintf(os.Stderr, "sleep %d secs\n", int(killOldDelay/time.Second))
if killOldDelay > 0 {
time.Sleep(killOldDelay)
}
fmt.Fprintf(os.Stderr, "killing old workers\n")
for pid := range oldWorkers {
worker, err := os.FindProcess(pid)
if err != nil {
continue
}
_ = worker.Signal(s.signalOnHUP)
}
}
}
}
}
// nolint:govet
return nil
}
func getKillOldDelay() time.Duration {
// Ignore errors.
delay, _ := strconv.ParseInt(os.Getenv("KILL_OLD_DELAY"), 10, 0)
autoRestart, _ := strconv.ParseBool(os.Getenv("ENABLE_AUTO_RESTART"))
if autoRestart && delay == 0 {
delay = 5
}
return time.Duration(delay) * time.Second
}
type WorkerState int
const (
WorkerStarted WorkerState = iota
ErrFailedToStart
)
// StartWorker starts the actual command.
func (s *Starter) StartWorker(sigCh chan os.Signal, ch chan processState) *os.Process {
// Don't give up until we're running.
for {
pid := -1
cmd := exec.Command(s.command, s.args...)
if s.dir != "" {
cmd.Dir = s.dir
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// This whole section here basically sets up the env
// var and the file descriptors that are inherited by the
// external process
files := make([]*os.File, len(s.ports)+len(s.paths))
ports := make([]string, len(s.ports)+len(s.paths))
s.mu.RLock()
for i, l := range s.listeners {
// file descriptor numbers in ExtraFiles turn out to be
// index + 3, so we can just hard code it
var f *os.File
var err error
switch l.listener.(type) {
case *net.TCPListener:
f, err = l.listener.(*net.TCPListener).File()
case *net.UnixListener:
f, err = l.listener.(*net.UnixListener).File()
default:
panic("Unknown listener type")
}
if err != nil {
panic(err)
}
defer f.Close()
ports[i] = fmt.Sprintf("%s=%d", l.spec, i+3)
files[i] = f
}
s.mu.RUnlock()
cmd.ExtraFiles = files
s.generation++
os.Setenv("SERVER_STARTER_PORT", strings.Join(ports, ";"))
os.Setenv("SERVER_STARTER_GENERATION", fmt.Sprintf("%d", s.generation))
// Now start!
if err := cmd.Start(); err != nil {
fmt.Fprintf(os.Stderr, "failed to exec %s: %s\n", cmd.Path, err)
} else {
// Save pid...
pid = cmd.Process.Pid
fmt.Fprintf(os.Stderr, "starting new worker %d\n", pid)
// Wait for interval before checking if the process is alive
tch := time.After(s.interval)
sigs := []os.Signal{}
for loop := true; loop; {
select {
case <-tch:
// bail out
loop = false
case sig := <-sigCh:
sigs = append(sigs, sig)
}
}
// if received any signals, during the wait, we bail out
gotSig := false
if len(sigs) > 0 {
for _, sig := range sigs {
// we need to resend these signals so it can be caught in the
// main routine...
go func(sig os.Signal) {
sigCh <- sig
}(sig)
if sysSig, ok := sig.(syscall.Signal); ok {
if sysSig != syscall.SIGHUP {
gotSig = true
}
}
}
}
// Check if we can find a process by its pid
p := findWorker(pid)
if gotSig || p != nil {
// No error? We were successful! Make sure we capture
// the program exiting
go func() {
err := cmd.Wait()
if err != nil {
ch <- err.(*exec.ExitError).ProcessState
} else {
ch <- &dummyProcessState{pid: pid, status: successStatus}
}
}()
// Bail out
return p
}
}
// If we fall through here, we prematurely exited :/
// Make sure to wait to release resources
_ = cmd.Wait()
for _, f := range cmd.ExtraFiles {
f.Close()
}
fmt.Fprintf(os.Stderr, "new worker %d seems to have failed to start\n", pid)
}
// never reached
//nolint:govet
return nil
}
func (s *Starter) Teardown() error {
if s.statusFile != "" {
os.Remove(s.statusFile)
}
s.mu.RLock()
for _, l := range s.listeners {
l.listener.Close()
}
s.mu.RUnlock()
return nil
}