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

134 lines
3.4 KiB

  1. package consume
  2. import (
  3. "applet/app/db"
  4. utils2 "applet/app/utils"
  5. "applet/app/utils/logx"
  6. "applet/consume/md"
  7. md2 "applet/es/md"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  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. "context"
  13. "encoding/json"
  14. "errors"
  15. "fmt"
  16. "github.com/olivere/elastic/v7"
  17. "github.com/streadway/amqp"
  18. "time"
  19. )
  20. // EggCanalViolateNumsConsume 更新违规次数
  21. func EggCanalViolateNumsConsume(queue md.MqQueue) {
  22. fmt.Println(">>>>>>>>>>>EggCanalViolateNumsConsume>>>>>>>>>>>>>")
  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(1000)
  33. delivery := ch.Consume(queue.Name, true) //设置自动应答
  34. var res amqp.Delivery
  35. var ok bool
  36. for {
  37. res, ok = <-delivery
  38. if ok == true {
  39. //fmt.Println(string(res.Body))
  40. fmt.Println(">>>>>>>>>>>>>>>>>>EggCanalViolateNumsConsume<<<<<<<<<<<<<<<<<<<<<<<<<")
  41. err = handleEggCanalViolateNumsConsume(res.Body)
  42. if err != nil {
  43. fmt.Println("EggCanalViolateNumsConsume_ERR:::::", err.Error())
  44. utils2.FilePutContents("EggCanalViolateNumsConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  45. "body": res.Body,
  46. "err": err.Error(),
  47. }))
  48. }
  49. //_ = res.Reject(false)
  50. //_ = res.Ack(true)
  51. } else {
  52. panic(errors.New("error getting message"))
  53. }
  54. }
  55. }
  56. func handleEggCanalViolateNumsConsume(msg []byte) error {
  57. //1、解析canal采集至mq中queue的数据结构体
  58. var canalMsg *md.CanalTagRecordsMessage[md.CanalTagRecords]
  59. err := json.Unmarshal(msg, &canalMsg)
  60. if err != nil {
  61. return nil
  62. }
  63. year, week := time.Now().ISOWeek()
  64. yearStr := utils2.IntToStr(year)
  65. weekStr := utils2.IntToStr(week)
  66. index := es2.GetAppointIndexFromAlias(yearStr, weekStr)
  67. // 2. 监听插入信息
  68. if canalMsg.Type == md2.CanalMsgInsertSqlType {
  69. tagDb := implement.NewUserTagDb(db.Db)
  70. for _, item := range canalMsg.Data {
  71. tag, err1 := tagDb.UserTagGetOneByParams(map[string]interface{}{
  72. "key": "id",
  73. "value": item.TagId,
  74. })
  75. if err1 != nil {
  76. return err1
  77. }
  78. // 2.1. 判断是否为处罚标签
  79. if tag.IsPunish == 0 {
  80. continue
  81. }
  82. uid := item.Uid
  83. id := fmt.Sprintf("%d%d_%s", year, week, uid)
  84. // 2.2. 增加违规次数记录
  85. script := elastic.NewScript("ctx._source.violate_nums += params.inc").Param("inc", 1)
  86. _, err = es.EsClient.Update().
  87. Index(index).
  88. Id(id).
  89. Script(script).
  90. Do(context.Background())
  91. if err != nil {
  92. return err
  93. }
  94. }
  95. }
  96. // 3. 监听删除信息
  97. if canalMsg.Type == md2.CanalMsgDeleteSqlType {
  98. tagDb := implement.NewUserTagDb(db.Db)
  99. for _, item := range canalMsg.Data {
  100. tag, err1 := tagDb.UserTagGetOneByParams(map[string]interface{}{
  101. "key": "id",
  102. "value": item.TagId,
  103. })
  104. if err1 != nil {
  105. return err1
  106. }
  107. // 3.1 判断是否为处罚标签
  108. if tag.IsPunish == 0 {
  109. continue
  110. }
  111. uid := item.Uid
  112. id := fmt.Sprintf("%d%d_%s", year, week, uid)
  113. // 3.2 减少违规次数记录
  114. script := elastic.NewScript("ctx._source.violate_nums -= params.dec").Param("dec", 1)
  115. _, err = es.EsClient.Update().
  116. Index(index).
  117. Id(id).
  118. Script(script).
  119. Do(context.Background())
  120. if err != nil {
  121. return err
  122. }
  123. }
  124. }
  125. return nil
  126. }