forked from kuangchanglang/graceful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
160 lines (139 loc) · 2.95 KB
/
worker.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
//go:build !windows
// +build !windows
package graceful
import (
"errors"
"fmt"
"github.com/gofiber/fiber/v2"
"log"
"net"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
)
const (
workerStopSignal = syscall.SIGTERM
)
var (
ErrNoServers = errors.New("no servers")
)
type worker struct {
handlers []*fiber.App
servers []server
opt *option
stopCh chan struct{}
sync.Mutex
}
type server struct {
*fiber.App
listener net.Listener
}
func (w *worker) run() error {
// init servers with fds from master
err := w.initServers()
if err != nil {
return err
}
// start http servers
err = w.startServers()
if err != nil {
return err
}
oldWorkerPid, err := strconv.Atoi(os.Getenv(EnvOldWorkerPid))
if err == nil && oldWorkerPid > 1 {
// tell old worker i'm ready, you should go away
err = syscall.Kill(oldWorkerPid, workerStopSignal)
if err != nil {
// unexpected: kill old worker fail
log.Printf("[warning] kill old worker error: %v\n", err)
}
}
go w.watchMaster()
// waitSignal
w.waitSignal()
return nil
}
func (w *worker) initServers() error {
numFDs, err := strconv.Atoi(os.Getenv(EnvNumFD))
if err != nil {
return fmt.Errorf("invalid %s integer", EnvNumFD)
}
if len(w.handlers) != numFDs {
return fmt.Errorf("handler number does not match numFDs, %v!=%v", len(w.handlers), numFDs)
}
for i := 0; i < numFDs; i++ {
f := os.NewFile(uintptr(3+i), "") // fd start from 3
l, err := net.FileListener(f)
if err != nil {
return fmt.Errorf("failed to inherit file descriptor: %d", i)
}
server := server{
App: w.handlers[i],
listener: l,
}
w.servers = append(w.servers, server)
}
return nil
}
func (w *worker) startServers() error {
if len(w.servers) == 0 {
return ErrNoServers
}
for i := 0; i < len(w.servers); i++ {
s := w.servers[i]
go func() {
if err := s.Listener(s.listener); err != nil {
log.Printf("http Serve error: %v\n", err)
}
}()
}
return nil
}
// watchMaster to monitor if master dead
func (w *worker) watchMaster() error {
masterPid := os.Getenv(EnvParentPid)
for {
// if parent id change to 1, it means parent is dead
if !processExist(masterPid) {
log.Printf("master dead, stop worker\n")
w.stop()
break
}
time.Sleep(w.opt.watchInterval)
}
w.stopCh <- struct{}{}
return nil
}
func processExist(pid string) bool {
Pid, _ := strconv.ParseInt(pid, 10, 64)
if err := syscall.Kill(int(Pid), 0); err != nil {
log.Printf("kill err: %v", err)
return false
}
return true
}
func (w *worker) waitSignal() {
ch := make(chan os.Signal)
signal.Notify(ch, workerStopSignal)
select {
case sig := <-ch:
log.Printf("worker got signal: %v\n", sig)
case <-w.stopCh:
log.Printf("stop worker")
}
w.stop()
}
// TODO: shutdown in parallel
func (w *worker) stop() {
w.Lock()
defer w.Unlock()
for _, server := range w.servers {
err := server.ShutdownWithTimeout(w.opt.stopTimeout)
if err != nil {
log.Printf("shutdown server error: %v\n", err)
}
}
}