job.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "github.com/robfig/cron"
  7. "kpt.notice/apiserver/model"
  8. "kpt.notice/middleware/notice"
  9. "kpt.notice/pkg/log"
  10. )
  11. func JobInit() {
  12. c := cron.New()
  13. // ReadMsgs()
  14. // notice.SendMessage(nil)
  15. c.AddFunc("30 * * * * *", ReadMsgs)
  16. c.Start()
  17. // 定时清理数据(记录+删除)
  18. c2 := cron.New()
  19. c2.AddFunc("0 0 0/1 * * *", clearMessage)
  20. c2.Start()
  21. // 清理七天前的记录数据
  22. c3 := cron.New()
  23. c3.AddFunc("0 0 0 0/1 * *", clearMessageOfBeforeWeek)
  24. c3.Start()
  25. }
  26. func clearMessageOfBeforeWeek() {
  27. log.Errorf("%v\n", "-------------------定时清理七天前的备份数据--------------------")
  28. errCopy := svc.DB.Exec(" delete from message_history where created_at< DATE_SUB(NOW(),INTERVAL 7 day) ")
  29. if errCopy.Error == nil {
  30. log.Errorf("%v\n", "-------------------备份数据删除完成!--------------------")
  31. } else {
  32. log.Errorf("%v\n", "-------------------备份数据删除失败!--------------------")
  33. log.Errorf("%v", errCopy.Error)
  34. }
  35. }
  36. func clearMessage() {
  37. log.Errorf("%v\n", "-------------------定时清理数据,开始copy数据--------------------")
  38. errCopy := svc.DB.Exec(" insert into message_history select * from message where `status`!=0 or target='null' ")
  39. if errCopy.Error == nil {
  40. log.Errorf("%v\n", "-------------------copy数据完成,开始删除数据--------------------")
  41. errDel := svc.DB.Exec(" delete from message where `status`!=0 or target='null' ")
  42. if errDel.Error == nil {
  43. log.Errorf("%v\n", "-------------------删除数据完成,定时清理数据完成!--------------------")
  44. } else {
  45. log.Errorf("%v\n", "-------------------删除数据失败!--------------------")
  46. log.Errorf("%v", errDel.Error)
  47. }
  48. } else {
  49. log.Errorf("%v\n", "-------------------copy数据失败!--------------------")
  50. log.Errorf("%v", errCopy.Error)
  51. }
  52. }
  53. func ReadMsgs() {
  54. log.Errorf("%v\n", "-------------------发送消息--------------------")
  55. var msgs []model.Message
  56. var err error
  57. if msgs, err = svc.QueryMsg(); err != nil {
  58. log.Errorf("%v", err)
  59. return
  60. }
  61. fmt.Println("msg len=", len(msgs))
  62. if len(msgs) == 0 {
  63. return
  64. }
  65. fmt.Printf("%v\n", msgs)
  66. for _, val := range msgs {
  67. e := SendMsg(&val)
  68. log.Error("----e----", e)
  69. }
  70. }
  71. func SendMsg(val *model.Message) (err error) {
  72. svc.UpdateMessageStatus(val.ID)
  73. // if val.SysName != "tmrWatch_test" {
  74. // return
  75. // } else {
  76. // val.Target = `["oLd1b56PwpexCa0QK4NCkza9TKyY"]`
  77. // }
  78. data := make(map[string]interface{})
  79. targets := make([]string, 10)
  80. if e := json.Unmarshal([]byte(val.MsgContent), &data); e != nil {
  81. log.Errorf("%v", err)
  82. return
  83. }
  84. if e := json.Unmarshal([]byte(val.Target), &targets); e != nil {
  85. log.Errorf("%v", err)
  86. return
  87. }
  88. for _, openid := range targets {
  89. data["touser"] = openid
  90. if jsonstr, e := json.Marshal(data); e != nil {
  91. err = errors.Wrap(e, "SendMessage{json marshal error of data}=%v")
  92. log.Errorf("%v", err)
  93. continue
  94. } else {
  95. if resp, tmperr := notice.MsgTemplate(jsonstr); tmperr != nil {
  96. err = errors.Wrap(tmperr, "SendMessage{send message error}==%v")
  97. log.Errorf("%v", err)
  98. val.ErrInfo = tmperr.Error()
  99. // 失败记录错误信息、状态
  100. // if e := svc.UpdateMessageFail(val); e != nil {
  101. // err = errors.Wrap(e, "SendMessage{update message error}=%v")
  102. // log.Errorf("%v", err)
  103. // }
  104. } else {
  105. log.Infof("send msg success%v", string(resp))
  106. if e := svc.UpdateMessage(val); e != nil {
  107. err = errors.Wrap(e, "SendMessage{update message error}=%v")
  108. log.Errorf("%v", err)
  109. }
  110. }
  111. }
  112. }
  113. return
  114. }