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

import (
	"git.hilo.cn/hilo-common/domain"
	"github.com/robfig/cron"
hujiebin's avatar
hujiebin committed
6
	"hilo-group/domain/cache/mic_c"
hujiebin's avatar
hujiebin committed
7 8
	"hilo-group/domain/event/mic_ev"
	"hilo-group/domain/model/group_m"
hujiebin's avatar
hujiebin committed
9 10
	"hilo-group/domain/model/mic_m"
	"hilo-group/domain/service/group_mic_s"
hujiebin's avatar
hujiebin committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
)

func OnMicCheck() {
	c := cron.New()
	//一分钟执行一次
	spec := "0 */1 * * * ?"
	_ = 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
27
			for i := 1; i <= group_m.MaxMicNum; i++ {
hujiebin's avatar
hujiebin committed
28 29 30 31 32
				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
33
					if err := mic_c.QueueOnMic(model, &mic_ev.OnMicEvent{
hujiebin's avatar
hujiebin committed
34 35 36 37 38 39 40
						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
41
						model.Log.Errorf("QueueOnMic fail:%v", err)
hujiebin's avatar
hujiebin committed
42 43 44 45 46 47 48
					}
				}
			}
		}
	})
	c.Start()
}
hujiebin's avatar
hujiebin committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

func OnMicQueue() {
	go func() {
		for true {
			model := domain.CreateModelNil()
			if onMic := mic_c.BLPopQueueOnMic(model); onMic != nil {
				// 群组上麦经验
				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 {
					model.Log.Infof("cron micIn GroupPowerOnMicExp success, groupId:%v, userId:%v", onMic.GroupUuid, onMic.UserId)
				}

				// 用户上麦
				if err := mic_m.IncrUserOnMic(model, onMic.UserId, onMic.Timestamp); err != nil {
					model.Log.Errorf("cron micIn IncrUserOnMic err:%v", err)
				} else {
					model.Log.Infof("cron micIn IncrUserOnMic success,userId:%v", onMic.UserId)
				}
			}
		}
	}()
}