蛋蛋星球RabbitMq消费项目
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

advertising_smash_consume.go 1.8 KiB

2 veckor sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/utils/logx"
  6. "applet/consume/md"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  9. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
  10. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  11. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  12. "encoding/json"
  13. "errors"
  14. "fmt"
  15. "github.com/streadway/amqp"
  16. "time"
  17. )
  18. func AdvertisingSmashConsume(queue md.MqQueue) {
  19. fmt.Println(">>>>>>>>>>>>JpushRecordConsume>>>>>>>>>>>>")
  20. ch, err := rabbit.Cfg.Pool.GetChannel()
  21. if err != nil {
  22. logx.Error(err)
  23. return
  24. }
  25. defer ch.Release()
  26. //1、将自己绑定到交换机上
  27. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  28. //2、取出数据进行消费
  29. ch.Qos(1)
  30. delivery := ch.Consume(queue.Name, false)
  31. egg_system_rules.Init(cfg.RedisAddr)
  32. var res amqp.Delivery
  33. var ok bool
  34. for {
  35. res, ok = <-delivery
  36. if ok == true {
  37. err = handleAdvertisingSmashConsume(res.Body)
  38. err = res.Ack(true)
  39. fmt.Println("err ::: ", err)
  40. } else {
  41. panic(errors.New("error getting message"))
  42. }
  43. }
  44. fmt.Println("get msg done")
  45. }
  46. func handleAdvertisingSmashConsume(msgData []byte) error {
  47. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  48. // 1.解析mq中queue的数据结构体
  49. var msg *md.AdvertisingWatch
  50. err := json.Unmarshal(msgData, &msg)
  51. if err != nil {
  52. return err
  53. }
  54. var data model.AdvertisingCallback
  55. db.Db.Where("id=? ", msg.Id).Get(&data)
  56. if data.Id == 0 {
  57. return errors.New("记录不存在")
  58. }
  59. if data.IsRun == 1 {
  60. return nil
  61. }
  62. req := md2.HomePageWatchOverAdReq{UID: int64(data.Uid)}
  63. err = egg_energy.HomePageWatchOverAd(db.Db, req)
  64. if err != nil {
  65. return err
  66. }
  67. data.IsRun = 1
  68. db.Db.Where("id=?", data.Id).Cols("is_run").Update(&data)
  69. return nil
  70. }