publish.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package pubsub
  4. import (
  5. "gd_management/apis"
  6. "gd_management/common.in/cache"
  7. "gd_management/common.in/config"
  8. "gd_management/errors"
  9. "encoding/json"
  10. "fmt"
  11. "time"
  12. "github.com/astaxie/beego/orm"
  13. )
  14. func PublishApiExportInfoNotify() error {
  15. _, err := cache.Redis.Publish(apis.ApiExportInfoNotifyChannel, "-")
  16. if err != nil {
  17. return errors.RedisPublishFailed
  18. }
  19. return nil
  20. }
  21. func PublishProviderExportInfoNotify() error {
  22. _, err := cache.Redis.Publish(apis.ProviderExportInfoNotifyChannel, "-")
  23. if err != nil {
  24. return errors.RedisPublishFailed
  25. }
  26. return nil
  27. }
  28. func PublishApiNotify(apiId int64, router, method string) error {
  29. if router == "" || method == "" {
  30. o := orm.NewOrm()
  31. o.Raw("select router, method from t_gd_api where id=?", apiId).QueryRow(&router, &method)
  32. }
  33. if router == "" || method == "" {
  34. return nil
  35. }
  36. m := map[string]string{
  37. "router": router,
  38. "method": method,
  39. }
  40. bytes, _ := json.Marshal(m)
  41. _, err := cache.Redis.Publish(apis.ApiNotifyChannel, string(bytes))
  42. if err != nil {
  43. return errors.RedisPublishFailed
  44. }
  45. return nil
  46. }
  47. func PublishMerchantNotify(appKey string) error {
  48. _, err := cache.Redis.Publish(apis.MerchantNotifyChannel, appKey)
  49. if err != nil {
  50. return errors.RedisPublishFailed
  51. }
  52. return nil
  53. }
  54. type MerchantApiInfo struct {
  55. ApiId int64 `json:"api_id"`
  56. MerchantId int64 `json:"merchant_id"`
  57. }
  58. func PublishMerchantApiNotify(merchantDataApiId int64, queryTypeId int64, apiId int64, merchantId int64) error {
  59. if merchantDataApiId > 0 {
  60. tabs := []MerchantApiInfo{}
  61. o := orm.NewOrm()
  62. o.Raw("select t1.merchant_id, t2.api_id from t_gd_merchant_data_api as t1 left join t_gd_merchant_child_data_api as t2 on t1.id=t2.merchant_data_api_id where t2.merchant_data_api_id=?", merchantDataApiId).QueryRows(&tabs)
  63. for _, v := range tabs {
  64. bytes, _ := json.Marshal(v)
  65. _, err := cache.Redis.Publish(apis.MerchantApiNotifyChannel, string(bytes))
  66. if err != nil {
  67. return errors.RedisPublishFailed
  68. }
  69. }
  70. return nil
  71. }
  72. if queryTypeId > 0 {
  73. tabs := []MerchantApiInfo{}
  74. o := orm.NewOrm()
  75. o.Raw("select t1.merchant_id, t2.api_id from t_gd_merchant_data_api as t1 left join t_gd_merchant_child_data_api as t2 on t1.id=t2.merchant_data_api_id where t1.query_type_id=?", queryTypeId).QueryRows(&tabs)
  76. for _, v := range tabs {
  77. bytes, _ := json.Marshal(v)
  78. _, err := cache.Redis.Publish(apis.MerchantApiNotifyChannel, string(bytes))
  79. if err != nil {
  80. return errors.RedisPublishFailed
  81. }
  82. }
  83. return nil
  84. }
  85. tab := MerchantApiInfo{
  86. ApiId: apiId,
  87. MerchantId: merchantId,
  88. }
  89. bytes, _ := json.Marshal(tab)
  90. _, err := cache.Redis.Publish(apis.MerchantApiNotifyChannel, string(bytes))
  91. if err != nil {
  92. return errors.RedisPublishFailed
  93. }
  94. return nil
  95. }
  96. func PublishNameNotify(field string, id int64, providerId int64) error {
  97. if providerId == 0 {
  98. msg := fmt.Sprintf("\"%s\":%d", field, id)
  99. _, err := cache.Redis.Publish(apis.NameNotifyChannel, msg)
  100. if err != nil {
  101. return errors.RedisPublishFailed
  102. }
  103. return nil
  104. }
  105. o := orm.NewOrm()
  106. ids := []int64{}
  107. o.Raw("select id from t_gd_provider_api where provider_id=?", providerId).QueryRows(&ids)
  108. for _, v := range ids {
  109. msg := fmt.Sprintf("\"%s\":%d", "provider_api_id", v)
  110. _, err := cache.Redis.Publish(apis.NameNotifyChannel, msg)
  111. if err != nil {
  112. return errors.RedisPublishFailed
  113. }
  114. }
  115. return nil
  116. }
  117. func PublishRateLimit() {
  118. for i := 0; i < 3; i++ {
  119. if _, err := cache.Redis.Publish(config.Conf.RateLimit.RateLimitChannel, time.Now().UnixNano()); err == nil {
  120. break
  121. }
  122. }
  123. }