on_mic.go 2.51 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3
package mic_cron

import (
hujiebin's avatar
hujiebin committed
4
	"fmt"
hujiebin's avatar
hujiebin committed
5 6
	"git.hilo.cn/hilo-common/domain"
	"github.com/robfig/cron"
hujiebin's avatar
hujiebin committed
7
	"hilo-group/_const/redis_key/mic_k"
hujiebin's avatar
hujiebin committed
8
	"hilo-group/domain/cache/mic_c"
hujiebin's avatar
hujiebin committed
9 10
	"hilo-group/domain/event/mic_ev"
	"hilo-group/domain/model/group_m"
11
	"hilo-group/domain/model/mic_m"
hujiebin's avatar
hujiebin committed
12
	"hilo-group/domain/service/group_mic_s"
iamhujiebin's avatar
iamhujiebin committed
13
	"time"
hujiebin's avatar
hujiebin committed
14 15 16 17 18
)

func OnMicCheck() {
	c := cron.New()
	//一分钟执行一次
19
	spec := "30 */1 * * * ?"
hujiebin's avatar
hujiebin committed
20 21 22 23 24 25 26 27 28
	_ = c.AddFunc(spec, func() {
		//获取麦上的所有人
		var model = domain.CreateModelNil()
		groupIds, err := group_m.GetMicHasInGroups()
		if err != nil {
			model.Log.Errorf("cron micIn GetMicHasInGroups err:%v", err)
			return
		}
		for _, groupId := range groupIds {
hujiebin's avatar
hujiebin committed
29
			onGroupMicKey := mic_k.GetGroupOnMicUser(groupId) // 同步在mic上的人
hujiebin's avatar
hujiebin committed
30
			//遍历每个麦位
hujiebin's avatar
hujiebin committed
31
			for i := 1; i <= group_m.MaxMicNum; i++ {
hujiebin's avatar
hujiebin committed
32 33 34 35 36
				micUser, err := group_m.GetMicUser(model, groupId, i)
				if err != nil {
					model.Log.Errorf("cron micIn group_m.GetMicUser err:%v", err)
				}
				if micUser != nil {
hujiebin's avatar
hujiebin committed
37
					if err := mic_c.QueueOnMic(model, &mic_ev.OnMicEvent{
hujiebin's avatar
hujiebin committed
38 39 40 41 42 43 44
						GroupUuid:  micUser.GroupUuid,
						I:          micUser.I,
						ExternalId: micUser.ExternalId,
						UserId:     micUser.UserId,
						Forbid:     micUser.Forbid,
						Timestamp:  micUser.Timestamp,
					}); err != nil {
hujiebin's avatar
hujiebin committed
45
						model.Log.Errorf("QueueOnMic fail:%v", err)
hujiebin's avatar
hujiebin committed
46
					}
hujiebin's avatar
hujiebin committed
47 48 49 50 51
					// 设置mic上的人
					model.Redis.HSet(model, onGroupMicKey, i, micUser.UserId)
				} else {
					// 删除mic上的人
					model.Redis.HDel(model, onGroupMicKey, fmt.Sprintf("%d", i))
hujiebin's avatar
hujiebin committed
52 53 54 55 56 57
				}
			}
		}
	})
	c.Start()
}
hujiebin's avatar
hujiebin committed
58 59

func OnMicQueue() {
iamhujiebin's avatar
iamhujiebin committed
60 61 62 63 64
	for i := 0; i < 4; i++ {
		go func() {
			for true {
				model := domain.CreateModelNil()
				if onMic := mic_c.BLPopQueueOnMic(model); onMic != nil {
iamhujiebin's avatar
iamhujiebin committed
65
					start := time.Now()
iamhujiebin's avatar
iamhujiebin committed
66 67 68 69
					// 群组上麦经验
					if err := group_mic_s.NewGroupPowerService(model.MyContext).IncrGroupPowerOnMicExpAndTime(onMic.GroupUuid, onMic.UserId, onMic.Timestamp); err != nil {
						model.Log.Errorf("cron micIn GroupPowerOnMicExp err:%v", err)
					} else {
iamhujiebin's avatar
iamhujiebin committed
70
						model.Log.Infof("cron micIn GroupPowerOnMicExp success, groupId:%v, userId:%v,cost:%vs", onMic.GroupUuid, onMic.UserId, time.Now().Sub(start).Seconds())
iamhujiebin's avatar
iamhujiebin committed
71
					}
hujiebin's avatar
hujiebin committed
72

iamhujiebin's avatar
iamhujiebin committed
73
					// 用户上麦
JiebinHu's avatar
JiebinHu committed
74
					//if err := mic_m.IncrUserOnMic(model, onMic.UserId, onMic.Timestamp); err != nil {
JiebinHu's avatar
JiebinHu committed
75 76
					//if err := mic_m.IncrUserOnMicV2(model, onMic.UserId); err != nil {
					if err := mic_m.IncrUserOnMicV3(model, onMic.UserId); err != nil {
77 78 79
						model.Log.Errorf("cron micIn IncrUserOnMic err:%v", err)
					} else {
					}
iamhujiebin's avatar
iamhujiebin committed
80
				}
hujiebin's avatar
hujiebin committed
81
			}
iamhujiebin's avatar
iamhujiebin committed
82 83
		}()
	}
hujiebin's avatar
hujiebin committed
84
}