蛋蛋星球RabbitMq消费项目
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

93 linhas
2.6 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. es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
  12. "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  13. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  14. "encoding/json"
  15. "errors"
  16. "fmt"
  17. "github.com/streadway/amqp"
  18. "strings"
  19. "time"
  20. )
  21. func EggEnergyDealUserECPMConsume(queue md.MqQueue) {
  22. fmt.Println(">>>>>>>>>>>>EggEnergyDealUserECPMConsume>>>>>>>>>>>>")
  23. ch, err := rabbit.Cfg.Pool.GetChannel()
  24. if err != nil {
  25. logx.Error(err)
  26. return
  27. }
  28. defer ch.Release()
  29. //1、将自己绑定到交换机上
  30. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  31. //2、取出数据进行消费
  32. ch.Qos(1)
  33. delivery := ch.Consume(queue.Name, false)
  34. egg_system_rules.Init(cfg.RedisAddr)
  35. var res amqp.Delivery
  36. var ok bool
  37. for {
  38. res, ok = <-delivery
  39. if ok == true {
  40. err = handleEggEnergyDealUserECPMConsume(res.Body)
  41. if err != nil {
  42. fmt.Println("EggEnergyDealUserECPMConsume_ERR:::::", err.Error())
  43. utils2.FilePutContents("EggEnergyDealUserECPMConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  44. "body": res.Body,
  45. "err": err.Error(),
  46. }))
  47. }
  48. //_ = res.Reject(false)
  49. err = res.Ack(true)
  50. fmt.Println("err ::: ", err)
  51. } else {
  52. panic(errors.New("error getting message"))
  53. }
  54. }
  55. }
  56. func handleEggEnergyDealUserECPMConsume(msgData []byte) error {
  57. //1、解析mq中queue的数据结构体
  58. var msg *md2.DealUserEcpmReq
  59. err := json.Unmarshal(msgData, &msg)
  60. if err != nil {
  61. return err
  62. }
  63. //2、更新用户信息
  64. year, week := time.Now().ISOWeek()
  65. yearStr := utils2.IntToStr(year)
  66. weekStr := utils2.IntToStr(week)
  67. index := es2.GetAppointIndexFromAlias(yearStr, weekStr)
  68. id := fmt.Sprintf("%d%d_%d", year, week, msg.Uid)
  69. updateMap := make(map[string]interface{})
  70. updateMap["ecpm"] = utils2.StrToFloat64(msg.Ecpm)
  71. updateDocRet, err := es.UpdateDoc(index, id, updateMap)
  72. if err != nil {
  73. if strings.Contains(err.Error(), "elastic: Error 404 (Not Found)") {
  74. // 蛋蛋分数据还不存在,创建蛋蛋分数据
  75. now := time.Now().Format("2006-01-02 15:04:05")
  76. err1 := egg_energy.CreateEsScoreAndAssignValuesDoc(index, id, msg.Uid, enum.Ecpm, msg.Ecpm, now)
  77. if err1 != nil {
  78. return err1
  79. }
  80. return nil
  81. }
  82. return err
  83. }
  84. fmt.Printf("UpdateCreateDoc ==> %+v \n\n", updateDocRet)
  85. return err
  86. }