Skip to content

Commit ea71ca0

Browse files
authored
add ready and liveness healthchecks and log dataset updates (#253)
* log dataset updates * add readiness/liveness checks * tweak update complete messages
1 parent ad7924f commit ea71ca0

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

annotator.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ env_variables:
4343
# These should be substituted in the travis deployment script.
4444
RELEASE_TAG: ${TRAVIS_TAG}
4545
COMMIT_HASH: ${TRAVIS_COMMIT}
46+
47+
liveness_check:
48+
path: "/live"
49+
50+
readiness_check:
51+
path: "/ready"

handler/handler.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,9 @@ const (
3131
func InitHandler() {
3232
manager.MustUpdateDirectory()
3333

34-
// sets up any handlers that are needed, including url
35-
// handlers and pubsub handlers
34+
// sets up any handlers that are needed
3635
http.HandleFunc("/annotate", Annotate)
3736
http.HandleFunc("/batch_annotate", BatchAnnotate)
38-
39-
// DEPRECATED
40-
// This code is disabled, to deal with a confusing pubsub subscription quota.
41-
// It is no longer needed because Ya implemented an external cron trigger.
42-
// This listens for pubsub messages about new downloader files, and loads them
43-
// when they become available.
44-
// go waitForDownloaderMessages()
4537
}
4638

4739
// Annotate is a URL handler that looks up IP address and puts

main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ func updateMaxmindDatasets(w http.ResponseWriter, r *http.Request) {
5353
w.Write([]byte("OK"))
5454
}
5555

56+
func live(w http.ResponseWriter, r *http.Request) {
57+
w.WriteHeader(http.StatusOK)
58+
}
59+
60+
func ready(w http.ResponseWriter, r *http.Request) {
61+
ann, _ := manager.GetAnnotator(time.Now())
62+
if ann == nil {
63+
w.WriteHeader(http.StatusServiceUnavailable)
64+
return
65+
}
66+
w.WriteHeader(http.StatusOK)
67+
}
68+
5669
func init() {
5770
// Always prepend the filename and line number.
5871
log.SetFlags(log.LstdFlags | log.Lshortfile)
@@ -72,6 +85,8 @@ func main() {
7285

7386
http.HandleFunc("/status", Status)
7487
http.HandleFunc("/updateDatasets", updateMaxmindDatasets)
88+
http.HandleFunc("/ready", ready)
89+
http.HandleFunc("/live", live)
7590

7691
handler.InitHandler()
7792
log.Print("Listening on port 8080")

manager/manager.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,38 @@ func (bldr *listBuilder) update() error {
130130

131131
var errV4, errV6, errG2, errAsnV4, errAsnV6 error
132132

133+
log.Println("Updating dataset directory")
133134
wg := sync.WaitGroup{}
134135
wg.Add(5)
135136
go func() {
136137
errV4 = bldr.legacyV4.UpdateCache()
138+
log.Println("Legacy V4 loading done.")
137139
wg.Done()
138140
}()
139141
go func() {
140142
errV6 = bldr.legacyV6.UpdateCache()
143+
log.Println("Legacy V6 loading done.")
141144
wg.Done()
142145
}()
143146
go func() {
144147
errG2 = bldr.geolite2.UpdateCache()
148+
log.Println("Geolite2 loading done.")
145149
wg.Done()
146150
}()
147151
go func() {
148152
errAsnV4 = bldr.asnV4.UpdateCache()
153+
log.Println("ASN V4 loading done.")
149154
wg.Done()
150155
}()
151156
go func() {
152157
errAsnV6 = bldr.asnV6.UpdateCache()
158+
log.Println("ASN V6 loading done.")
153159
wg.Done()
154160
}()
155161
wg.Wait()
156162

163+
log.Println("Dataset update complete.")
164+
157165
if errV4 != nil {
158166
return errV4
159167
}

0 commit comments

Comments
 (0)