message.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package esports
  2. import (
  3. "context"
  4. "net/url"
  5. mdlesp "go-common/app/job/main/web-goblin/model/esports"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. var _notify = "4"
  11. // SendMessage send system notify.
  12. func (d *Dao) SendMessage(mids []int64, msg string, contest *mdlesp.Contest) (err error) {
  13. params := url.Values{}
  14. params.Set("mid_list", xstr.JoinInts(mids))
  15. params.Set("title", d.c.Rule.AlertTitle)
  16. params.Set("mc", d.c.Message.MC)
  17. params.Set("data_type", _notify)
  18. params.Set("context", msg)
  19. var res struct {
  20. Code int `json:"code"`
  21. }
  22. err = d.messageHTTPClient.Post(context.Background(), d.c.Message.URL, "", params, &res)
  23. if err != nil {
  24. log.Error("SendMessage d.messageHTTPClient.Post(%s) error(%+v)", d.c.Message.URL+"?"+params.Encode(), err)
  25. return
  26. }
  27. if res.Code != 0 {
  28. log.Error("SendMessage url(%s) res code(%d)", d.c.Message.URL+"?"+params.Encode(), res.Code)
  29. err = ecode.Int(res.Code)
  30. }
  31. return
  32. }
  33. //Batch 批量处理
  34. func (d *Dao) Batch(list []int64, msg string, contest *mdlesp.Contest, batchSize int, f func(users []int64, msg string, contest *mdlesp.Contest) error) {
  35. if msg == "" {
  36. log.Warn("Batch msg is empty")
  37. return
  38. }
  39. retry := d.c.Push.RetryTimes
  40. for {
  41. var (
  42. mids []int64
  43. err error
  44. )
  45. l := len(list)
  46. if l == 0 {
  47. break
  48. } else if l <= batchSize {
  49. mids = list[:l]
  50. } else {
  51. mids = list[:batchSize]
  52. l = batchSize
  53. }
  54. list = list[l:]
  55. for i := 0; i < retry; i++ {
  56. if err = f(mids, msg, contest); err == nil {
  57. break
  58. }
  59. }
  60. if err != nil {
  61. log.Error("Batch error(%v), params(%s)", err, msg)
  62. }
  63. }
  64. }