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

98 lines
2.8 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. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
  9. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
  10. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  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. "context"
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "github.com/olivere/elastic/v7"
  19. "github.com/streadway/amqp"
  20. "strings"
  21. "time"
  22. )
  23. func EggCommentDataConsume(queue md.MqQueue) {
  24. fmt.Println(">>>>>>>>>>>>EggCommentDataConsume>>>>>>>>>>>>")
  25. ch, err := rabbit.Cfg.Pool.GetChannel()
  26. if err != nil {
  27. logx.Error(err)
  28. return
  29. }
  30. defer ch.Release()
  31. //1、将自己绑定到交换机上
  32. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  33. //2、取出数据进行消费
  34. ch.Qos(1)
  35. delivery := ch.Consume(queue.Name, false)
  36. egg_system_rules.Init(cfg.RedisAddr)
  37. var res amqp.Delivery
  38. var ok bool
  39. for {
  40. res, ok = <-delivery
  41. if ok == true {
  42. err = handleEggCommentDataConsume(res.Body)
  43. if err != nil {
  44. fmt.Println("EggCommentDataConsume:::::", err.Error())
  45. utils2.FilePutContents("EggCommentDataConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  46. "body": res.Body,
  47. "err": err.Error(),
  48. }))
  49. }
  50. //_ = res.Reject(false)
  51. err = res.Ack(true)
  52. fmt.Println("err ::: ", err)
  53. } else {
  54. panic(errors.New("error getting message"))
  55. }
  56. }
  57. }
  58. func handleEggCommentDataConsume(msgData []byte) error {
  59. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  60. // 1.解析mq中queue的数据结构体
  61. var msg *md2.EggSendFriendCircleData
  62. err := json.Unmarshal(msgData, &msg)
  63. if err != nil {
  64. return err
  65. }
  66. year, week := time.Now().ISOWeek()
  67. yearStr := utils2.IntToStr(year)
  68. weekStr := utils2.IntToStr(week)
  69. index := es2.GetAppointIndexFromAlias(yearStr, weekStr)
  70. id := fmt.Sprintf("%d%d_%d", year, week, msg.Uid)
  71. script := elastic.NewScript("ctx._source.forum_comments_nums += params.inc").Param("inc", 1)
  72. updateDoc, err := es.EsClient.Update().
  73. Index(index).
  74. Id(id).
  75. Script(script).
  76. Do(context.Background())
  77. if err != nil {
  78. if strings.Contains(err.Error(), "elastic: Error 404 (Not Found)") {
  79. // 蛋蛋分数据还不存在,创建蛋蛋分数据
  80. now := time.Now().Format("2006-01-02 15:04:05")
  81. err1 := egg_energy.CreateEsScoreAndAssignValuesDoc(index, id, msg.Uid, enum.ForumCommentsNums, "1", now)
  82. if err1 != nil {
  83. return err1
  84. }
  85. return nil
  86. }
  87. fmt.Println("EggCommentDataConsumeUpdateDoc_ERR::::", err.Error())
  88. return err
  89. }
  90. fmt.Println("updateDoc==========>", updateDoc)
  91. return nil
  92. }