long running job

This commit is contained in:
Stefan Prodan
2018-01-27 18:37:42 +02:00
parent 2aeb627fdd
commit 46862ae319
3 changed files with 39 additions and 0 deletions
+2
View File
@@ -25,6 +25,8 @@ Web API:
* `POST /readyz/enable` signals the Kubernetes LB that this instance is ready to receive traffic
* `POST /readyz/disable` signals the Kubernetes LB to stop sending requests to this instance
* `GET /panic` crashes the process with exit code 255
* `POST /echo` echos the posted content
* `POST /job` long running job, json body: `{"wait":2}`
### Deployment
+36
View File
@@ -8,6 +8,8 @@ import (
"github.com/golang/glog"
"github.com/stefanprodan/k8s-podinfo/pkg/version"
"gopkg.in/yaml.v2"
"encoding/json"
"time"
)
func (s *Server) index(w http.ResponseWriter, r *http.Request) {
@@ -53,6 +55,40 @@ func (s *Server) echo(w http.ResponseWriter, r *http.Request) {
}
}
func (s *Server) job(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
body, err := ioutil.ReadAll(r.Body)
if err != nil {
glog.Errorf("Reading the request body failed: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
glog.Infof("Payload received from %s: %s", r.RemoteAddr, string(body))
job := struct{
Wait int `json:"wait"`
}{
Wait: 0,
}
err = json.Unmarshal(body, &job)
if err != nil {
glog.Errorf("Reading the request body failed: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
if job.Wait > 0 {
time.Sleep(time.Duration(job.Wait) * time.Second)
}
w.WriteHeader(http.StatusAccepted)
w.Write([]byte("Job done"))
default:
w.WriteHeader(http.StatusNotAcceptable)
}
}
func (s *Server) version(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/version" {
w.WriteHeader(http.StatusNotFound)
+1
View File
@@ -33,6 +33,7 @@ func NewServer(options ...func(*Server)) *Server {
s.mux.HandleFunc("/readyz/enable", s.enable)
s.mux.HandleFunc("/readyz/disable", s.disable)
s.mux.HandleFunc("/echo", s.echo)
s.mux.HandleFunc("/job", s.job)
s.mux.HandleFunc("/panic", s.panic)
s.mux.HandleFunc("/version", s.version)
s.mux.Handle("/metrics", promhttp.Handler())