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

jpush_record_consume.go 2.7 KiB

2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. utils2 "applet/app/utils"
  6. "applet/app/utils/cache"
  7. "applet/app/utils/logx"
  8. "applet/consume/md"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  10. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  12. "code.fnuoos.com/EggPlanet/egg_system_rules.git/jPush"
  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 JpushRecordConsume(queue md.MqQueue) {
  22. fmt.Println(">>>>>>>>>>>>JpushRecordConsume>>>>>>>>>>>>")
  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 = handleJpushRecordConsume(res.Body)
  41. if err != nil {
  42. fmt.Println("JpushRecordConsume_ERR:::::", err.Error())
  43. utils2.FilePutContents("JpushRecordConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  44. "body": res.Body,
  45. "err": err.Error(),
  46. }))
  47. _ = res.Reject(false)
  48. //TODO::重新推回队列末尾,避免造成队列堵塞
  49. var msg *md.JpushRecordFundData
  50. json.Unmarshal(res.Body, &msg)
  51. msg.Num = "1"
  52. ch.Publish(queue.ExchangeName, msg, queue.RoutKey)
  53. } else {
  54. //_ = res.Reject(false)
  55. err = res.Ack(true)
  56. }
  57. fmt.Println("err ::: ", err)
  58. } else {
  59. panic(errors.New("error getting message"))
  60. }
  61. }
  62. fmt.Println("get msg done")
  63. }
  64. func handleJpushRecordConsume(msgData []byte) error {
  65. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  66. // 1.解析mq中queue的数据结构体
  67. var msg *md.JpushRecordFundData
  68. err := json.Unmarshal(msgData, &msg)
  69. if err != nil {
  70. return err
  71. }
  72. engine := db.Db
  73. redisConn := cache.GetPool().Get()
  74. sysCfgDb := implement.NewSysCfgDb(engine, redisConn)
  75. jpushKey := sysCfgDb.SysCfgGetWithDb("jpush_key")
  76. jpushSecret := sysCfgDb.SysCfgGetWithDb("jpush_secret")
  77. if msg.Target == "0" { //广播全部
  78. _, err := jPush.PushAllUser(jpushKey, jpushSecret, msg.Title, msg.Content, msg.Platform, nil)
  79. if err != nil && msg.Num != "1" {
  80. return err
  81. }
  82. } else {
  83. _, err = jPush.PushMoreUser(jpushKey, jpushSecret, msg.Title, msg.Content, msg.Platform, strings.Split(msg.UserId, ","), nil)
  84. if err != nil && msg.Num != "1" {
  85. return err
  86. }
  87. }
  88. if msg.Id != "" {
  89. engine.Where("id=?", msg.Id).Cols("state").Update(&model.JpushRecord{State: 1})
  90. }
  91. return nil
  92. }