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

2 weeks ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/svc/sys_cfg"
  6. utils2 "applet/app/utils"
  7. "applet/app/utils/logx"
  8. "applet/consume/md"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git/jPush"
  12. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  13. "encoding/json"
  14. "errors"
  15. "fmt"
  16. "github.com/streadway/amqp"
  17. "strings"
  18. "time"
  19. )
  20. func JpushRecordConsume(queue md.MqQueue) {
  21. fmt.Println(">>>>>>>>>>>>JpushRecordConsume>>>>>>>>>>>>")
  22. ch, err := rabbit.Cfg.Pool.GetChannel()
  23. if err != nil {
  24. logx.Error(err)
  25. return
  26. }
  27. defer ch.Release()
  28. //1、将自己绑定到交换机上
  29. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  30. //2、取出数据进行消费
  31. ch.Qos(1)
  32. delivery := ch.Consume(queue.Name, false)
  33. egg_system_rules.Init(cfg.RedisAddr)
  34. var res amqp.Delivery
  35. var ok bool
  36. for {
  37. res, ok = <-delivery
  38. if ok == true {
  39. err = handleJpushRecordConsume(res.Body)
  40. if err != nil {
  41. fmt.Println("JpushRecordConsume_ERR:::::", err.Error())
  42. utils2.FilePutContents("JpushRecordConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  43. "body": res.Body,
  44. "err": err.Error(),
  45. }))
  46. _ = res.Reject(false)
  47. //TODO::重新推回队列末尾,避免造成队列堵塞
  48. var msg *md.JpushRecordFundData
  49. json.Unmarshal(res.Body, &msg)
  50. msg.Num = "1"
  51. ch.Publish(queue.ExchangeName, msg, queue.RoutKey)
  52. } else {
  53. //_ = res.Reject(false)
  54. err = res.Ack(true)
  55. }
  56. fmt.Println("err ::: ", err)
  57. } else {
  58. panic(errors.New("error getting message"))
  59. }
  60. }
  61. fmt.Println("get msg done")
  62. }
  63. func handleJpushRecordConsume(msgData []byte) error {
  64. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  65. // 1.解析mq中queue的数据结构体
  66. var msg *md.JpushRecordFundData
  67. err := json.Unmarshal(msgData, &msg)
  68. if err != nil {
  69. return err
  70. }
  71. engine := db.Db
  72. sysCfgDb := sys_cfg.NewSysCfgDb(engine)
  73. jpushKey := sysCfgDb.SysCfgGetWithDb("jpush_key")
  74. jpushSecret := sysCfgDb.SysCfgGetWithDb("jpush_secret")
  75. if msg.Target == "0" { //广播全部
  76. _, err := jPush.PushAllUser(jpushKey, jpushSecret, msg.Title, msg.Content, msg.Platform, nil)
  77. if err != nil && msg.Num != "1" {
  78. return err
  79. }
  80. } else {
  81. _, err = jPush.PushMoreUser(jpushKey, jpushSecret, msg.Title, msg.Content, msg.Platform, strings.Split(msg.UserId, ","), nil)
  82. if err != nil && msg.Num != "1" {
  83. return err
  84. }
  85. }
  86. if msg.Id != "" {
  87. engine.Where("id=?", msg.Id).Cols("state").Update(&model.JpushRecord{State: 1})
  88. }
  89. return nil
  90. }