main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "app.yhyue.com/data_processing/common_utils/log"
  5. "bytes"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/robfig/cron"
  9. "github.com/spf13/cobra"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "go.uber.org/zap"
  12. "io/ioutil"
  13. "monitor/config"
  14. "net/http"
  15. )
  16. var (
  17. WebUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=45962efc-ca87-4996-9ffa-08bf6608ab7a"
  18. WarningStr = "数据采集bidding_file表数据,已累计%d条数据未处理"
  19. WarningStr1 = "标的物等字段识别提醒,bidding表数据已积累%d条未处理"
  20. )
  21. func init() {
  22. config.Init("./common.toml")
  23. InitLog()
  24. }
  25. func main() {
  26. rootCmd := &cobra.Command{Use: "my cmd"}
  27. rootCmd.AddCommand(biddingFile())
  28. if err := rootCmd.Execute(); err != nil {
  29. fmt.Println("rootCmd.Execute failed", err.Error())
  30. }
  31. }
  32. func biddingFile() *cobra.Command {
  33. cmdClient := &cobra.Command{
  34. Use: "bidding_warn",
  35. Short: "Start statistics bidding_file data",
  36. Run: func(cmd *cobra.Command, args []string) {
  37. InitMgo()
  38. crn := cron.New()
  39. _ = crn.AddFunc("@hourly", func() {
  40. taskFile()
  41. })
  42. crn.Start()
  43. _ = crn.AddFunc("0 */30 * * * ?", func() {
  44. taskPy()
  45. })
  46. c := make(chan bool, 1)
  47. <-c
  48. },
  49. }
  50. return cmdClient
  51. }
  52. func taskFile() {
  53. count := MgoB.Count("bidding_file", bson.M{"moveok": bson.M{"$exists": false}})
  54. if count > config.Conf.Serve.FileWarn {
  55. SendMsg(fmt.Sprintf(WarningStr, count))
  56. } else {
  57. log.Info("bidding_file", zap.Int("count", count))
  58. }
  59. }
  60. func taskPy() {
  61. info, _ := MgoB.Find("bidding_processing_ids", bson.M{"dataprocess": bson.M{"$in": []int{1, 2}}}, nil, bson.M{"count": 1, "dataprocess": 1}, false, -1, -1)
  62. count := 0
  63. for _, m := range *info {
  64. count += util.IntAll(m["count"])
  65. }
  66. if count > config.Conf.Serve.PyWarn {
  67. SendMsg(fmt.Sprintf(WarningStr1, count))
  68. } else {
  69. log.Info("bidding_py", zap.Int("count", count))
  70. }
  71. }
  72. func SendMsg(content string) {
  73. client := &http.Client{}
  74. data := map[string]interface{}{"msgtype": "text", "text": map[string]interface{}{
  75. "content": content, "mentioned_mobile_list": []string{"13373929153", "15090279371", "15639297172"},
  76. }}
  77. bytesData, _ := json.Marshal(data)
  78. req, _ := http.NewRequest("POST", WebUrl, bytes.NewReader(bytesData))
  79. resp, _ := client.Do(req)
  80. body, _ := ioutil.ReadAll(resp.Body)
  81. log.Info("SendMsg", zap.String("resp", string(body)))
  82. }