task.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/cron"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "io/ioutil"
  9. "mongodb"
  10. "net/http"
  11. "qfw/util"
  12. "strconv"
  13. "time"
  14. )
  15. var taskA = 0 // 增量统计跳过
  16. var (
  17. WebUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=45962efc-ca87-4996-9ffa-08bf6608ab7a"
  18. skip, send = 0, 0
  19. )
  20. func TimeTask() {
  21. //StartTask()
  22. c := cron.New()
  23. //cronstr := "0 0 * * * ?" // 每小时执行一次
  24. cronstr := "0 */10 * * * ?" // 每10min执行一次
  25. //_ = c.AddFunc(cronstr, func() { StartTask() })
  26. _ = c.AddFunc(cronstr, func() {
  27. util.Debug("Start Task...")
  28. Attachment()
  29. })
  30. c.Start()
  31. }
  32. func StartTask() {
  33. util.Debug("Start Task...")
  34. result := MgoCount()
  35. result = EsCheck(result)
  36. if result != "" {
  37. util.Debug(result)
  38. }
  39. util.Debug("Task Over...")
  40. }
  41. // @Description 附件识别监控
  42. // @Author J 2022/6/9 5:58 PM
  43. func Attachment() {
  44. info, _ := Mgo.Find("ocr_goods_over", nil, map[string]interface{}{"_id": -1}, nil, true, -1, -1)
  45. lastT := util.Int64All((*info)[0]["import_time"])
  46. if time.Now().Unix()-lastT > 30*60 {
  47. if skip == 0 {
  48. skip += 1
  49. util.Debug("跳过第一次", (*info)[0]["_id"])
  50. } else {
  51. send++
  52. if send <= 3 {
  53. SendMsg("当前最后一个id段数据累积时间已经超过30分钟, 最后一段数据入库时间为:" + util.FormatDateByInt64(&lastT, util.Date_Full_Layout))
  54. }
  55. }
  56. } else {
  57. if skip != 0 || send != 0 {
  58. skip = 0
  59. send = 0
  60. }
  61. }
  62. }
  63. // 统计mgo数据
  64. func MgoCount() string {
  65. defer util.Catch()
  66. sess := Mgo.GetMgoConn()
  67. defer Mgo.DestoryMongoConn(sess)
  68. result := ""
  69. // biddingback
  70. count := Mgo.Count("bidding_back", nil)
  71. util.Debug("bidding_back---", BiddingBackSize, count)
  72. if int64(count) != BiddingBackSize {
  73. result = fmt.Sprintf("bindding_back数据异常,统计结果%d,往常数据量%d", count, BiddingBackSize)
  74. result += "<br>"
  75. }
  76. // bidding
  77. st := time.Now().Unix() - 3600
  78. currentLastId := fmt.Sprintf("%x0000000000000000", st)
  79. // 对比内存中存量id的统计结果
  80. q1 := bson.M{"_id": bson.M{"$lte": mongodb.StringTOBsonId(LastStockId)}}
  81. count1 := Mgo.Count("bidding", q1)
  82. util.Debug("bidding---", LastStockSize, count1, q1)
  83. if int64(count1) != LastStockSize {
  84. result = fmt.Sprintf("bindding数据存量统计异常,统计条件%v,统计结果%d,上次统计数据量%d", q1, count1, LastStockSize)
  85. result += "<br>"
  86. } else {
  87. // 无异常,替换成当前时间点(当前时间点往前一个小时的id)存量的统计结果
  88. LastStockId = currentLastId
  89. q2 := bson.M{"_id": bson.M{"$lte": mongodb.StringTOBsonId(LastStockId)}}
  90. count2 := Mgo.Count("bidding", q2)
  91. LastStockSize = int64(count2)
  92. }
  93. // 增量
  94. et := time.Now().Unix()
  95. currentId := fmt.Sprintf("%x0000000000000000", et)
  96. q3 := bson.M{"_id": bson.M{"$gt": mongodb.StringTOBsonId(currentLastId), "$lte": mongodb.StringTOBsonId(currentId)}}
  97. count3 := Mgo.Count("bidding", q3)
  98. util.Debug("bidding---", count3, q3)
  99. if int64(count3) == 0 {
  100. if taskA > 1 {
  101. result += fmt.Sprintf("bindding数据增量统计异常,统计条件%v,统计结果%d", q3, count3)
  102. result += "<br><br>"
  103. taskA = 0
  104. } else {
  105. taskA++
  106. }
  107. }
  108. return result
  109. }
  110. func EsCheck(result string) string {
  111. client := Es.GetEsConn()
  112. defer Es.DestoryEsConn(client)
  113. resp, _ := client.ClusterHealth().Do()
  114. util.Debug(*resp)
  115. if resp.Status != "green" {
  116. result += "<br>" + "检索库异常,异常内容:" + "<br>" +
  117. "cluster_name:" + resp.ClusterName + "<br>" +
  118. "status:" + resp.Status + "<br>" +
  119. "number_of_nodes:" + strconv.Itoa(resp.NumberOfNodes) + "<br>" +
  120. "number_of_data_nodes:" + strconv.Itoa(resp.NumberOfDataNodes) + "<br>" +
  121. "number_of_data_nodes:" + strconv.Itoa(resp.ActivePrimaryShards) + "<br>" +
  122. "active_shards:" + strconv.Itoa(resp.ActiveShards) + "<br>" +
  123. "relocating_shards:" + strconv.Itoa(resp.RelocatingShards) + "<br>" +
  124. "initialized_shards:" + strconv.Itoa(resp.InitializedShards) + "<br>" +
  125. "unassigned_shards:" + strconv.Itoa(resp.UnassignedShards) + "<br>"
  126. }
  127. return result
  128. }
  129. func SendMail(report string) {
  130. _, _ = http.Get(fmt.Sprintf("%s?to=%s&title=%s&body=%s", api, to, "异常报告!", report))
  131. }
  132. func SendMsg(content string) {
  133. client := &http.Client{}
  134. data := map[string]interface{}{"msgtype": "text", "text": map[string]interface{}{
  135. "content": content, "mentioned_list": []string{"@all"},
  136. }}
  137. bytesData, _ := json.Marshal(data)
  138. req, _ := http.NewRequest("POST", WebUrl, bytes.NewReader(bytesData))
  139. resp, _ := client.Do(req)
  140. body, _ := ioutil.ReadAll(resp.Body)
  141. fmt.Println(string(body))
  142. }