蛋蛋星球RabbitMq消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

82 lines
2.1 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. utils2 "applet/app/utils"
  5. "applet/app/utils/logx"
  6. "applet/consume/md"
  7. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  8. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  9. es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
  10. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  11. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  12. "encoding/json"
  13. "errors"
  14. "fmt"
  15. "github.com/streadway/amqp"
  16. "time"
  17. )
  18. func EggEnergyDealUserECPMConsume(queue md.MqQueue) {
  19. fmt.Println(">>>>>>>>>>>>EggEnergyDealUserECPMConsume>>>>>>>>>>>>")
  20. ch, err := rabbit.Cfg.Pool.GetChannel()
  21. if err != nil {
  22. logx.Error(err)
  23. return
  24. }
  25. defer ch.Release()
  26. //1、将自己绑定到交换机上
  27. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  28. //2、取出数据进行消费
  29. ch.Qos(1)
  30. delivery := ch.Consume(queue.Name, false)
  31. egg_system_rules.Init(cfg.RedisAddr)
  32. var res amqp.Delivery
  33. var ok bool
  34. for {
  35. res, ok = <-delivery
  36. if ok == true {
  37. err = handleEggEnergyDealUserECPMConsume(res.Body)
  38. if err != nil {
  39. fmt.Println("EggEnergyDealUserECPMConsume_ERR:::::", err.Error())
  40. utils2.FilePutContents("EggEnergyDealUserECPMConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  41. "body": res.Body,
  42. "err": err.Error(),
  43. }))
  44. }
  45. //_ = res.Reject(false)
  46. err = res.Ack(true)
  47. fmt.Println("err ::: ", err)
  48. } else {
  49. panic(errors.New("error getting message"))
  50. }
  51. }
  52. }
  53. func handleEggEnergyDealUserECPMConsume(msgData []byte) error {
  54. //1、解析mq中queue的数据结构体
  55. var msg *md2.DealUserEcpmReq
  56. err := json.Unmarshal(msgData, &msg)
  57. if err != nil {
  58. return err
  59. }
  60. //2、更新用户信息
  61. year, week := time.Now().ISOWeek()
  62. yearStr := utils2.IntToStr(year)
  63. weekStr := utils2.IntToStr(week)
  64. index := es2.GetAppointIndexFromAlias(yearStr, weekStr)
  65. id := fmt.Sprintf("%d%d-%d", year, week, msg.Uid)
  66. m := md2.EggEnergyUserEggScoreEs{
  67. Ecpm: utils2.StrToFloat64(msg.Ecpm),
  68. }
  69. updateDocRet, err := es.UpdateDoc(index, id, m)
  70. if err != nil {
  71. return err
  72. }
  73. fmt.Printf("UpdateCreateDoc ==> %+v \n\n", updateDocRet)
  74. return err
  75. }