2019-03-04 09:19:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"net/http"
|
|
|
|
"log"
|
|
|
|
"encoding/json"
|
2019-03-20 03:14:07 +00:00
|
|
|
"fmt"
|
2019-03-04 09:19:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var addr = flag.String("addr", ":8080", "http service address")
|
|
|
|
|
|
|
|
var pool *ResourcePool
|
|
|
|
|
2019-03-20 03:14:07 +00:00
|
|
|
var allocator *AllocatorFIFO
|
|
|
|
|
2019-03-04 09:19:55 +00:00
|
|
|
func serverAPI(w http.ResponseWriter, r *http.Request) {
|
2019-03-20 03:14:07 +00:00
|
|
|
var nodes []int
|
2019-03-04 09:19:55 +00:00
|
|
|
for id := range pool.nodes {
|
|
|
|
nodes = append(nodes, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.URL.Query().Get("action") {
|
2019-03-20 03:14:07 +00:00
|
|
|
case "node_gets":
|
2019-03-04 09:19:55 +00:00
|
|
|
js, _ := json.Marshal(nodes)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
break
|
2019-03-20 03:14:07 +00:00
|
|
|
|
2019-03-04 09:19:55 +00:00
|
|
|
case "resource_get_by_node":
|
|
|
|
id := str2int(r.URL.Query().Get("id"), -1)
|
|
|
|
js, _ := json.Marshal(pool.getByID(id))
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
break
|
2019-03-20 03:14:07 +00:00
|
|
|
|
2019-03-04 09:19:55 +00:00
|
|
|
case "job_submit":
|
2019-03-20 03:14:07 +00:00
|
|
|
var job Job
|
|
|
|
fmt.Println("job_submit")
|
|
|
|
err := json.Unmarshal([]byte(string(r.PostFormValue("job"))), &job)
|
|
|
|
if err != nil {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
allocator.schedule(job)
|
2019-03-04 09:19:55 +00:00
|
|
|
js, _ := json.Marshal(nodes)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
break
|
2019-03-20 03:14:07 +00:00
|
|
|
|
|
|
|
case "job_status":
|
|
|
|
fmt.Println("job_status")
|
|
|
|
js, _ := json.Marshal(allocator.status(r.URL.Query().Get("id")))
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
break
|
|
|
|
|
|
|
|
case "task_logs":
|
|
|
|
fmt.Println("task_logs")
|
|
|
|
fmt.Println(r.URL.Query().Get("id"))
|
2019-03-25 07:36:30 +00:00
|
|
|
js, _ := json.Marshal(allocator.logs(r.URL.Query().Get("job"), r.URL.Query().Get("task")))
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
break
|
|
|
|
|
|
|
|
case "jobs":
|
|
|
|
fmt.Println("job_list")
|
|
|
|
js, _ := json.Marshal(allocator.listJobs())
|
2019-03-20 03:14:07 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(js)
|
|
|
|
break
|
2019-03-04 09:19:55 +00:00
|
|
|
default:
|
|
|
|
http.Error(w, "Not Found", http.StatusNotFound)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
pool = &ResourcePool{}
|
2019-03-20 03:14:07 +00:00
|
|
|
pool.nodes = make(map[int][]NodeStatus)
|
|
|
|
|
|
|
|
allocator = &AllocatorFIFO{}
|
|
|
|
allocator.start()
|
|
|
|
|
2019-03-04 09:19:55 +00:00
|
|
|
go func() {
|
|
|
|
start(pool)
|
|
|
|
}()
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
serverAPI(w, r)
|
|
|
|
})
|
|
|
|
|
|
|
|
err := http.ListenAndServe(*addr, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("ListenAndServe: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|