-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
105 lines (97 loc) · 2.31 KB
/
service.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
package dyndns
import (
"os"
"os/signal"
"sync"
"syscall"
"time"
"go.uber.org/zap"
)
func runServiceManager(s *Server, wg *sync.WaitGroup, goRoutineCount int) {
defer wg.Done()
var fn = s.name + "-service-mgr"
s.log.Debug(
"starting sybsystem",
zap.String("subsystem", fn),
zap.String("app", s.name),
zap.Any("config", s.GetConfig()),
)
M:
for {
select {
case err := <-s.ctx.error:
if err != nil {
s.ctx.fatalError = err
s.log.Error(
"notifying goroutines in response to subsystem error",
zap.String("app", s.name),
zap.String("error", err.Error()),
)
for i := 0; i < goRoutineCount; i++ {
s.ctx.exitRoutine <- true
}
} else {
goRoutineCount--
}
case reloadNotice := <-s.ctx.reload:
s.log.Info(
"notifying goroutines in response to reload signal",
zap.String("app", s.name),
)
for i := 0; i < goRoutineCount; i++ {
s.ctx.reloadRoutine <- reloadNotice
}
case exitNotice := <-s.ctx.exit:
s.log.Debug(
"notifying goroutines in response to graceful shutdown signal",
zap.String("app", s.name),
)
for i := 0; i < goRoutineCount; i++ {
s.ctx.exitRoutine <- exitNotice
s.log.Debug(
"notified goroutine in response to graceful shutdown signal",
zap.String("app", s.name),
zap.Int("routine_id", i),
)
}
default:
if goRoutineCount == 0 {
s.log.Info(
"all subsystems exited successfully",
zap.String("app", s.name),
)
break M
}
time.Sleep(500 * time.Millisecond)
}
}
return
}
func runSignalManager(s *Server) {
sysChannel := make(chan os.Signal, 1)
signal.Notify(sysChannel, os.Interrupt, syscall.SIGTERM)
var fn = s.name + "-signal-mgr"
s.log.Debug(
"starting sybsystem",
zap.String("subsystem", fn),
zap.String("app", s.name),
zap.Any("config", s.GetConfig()),
)
hardStopSeconds := time.Duration(10)
signalID := <-sysChannel
// TODO: implement reload
s.log.Debug(
"shutting down all subsystems in response to the received system",
zap.String("app", s.name),
zap.Duration("timeout", hardStopSeconds),
zap.String("signal_name", signalID.String()),
zap.Any("signal_id", signalID),
)
s.ctx.exit <- true
time.Sleep(time.Second * hardStopSeconds)
s.log.Warn(
"some subsystems did not shutdown gracefully",
zap.String("app", s.name),
)
os.Exit(1)
}