diff options
| author | Jonathan Amsterdam <jba@google.com> | 2020-09-04 09:58:08 -0400 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2020-09-11 22:16:16 +0000 |
| commit | 2f89a00a45a438e962e362238dcdd1987a8254b6 (patch) | |
| tree | 4a95d2683f62ad5162c8e092853ee70cf2477d82 | |
| parent | 4be2e9ddc13f0f7ce5dc1a0facc46dbf258dc5b4 (diff) | |
| download | go-x-pkgsite-2f89a00a45a438e962e362238dcdd1987a8254b6.tar.xz | |
internal/worker: add health check endpoint
This will be used with GKE health checks. The health checks
hit "/" by default, which causes unnecessary work.
Change-Id: I6093e5985102153bbabeb19feebbae1a44d55a9f
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/254378
Reviewed-by: Julie Qiu <julie@golang.org>
| -rw-r--r-- | internal/database/database.go | 4 | ||||
| -rw-r--r-- | internal/worker/server.go | 11 |
2 files changed, 15 insertions, 0 deletions
diff --git a/internal/database/database.go b/internal/database/database.go index fa82e6ce..bff5b08c 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -55,6 +55,10 @@ func New(db *sql.DB, instanceID string) *DB { return &DB{db: db, instanceID: instanceID} } +func (db *DB) Ping() error { + return db.db.Ping() +} + func (db *DB) InTransaction() bool { return db.tx != nil } diff --git a/internal/worker/server.go b/internal/worker/server.go index 9622dbf3..3444beab 100644 --- a/internal/worker/server.go +++ b/internal/worker/server.go @@ -204,6 +204,9 @@ func (s *Server) Install(handle func(string, http.Handler)) { // returns an HTML page displaying information about recent versions that were processed. handle("/versions", http.HandlerFunc(s.handleHTMLPage(s.doVersionsPage))) + // Health check. + handle("/healthz", http.HandlerFunc(s.handleHealthCheck)) + // returns an HTML page displaying the homepage. handle("/", http.HandlerFunc(s.handleHTMLPage(s.doIndexPage))) } @@ -521,6 +524,14 @@ func (s *Server) updateExperiment(w http.ResponseWriter, r *http.Request) error return nil } +func (s *Server) handleHealthCheck(w http.ResponseWriter, r *http.Request) { + if err := s.db.Underlying().Ping(); err != nil { + http.Error(w, fmt.Sprintf("DB ping failed: %v", err), http.StatusInternalServerError) + return + } + fmt.Fprintln(w, "OK") +} + // Parse the template for the status page. func parseTemplate(staticPath, filename template.TrustedSource) (*template.Template, error) { if staticPath.String() == "" { |
