-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandlers.go
107 lines (95 loc) · 2.93 KB
/
handlers.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
104
105
106
107
package q
import (
"net/http"
"strconv"
"strings"
"zliu.org/goutil/rest"
)
var zlog = rest.Log()
type QServer struct {
queue *Queue
}
func NewQServer(path string) (*QServer, error) {
queue, err := NewQueue(path)
if err != nil {
return nil, err
}
return &QServer{queue: queue}, nil
}
func (qs *QServer) Close() {
qs.queue.Close()
}
// 创建任务
func (qs *QServer) CreateTaskHandler(w http.ResponseWriter, r *http.Request) {
zlog.Info().Str("addr", r.RemoteAddr).Str("method", r.Method).Str("host", r.Host).Str("uri", r.RequestURI).Msg("create task")
r.ParseForm()
data := strings.TrimSpace(r.FormValue("data"))
if data == "" {
rest.ErrBadRequest(w, "data is empty")
return
}
if err := qs.queue.Enqueue(data); err != nil {
rest.ErrInternalServer(w, err)
return
}
rest.MustEncode(w, map[string]string{"status": "ok"})
}
// 获取任务
func (qs *QServer) GetTaskHandler(w http.ResponseWriter, r *http.Request) {
zlog.Info().Str("addr", r.RemoteAddr).Str("method", r.Method).Str("host", r.Host).Str("uri", r.RequestURI).Msg("get task")
r.ParseForm()
t := strings.TrimSpace(r.FormValue("timeout"))
timeout, err := strconv.ParseInt(t, 10, 64)
if err != nil {
rest.ErrBadRequest(w, "invalid timeout")
return
}
key, value, err := qs.queue.Dequeue(timeout)
if err != nil {
if err.Error() == "Queue is empty" {
rest.ErrorMessageWithStatus(w, err.Error(), http.StatusNotFound)
} else {
rest.ErrInternalServer(w, err.Error())
}
return
}
rest.MustEncode(w, map[string]string{"key": key, "value": value})
}
// 更新任务状态
func (qs *QServer) UpdateTaskStatusHandler(w http.ResponseWriter, r *http.Request) {
zlog.Info().Str("addr", r.RemoteAddr).Str("method", r.Method).Str("host", r.Host).Str("uri", r.RequestURI).Msg("update task status")
key := r.PathValue("key")
if key == "" {
rest.ErrBadRequest(w, "empty key")
return
}
if err := qs.queue.Confirm(key); err != nil {
if strings.HasPrefix(err.Error(), "key not found") {
rest.ErrorMessageWithStatus(w, err.Error(), http.StatusNotFound)
} else {
rest.ErrInternalServer(w, err)
}
return
}
rest.MustEncode(w, map[string]string{"status": "ok"})
}
// 获取队列状态
func (qs *QServer) GetQueueStatusHandler(w http.ResponseWriter, r *http.Request) {
zlog.Info().Str("addr", r.RemoteAddr).Str("method", r.Method).Str("host", r.Host).Str("uri", r.RequestURI).Msg("get queue status")
rest.MustEncode(w, qs.queue.Status())
}
// 获取下一个任务预览
func (qs *QServer) GetNextTaskHandler(w http.ResponseWriter, r *http.Request) {
zlog.Info().Str("addr", r.RemoteAddr).Str("method", r.Method).Str("host", r.Host).Str("uri", r.RequestURI).Msg("get next task")
r.ParseForm()
value, err := qs.queue.Peek()
if err != nil {
if err.Error() == "Queue is empty" {
rest.ErrorMessageWithStatus(w, err.Error(), http.StatusNotFound)
} else {
rest.ErrInternalServer(w, err.Error())
}
return
}
rest.MustEncode(w, map[string]string{"value": value})
}