mirror of
https://github.com/stefanprodan/podinfo.git
synced 2026-08-23 22:26:29 +00:00
long running job
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user