package consume import ( "applet/app/cfg" "applet/app/db" "applet/app/utils" "applet/app/utils/logx" "applet/consume/md" md2 "applet/es/md" "code.fnuoos.com/EggPlanet/egg_system_rules.git" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule" "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "encoding/json" "errors" "fmt" "github.com/streadway/amqp" "time" ) func EggCanalUserConsume(queue md.MqQueue) { fmt.Println(">>>>>>>>>>>>EggCanalUserConsume>>>>>>>>>>>>") ch, err := rabbit.Cfg.Pool.GetChannel() if err != nil { logx.Error(err) return } defer ch.Release() //1、将自己绑定到交换机上 ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey) //2、取出数据进行消费 ch.Qos(1) delivery := ch.Consume(queue.Name, false) egg_system_rules.Init(cfg.RedisAddr) var res amqp.Delivery var ok bool for { res, ok = <-delivery if ok == true { err = handleEggCanalUserConsume(res.Body, ch) err = res.Ack(true) fmt.Println("err ::: ", err) } else { panic(errors.New("error getting message")) } } fmt.Println("get msg done") } func handleEggCanalUserConsume(msg []byte, ch *rabbit.Channel) error { time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒 var canalMsg *md.CanalUserMessage[md.CanalUser] err := json.Unmarshal(msg, &canalMsg) if err != nil { fmt.Println("EggCanalInviteUserNumsConsumeFaliedUnMarshal_ERR:::::", err.Error()) return nil } if canalMsg.Type == md2.CanalMsgInsertSqlType || canalMsg.Type == md2.CanalMsgUpdateSqlType { oldUser := make(map[string]md.CanalUser) for _, item := range canalMsg.Old { oldUser[item.Id] = item } for _, item := range canalMsg.Data { if utils.StrToInt(item.ParentUid) == 0 { continue } count := rule.ExtendUserCount(db.Db, utils.StrToInt(item.ParentUid)) if count > 1000 { msg1 := md.CommUserId{ Uid: item.ParentUid, } ch.Publish("egg.app", msg1, "egg_slow_auto_up_lv") continue } rule.UserUpgradeInsert(db.Db, utils.StrToInt(item.ParentUid)) } } return nil }