-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskQueueHandler.go
53 lines (40 loc) · 1.38 KB
/
taskQueueHandler.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
package handler
import (
"errors"
"fmt"
"time"
"net/http"
"github.com/fengjian0106/gomgo/appcontext"
"github.com/fengjian0106/gomgo/taskqueue"
)
func ReqRepTaskHandler(appCtx *appcontext.AppContext, w http.ResponseWriter, r *http.Request) (int, error) {
msg := r.FormValue("msg")
if msg == "" {
return http.StatusBadRequest, &ApiError{ApiErrorZmqMsgNotFound, errors.New("no msg")}
}
task := &taskqueue.ReqRepTask{Msg: msg, ResultChan: make(chan string)}
appCtx.ReqRepTaskQueue <- task
var reply string
select {
case reply = <-task.ResultChan:
//fmt.Println("Received ", reply)
case <-time.After(time.Second * 2):
return http.StatusInternalServerError, &ApiError{ApiErrorTaskQueueRequestTimeout, errors.New("task queue request timeout")}
}
w.Header().Set("Content-Type", "application/json")
jsonStr := fmt.Sprintf("{\"retMsg\": \"%s\"}", reply)
w.Write([]byte(jsonStr))
return http.StatusOK, nil
}
func PubTaskHandler(appCtx *appcontext.AppContext, w http.ResponseWriter, r *http.Request) (int, error) {
msg := r.FormValue("msg")
if msg == "" {
return http.StatusBadRequest, &ApiError{ApiErrorZmqMsgNotFound, errors.New("no msg")}
}
task := &taskqueue.PubTask{Msg: msg}
appCtx.PubTaskQueue <- task
w.Header().Set("Content-Type", "application/json")
jsonStr := fmt.Sprintf("{\"retMsg\": \"%s\"}", "publish success")
w.Write([]byte(jsonStr))
return http.StatusOK, nil
}