queue.go 1.02 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package mic_c

import (
	"encoding/json"
	"git.hilo.cn/hilo-common/domain"
	"github.com/go-redis/redis/v8"
	"hilo-group/_const/redis_key/mic_k"
	"hilo-group/domain/event/mic_ev"
	"time"
)

// redis queue on mic
func QueueOnMic(model *domain.Model, onMic *mic_ev.OnMicEvent) error {
	queue := mic_k.GetOnMicQueue()
	onMic.Ts = time.Now().UnixNano() // 给一个时间戳
	body, _ := json.Marshal(onMic)
JiebinHu's avatar
JiebinHu committed
17
	return model.RedisCluster.RPush(model, queue, string(body)).Err()
hujiebin's avatar
hujiebin committed
18 19 20 21 22 23
}

// redis pop on mic
func BLPopQueueOnMic(model *domain.Model) *mic_ev.OnMicEvent {
	var res *mic_ev.OnMicEvent
	queue := mic_k.GetOnMicQueue()
JiebinHu's avatar
JiebinHu committed
24
	strs, err := model.RedisCluster.BLPop(model, time.Second, queue).Result()
hujiebin's avatar
hujiebin committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
	if err != nil {
		if err != redis.Nil {
			model.Log.Errorf("BLPopQueueOnMic fail:%v", err)
		}
		return nil
	}
	if len(strs) >= 2 {
		content := strs[1]
		res = new(mic_ev.OnMicEvent)
		if err := json.Unmarshal([]byte(content), res); err != nil {
			model.Log.Errorf("BLPopQueueOnMic json fail:%v", err)
			return nil
		}
		return res
	}
	return nil
}