-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhealth-check.go
47 lines (40 loc) · 1.27 KB
/
health-check.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
package main
import (
"context"
"io"
"net/http"
"os"
"os/signal"
"time"
"github.com/bradleyfalzon/gopherci/internal/logger"
)
// shuttingDown tracks whether this instance of GopherCI is shutting down
// it's written to by the SignalHandler and read by HealthCheckHandler
var shuttingDown bool
// SignalHandler listens for a shutdown signal and calls cancel, if
// multiple signals are received in short succession, forcible quit.
func SignalHandler(logger logger.Logger, cancel context.CancelFunc, srv *http.Server) {
// chan size 2 as multiple interrupts is force quit (supports ^C for dev)
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt)
var lastSignal time.Time
for {
s := <-c
if time.Since(lastSignal) < time.Second {
logger.Fatal("Two signals in short succession, forcing quit")
}
lastSignal = time.Now()
logger.Infof("Received %v, preparing to shutdown", s)
shuttingDown = true
srv.Shutdown(context.Background())
cancel()
}
}
// HealthCheckHandler checks whether the instance is shutting down, and if so,
// responds with 503 Service Unavailable.
func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
if shuttingDown {
http.Error(w, "Service shutting down", http.StatusServiceUnavailable)
}
io.WriteString(w, "Service OK")
}