-
Notifications
You must be signed in to change notification settings - Fork 16
/
jobhandler.go
103 lines (90 loc) · 2.55 KB
/
jobhandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/isucon/isucon6-final/portal/job"
)
// serveQueueJob は参加者がベンチマーカのジョブをキューに挿入するエンドポイント。
func serveQueueJob(w http.ResponseWriter, req *http.Request) error {
if req.Method != http.MethodPost {
return errHTTP(http.StatusMethodNotAllowed)
}
// 18時になったらコンテスト終了なのでジョブを挿入させない
switch getContestStatus() {
case contestStatusNotStarted:
return errHTTPMessage{http.StatusForbidden, "Final has not started yet"}
case contestStatusEnded:
return errHTTPMessage{http.StatusForbidden, "Final has finished"}
}
team, err := loadTeamFromSession(req)
if err != nil {
return err
}
if team == nil {
return errHTTP(http.StatusForbidden)
}
if team.IPAddr == "" {
return errHTTP(http.StatusBadRequest)
}
err = enqueueJob(team.ID)
if err != nil {
if _, ok := err.(errAlreadyQueued); ok {
// ユーザに教えてあげる
return serveIndexWithMessage(w, req, "Job already queued")
}
return err
}
// TODO(motemen): flash
http.Redirect(w, req, "/", http.StatusFound)
return nil
}
// 新しいジョブを取り出す。ジョブが無い場合は 204 を返す
// クライアントは定期的(3秒おきくらい)にリクエストしてジョブを確認する
func serveNewJob(w http.ResponseWriter, req *http.Request) error {
if req.Method != http.MethodPost {
return errHTTP(http.StatusMethodNotAllowed)
}
benchNode := req.FormValue("bench_node")
j, err := dequeueJob(benchNode)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return nil
}
if j == nil {
w.WriteHeader(http.StatusNoContent)
return nil
}
j.URLs, err = getProxyURLs(j.TeamID)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return nil
}
w.Header().Set("Content-Type", "application/json")
b, _ := json.Marshal(j)
w.Write(b)
return nil
}
func servePostResult(w http.ResponseWriter, req *http.Request) error {
if req.Method != http.MethodPost {
http.Error(w, "Method Not Allowd", http.StatusMethodNotAllowed)
return nil
}
var res job.Result
if req.Body == nil {
http.Error(w, "Please send a request body", http.StatusBadRequest)
return nil
}
err := json.NewDecoder(req.Body).Decode(&res)
if err != nil {
http.Error(w, err.Error(), 400)
return nil
}
err = doneJob(&res)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"success":true}`)
return nil
}