push.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package push
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "go-common/app/job/main/appstatic/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. _diffFinish = "SELECT COUNT(1) FROM resource_file WHERE resource_id = ? AND url = ? AND file_type = ? AND is_deleted = 0"
  14. _pushMsg = "SELECT resource.id, resource.pool_id, resource_pool.`name` FROM resource LEFT JOIN resource_pool ON resource.pool_id = resource_pool.id WHERE resource.id = ? LIMIT 1"
  15. _platform = "SELECT b.value FROM resource_config a LEFT JOIN resource_limit b ON a.id = b.config_id WHERE a.resource_id = ? AND b.`column` = 'mobi_app' AND a.is_deleted = 0 AND b.is_deleted = 0"
  16. _diffPkg = 1
  17. )
  18. // CallPush calls the push server api
  19. func (d *Dao) CallPush(ctx context.Context, platform string, msg string, ip string) (err error) {
  20. var (
  21. cfg = d.c.Cfg.Push
  22. params = url.Values{}
  23. )
  24. params.Set("operation", fmt.Sprintf("%d", cfg.Operation))
  25. params.Set("platform", platform)
  26. params.Set("message", msg)
  27. params.Set("speed", fmt.Sprintf("%d", cfg.QPS))
  28. var res struct {
  29. Code int `json:"code"`
  30. }
  31. if err = d.client.Post(ctx, cfg.URL, ip, params, &res); err != nil {
  32. return
  33. }
  34. if res.Code != ecode.OK.Code() {
  35. err = errors.Wrap(ecode.Int(res.Code), cfg.URL+"?"+params.Encode())
  36. }
  37. return
  38. }
  39. // DiffFinish checks whether the resource's diff calculation has been finished or not
  40. func (d *Dao) DiffFinish(c context.Context, resID string) (res bool, err error) {
  41. count := 0
  42. row := d.db.QueryRow(c, _diffFinish, resID, "", _diffPkg)
  43. if err = row.Scan(&count); err != nil {
  44. log.Error("d.DiffFinish err(%v)", err)
  45. return
  46. }
  47. if count == 0 {
  48. res = true
  49. }
  50. return
  51. }
  52. // PushMsg combines the resource pool info to prepare the msg to call PUSH
  53. func (d *Dao) PushMsg(c context.Context, resID string) (res string, err error) {
  54. var (
  55. msg model.PushMsg
  56. data []byte
  57. )
  58. row := d.db.QueryRow(c, _pushMsg, resID)
  59. if err = row.Scan(&msg.ResID, &msg.ModID, &msg.ModName); err != nil {
  60. log.Error("d.PushMsg err(%v)", err)
  61. }
  62. if data, err = json.Marshal(msg); err != nil {
  63. log.Error("PushMsg Info ResID %d, Json Err %v", resID, err)
  64. return
  65. }
  66. res = string(data)
  67. return
  68. }
  69. // Platform picks the mobi_app value to distinguish the platform to push
  70. func (d *Dao) Platform(c context.Context, resID string) (res []string, err error) {
  71. rows, err := d.db.Query(c, _platform, resID)
  72. if err != nil {
  73. log.Error("db.Query(%d) error(%v)", resID, err)
  74. return
  75. }
  76. defer rows.Close()
  77. for rows.Next() {
  78. var mobiApp string
  79. if err = rows.Scan(&mobiApp); err != nil {
  80. log.Error("rows.Scan error(%v)", err)
  81. return
  82. }
  83. res = append(res, mobiApp)
  84. }
  85. return
  86. }