-
Notifications
You must be signed in to change notification settings - Fork 2
/
soma_handler_grim_reaper.go
145 lines (127 loc) · 3.25 KB
/
soma_handler_grim_reaper.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
package main
import (
"database/sql"
"fmt"
"log"
"os"
"strings"
"sync"
"time"
"github.com/1and1/soma/internal/msg"
"github.com/1and1/soma/lib/proto"
)
type grimReaper struct {
system chan msg.Request
conn *sql.DB
}
func (grim *grimReaper) run() {
// defer calls stack in LIFO order
defer os.Exit(0)
defer grim.conn.Close()
var res bool
lock := sync.Mutex{}
runloop:
for {
select {
case req := <-grim.system:
// this is mainly so the go runtime does not optimize
// away waiting for the shutdown routine
lock.Lock()
go func() {
res = grim.process(&req)
lock.Unlock()
}()
}
break runloop
}
// blocks until the go routine has unlocked the mutex
lock.Lock()
if !res {
lock.Unlock()
goto runloop
}
time.Sleep(time.Duration(SomaCfg.ShutdownDelay) * time.Second)
log.Println("grimReaper: shutdown complete")
}
func (grim *grimReaper) process(q *msg.Request) bool {
result := msg.Result{Type: `grimReaper`, Action: q.Action,
System: []proto.SystemOperation{}}
switch q.Action {
case `shutdown`:
default:
result.NotImplemented(
fmt.Errorf("Unknown requested action: %s",
q.Action),
)
q.Reply <- result
return false
}
// tell HTTP handlers to start turning people away
ShutdownInProgress = true
// answer shutdown request
result.OK()
q.Reply <- result
time.Sleep(time.Duration(SomaCfg.ShutdownDelay) * time.Second)
// I have awoken.
log.Println(`GRIM REAPER ACTIVATED. SYSTEM SHUTDOWN INITIATED`)
// stop all treeKeeper : /^repository_.*/
for handler, _ := range handlerMap {
if strings.HasPrefix(handler, `repository_`) {
handlerMap[handler].(Stopper).stopNow()
}
}
// shutdown all treeKeeper : /^repository_.*/
for handler, _ := range handlerMap {
if strings.HasPrefix(handler, `repository_`) {
handlerMap[handler].(Downer).shutdownNow()
delete(handlerMap, handler)
log.Printf("grimReaper: shut down %s", handler)
}
}
// shutdown all write handler: /WriteHandler$/
for handler, _ := range handlerMap {
if !strings.HasSuffix(handler, `WriteHandler`) {
continue
}
handlerMap[handler].(Downer).shutdownNow()
delete(handlerMap, handler)
log.Printf("grimReaper: shut down %s", handler)
}
// shutdown all read handler : /ReadHandler$/
for handler, _ := range handlerMap {
if !(strings.HasSuffix(handler, `ReadHandler`) ||
strings.HasSuffix(handler, `_r`)) {
continue
}
handlerMap[handler].(Downer).shutdownNow()
delete(handlerMap, handler)
log.Printf("grimReaper: shut down %s", handler)
}
// shutdown special handlers
for _, h := range []string{
`jobDelay`,
`forestCustodian`,
`guidePost`,
`lifeCycle`,
`deploymentHandler`,
`hostDeploymentHandler`,
} {
handlerMap[h].(Downer).shutdownNow()
delete(handlerMap, h)
log.Printf("grimReaper: shut down %s", h)
}
// shutdown supervisor -- needs handling in BasicAuth()
handlerMap[`supervisor`].(Downer).shutdownNow()
delete(handlerMap, `supervisor`)
log.Println(`grimReaper: shut down the supervisor`)
// log what we have missed
log.Println(`grimReaper: checking for still running handlers`)
for name, _ := range handlerMap {
if name == `grimReaper` {
continue
}
log.Printf("grimReaper: %s is still running\n", name)
}
return true
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix