Ejemplo n.º 1
0
func (h *RestQueueHandler) Get(ctx *web.Context, val string) {
	queue := redisq.NewRedisQueue(h.redis, val)
	if !queue.Exists() {
		writeError(ctx, 404, QueueNotFound)
		return
	}
	if queue.Empty() {
		writeError(ctx, 400, EmptyQueue)
		return
	}
	mesg, err := queue.GetNoWait()
	if err != nil {
		writeError(ctx, 500, GetError)
		if Settings.Debug {
			debug := fmt.Sprintf("Debug: %s", err)
			ctx.WriteString(debug)
		}
		h.logger.Printf("Dequeue from <%s> Error:%s", val, err)
		return
	}
	resp, _ := json.Marshal(mesg)
	//mesg.(type) is iteface{}
	//resp.(type) is []byte

	ctx.SetHeader("Content-Type", "application/json; charset=UTF-8", true)
	ctx.WriteString(string(resp))

}
Ejemplo n.º 2
0
func (h *RestQueueHandler) Put(ctx *web.Context, val string) {
	queue := redisq.NewRedisQueue(h.redis, val)
	if !queue.Exists() {
		h.logger.Printf("Queue [%s] didn't existst, will be ceated.", val)
	}
	if mesg, ok := ctx.Params["value"]; ok {
		var i interface{}
		err := json.Unmarshal([]byte(mesg), &i)
		if err != nil {
			writeError(ctx, 400, JsonDecodeError)
			return
		}
		err = queue.Put(i)
		if err != nil {
			writeError(ctx, 500, PostError)
			if Settings.Debug {
				debug := fmt.Sprintf("Debug: %s", err)
				ctx.WriteString(debug)
			}
			h.logger.Printf("Put message into [%s] Error:%s", val, err)
			return
		}
		h.logger.Printf("Put message into queue [%s]", val)

	} else {
		writeError(ctx, 400, LackPostValue)

	}

}
Ejemplo n.º 3
0
func (h *RestQueueHandler) Clear(ctx *web.Context, val string) {
	queue := redisq.NewRedisQueue(h.redis, val)
	if !queue.Exists() {
		writeError(ctx, 404, QueueNotFound)
		return
	}
	err := queue.Clear()
	if err != nil {
		writeError(ctx, 500, ClearError)
		if Settings.Debug {
			debug := fmt.Sprintf("Debug: %s", err)
			ctx.WriteString(debug)
		}
		h.logger.Printf("Delete queue [%s] Error:%s", val, err)
		return
	}
	h.logger.Printf("Queue [%s] deleted sucess", val)
}
Ejemplo n.º 4
0
func (wsh *WSQueueHandler) Consumer(ctx *web.Context, val string) {

	ws, err := wsh.handshake(ctx.Request, ctx.ResponseWriter)
	if err != nil {
		writeError(ctx, 400, WebSocketConnError)
	}

	queue := redisq.NewRedisQueue(wsh.redis, val)
	if !queue.Exists() {
		ws.WriteMessage(websocket.OpText, []byte(QueueNotFound))
		ws.WriteMessage(websocket.OpClose, []byte{})
		return
	}

	wsh.logger.Printf("Get websocket connection from %s", ws.RemoteAddr())
	wsh.logger.Printf("<%s> begin subscribe queue [%s]", ws.RemoteAddr(), val)

	c := WebSocketConn{ws, queue, wsh.logger}
	go c.writePump()
	// go c.readPump()

}