cmd/tip: move side health check to main health check. Use 503.

500 is considered to be healthy. Use 503 instead, so the instance is
considered unhealthy while the side is coming up.

Add some extra log statements that help debug instances from
application logs.

Fixes golang/go#13682.

Change-Id: I713c8c2fa75de4e275f632b999edc98cedd257bd
Reviewed-on: https://go-review.googlesource.com/18547
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
diff --git a/cmd/tip/godoc.yaml b/cmd/tip/godoc.yaml
index 93d0ec6..5f6d9dc 100644
--- a/cmd/tip/godoc.yaml
+++ b/cmd/tip/godoc.yaml
@@ -1,15 +1,18 @@
 module: tip
 runtime: custom
-api_version: go1
 vm: true
 
 automatic_scaling:
   min_num_instances: 1
   max_num_instances: 2
 
-handlers:
-- url: /.*
-  script: _go_app
-
 env_variables:
   TIP_BUILDER: 'godoc'
+
+health_check:
+  enable_health_check: True
+  check_interval_sec: 5
+  timeout_sec: 4
+  unhealthy_threshold: 2
+  healthy_threshold: 2
+  restart_threshold: 240
diff --git a/cmd/tip/talks.yaml b/cmd/tip/talks.yaml
index 5862759..edc90a5 100644
--- a/cmd/tip/talks.yaml
+++ b/cmd/tip/talks.yaml
@@ -1,15 +1,18 @@
 module: talks
 runtime: custom
-api_version: go1
 vm: true
 
 automatic_scaling:
   min_num_instances: 1
   max_num_instances: 5
 
-handlers:
-- url: /.*
-  script: _go_app
-
 env_variables:
   TIP_BUILDER: 'talks'
+
+health_check:
+  enable_health_check: True
+  check_interval_sec: 5
+  timeout_sec: 4
+  unhealthy_threshold: 2
+  healthy_threshold: 2
+  restart_threshold: 240
diff --git a/cmd/tip/tip.go b/cmd/tip/tip.go
index f9dad93..099b75e 100644
--- a/cmd/tip/tip.go
+++ b/cmd/tip/tip.go
@@ -47,6 +47,8 @@
 	http.Handle("/", p)
 	http.HandleFunc("/_ah/health", p.serveHealthCheck)
 
+	log.Print("Starting up")
+
 	if err := http.ListenAndServe(":8080", nil); err != nil {
 		p.stop()
 		log.Fatal(err)
@@ -90,14 +92,6 @@
 		http.Error(w, s, http.StatusInternalServerError)
 		return
 	}
-	if r.URL.Path == "/_ah/health" {
-		if err := p.builder.HealthCheck(p.hostport); err != nil {
-			http.Error(w, "Health check failde: "+err.Error(), http.StatusInternalServerError)
-			return
-		}
-		fmt.Fprintln(w, "OK")
-		return
-	}
 	proxy.ServeHTTP(w, r)
 }
 
@@ -110,8 +104,17 @@
 func (p *Proxy) serveHealthCheck(w http.ResponseWriter, r *http.Request) {
 	p.mu.Lock()
 	defer p.mu.Unlock()
+	// NOTE: Status 502, 503, 504 are the only status codes that signify an unhealthy app.
+	// So long as this handler returns one of those codes, this instance will not be sent any requests.
 	if p.proxy == nil {
-		http.Error(w, "not ready", 500)
+		log.Printf("Health check: not ready")
+		http.Error(w, "Not ready", http.StatusServiceUnavailable)
+		return
+	}
+
+	if err := p.builder.HealthCheck(p.hostport); err != nil {
+		log.Printf("Health check failed: %v", err)
+		http.Error(w, "Health check failed", http.StatusServiceUnavailable)
 		return
 	}
 	io.WriteString(w, "ok")