task.go 4.7 KB

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