Skip to content

Commit

Permalink
add ready and liveness healthchecks and log dataset updates (#253)
Browse files Browse the repository at this point in the history
* log dataset updates

* add readiness/liveness checks

* tweak update complete messages
  • Loading branch information
gfr10598 authored May 31, 2019
1 parent ad7924f commit ea71ca0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions annotator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ env_variables:
# These should be substituted in the travis deployment script.
RELEASE_TAG: ${TRAVIS_TAG}
COMMIT_HASH: ${TRAVIS_COMMIT}

liveness_check:
path: "/live"

readiness_check:
path: "/ready"
10 changes: 1 addition & 9 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,9 @@ const (
func InitHandler() {
manager.MustUpdateDirectory()

// sets up any handlers that are needed, including url
// handlers and pubsub handlers
// sets up any handlers that are needed
http.HandleFunc("/annotate", Annotate)
http.HandleFunc("/batch_annotate", BatchAnnotate)

// DEPRECATED
// This code is disabled, to deal with a confusing pubsub subscription quota.
// It is no longer needed because Ya implemented an external cron trigger.
// This listens for pubsub messages about new downloader files, and loads them
// when they become available.
// go waitForDownloaderMessages()
}

// Annotate is a URL handler that looks up IP address and puts
Expand Down
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ func updateMaxmindDatasets(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}

func live(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

func ready(w http.ResponseWriter, r *http.Request) {
ann, _ := manager.GetAnnotator(time.Now())
if ann == nil {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}

func init() {
// Always prepend the filename and line number.
log.SetFlags(log.LstdFlags | log.Lshortfile)
Expand All @@ -72,6 +85,8 @@ func main() {

http.HandleFunc("/status", Status)
http.HandleFunc("/updateDatasets", updateMaxmindDatasets)
http.HandleFunc("/ready", ready)
http.HandleFunc("/live", live)

handler.InitHandler()
log.Print("Listening on port 8080")
Expand Down
8 changes: 8 additions & 0 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,38 @@ func (bldr *listBuilder) update() error {

var errV4, errV6, errG2, errAsnV4, errAsnV6 error

log.Println("Updating dataset directory")
wg := sync.WaitGroup{}
wg.Add(5)
go func() {
errV4 = bldr.legacyV4.UpdateCache()
log.Println("Legacy V4 loading done.")
wg.Done()
}()
go func() {
errV6 = bldr.legacyV6.UpdateCache()
log.Println("Legacy V6 loading done.")
wg.Done()
}()
go func() {
errG2 = bldr.geolite2.UpdateCache()
log.Println("Geolite2 loading done.")
wg.Done()
}()
go func() {
errAsnV4 = bldr.asnV4.UpdateCache()
log.Println("ASN V4 loading done.")
wg.Done()
}()
go func() {
errAsnV6 = bldr.asnV6.UpdateCache()
log.Println("ASN V6 loading done.")
wg.Done()
}()
wg.Wait()

log.Println("Dataset update complete.")

if errV4 != nil {
return errV4
}
Expand Down

0 comments on commit ea71ca0

Please sign in to comment.