蛋蛋星球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.
 
 
 
 

126 lines
3.5 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. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
  11. md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  12. es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
  13. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  14. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  15. "context"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "github.com/olivere/elastic/v7"
  20. "github.com/streadway/amqp"
  21. "strings"
  22. "time"
  23. )
  24. func EggEnergyDealUserECPMConsume(queue md.MqQueue) {
  25. fmt.Println(">>>>>>>>>>>>EggEnergyDealUserECPMConsume>>>>>>>>>>>>")
  26. ch, err := rabbit.Cfg.Pool.GetChannel()
  27. if err != nil {
  28. logx.Error(err)
  29. return
  30. }
  31. defer ch.Release()
  32. //1、将自己绑定到交换机上
  33. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  34. //2、取出数据进行消费
  35. ch.Qos(1)
  36. delivery := ch.Consume(queue.Name, false)
  37. egg_system_rules.Init(cfg.RedisAddr)
  38. var res amqp.Delivery
  39. var ok bool
  40. for {
  41. res, ok = <-delivery
  42. if ok == true {
  43. err = handleEggEnergyDealUserECPMConsume(res.Body, ch)
  44. if err != nil {
  45. fmt.Println("EggEnergyDealUserECPMConsume_ERR:::::", err.Error())
  46. utils2.FilePutContents("EggEnergyDealUserECPMConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  47. "body": res.Body,
  48. "err": err.Error(),
  49. }))
  50. }
  51. //_ = res.Reject(false)
  52. err = res.Ack(true)
  53. if err != nil {
  54. fmt.Println("err ::: ", err)
  55. }
  56. } else {
  57. panic(errors.New("error getting message"))
  58. }
  59. }
  60. }
  61. func handleEggEnergyDealUserECPMConsume(msgData []byte, ch *rabbit.Channel) error {
  62. //1、解析mq中queue的数据结构体
  63. var msg *md2.DealUserEcpmReq
  64. err := json.Unmarshal(msgData, &msg)
  65. if err != nil {
  66. return err
  67. }
  68. //2、更新用户信息
  69. year, week := time.Now().ISOWeek()
  70. yearStr := utils2.IntToStr(year)
  71. weekStr := utils2.IntToStr(week)
  72. index := es2.GetAppointIndexFromAlias(yearStr, weekStr)
  73. id := fmt.Sprintf("%d%d_%d", year, week, msg.Uid)
  74. doc, err := es.FirstDoc(index, id)
  75. if err != nil {
  76. if !elastic.IsNotFound(err) {
  77. return err
  78. } else {
  79. now := time.Now().Format("2006-01-02 15:04:05")
  80. err1 := egg_energy.CreateEsScoreAndAssignValuesDoc(index, id, msg.Uid, enum.Ecpm, msg.Ecpm, now)
  81. if err1 != nil {
  82. return err1
  83. }
  84. return nil
  85. }
  86. } else {
  87. var score md2.EggEnergyUserEggScoreEs
  88. err = json.Unmarshal(doc.Source, &score)
  89. if err != nil {
  90. fmt.Println("handleTemporaryEggEnergyDealUserECPMConsume failed to unmarshal", err.Error())
  91. return err
  92. }
  93. ecpm := score.Ecpm
  94. ecpmNums := score.EcpmNums
  95. ecpmNums++
  96. ecpm += utils2.StrToFloat64(msg.Ecpm)
  97. ecpmAvg := ecpm / float64(ecpmNums)
  98. _, err = es.EsClient.Update().
  99. Index(index).
  100. Id(id).
  101. IfSeqNo(*doc.SeqNo).
  102. IfPrimaryTerm(*doc.PrimaryTerm).
  103. Doc(map[string]interface{}{
  104. "ecpm": ecpm,
  105. "ecpm_avg": ecpmAvg,
  106. "ecpm_nums": ecpmNums,
  107. }).Do(context.Background())
  108. if err != nil {
  109. fmt.Println("update ecpm err ==========>", err.Error())
  110. if strings.Contains(err.Error(), "elastic: Error 409") {
  111. ch.Publish(md3.EggEnergyExchange, msg, md3.EggEnergyRoutKeyForDealUserEcpm)
  112. fmt.Println("Successfully pushed to the end of the queue:", md3.EggEnergyRoutKeyForDealUserEcpm, msg)
  113. }
  114. return err
  115. }
  116. }
  117. return err
  118. }