1
0
mirror of https://github.com/newnius/YAO-scheduler.git synced 2025-06-07 22:31:55 +00:00
YAO-scheduler/src/main.go

111 lines
2.2 KiB
Go
Raw Normal View History

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")
2019-04-16 07:33:37 +00:00
msgSubmit := MsgSubmit{Code: 0}
2019-03-20 03:14:07 +00:00
err := json.Unmarshal([]byte(string(r.PostFormValue("job"))), &job)
if err != nil {
2019-04-16 07:33:37 +00:00
msgSubmit.Code = 1
msgSubmit.Error = err.Error()
} else {
allocator.schedule(job)
2019-03-20 03:14:07 +00:00
}
2019-04-16 07:33:37 +00:00
js, _ := json.Marshal(msgSubmit)
2019-03-04 09:19:55 +00:00
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-04-12 09:21:09 +00:00
case "summary":
fmt.Println("summary")
js, _ := json.Marshal(allocator.summary())
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-04-16 08:59:19 +00:00
pool.nodes = make(map[int]NodeStatus)
2019-03-20 03:14:07 +00:00
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)
}
}