main.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "context"
  4. _ "github.com/gogf/gf/contrib/nosql/redis/v2"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gcron"
  7. "github.com/gogf/gf/v2/os/gctx"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. )
  10. type ResponseStruct struct {
  11. Nodes map[string]ThreadPool `json:"nodes"`
  12. }
  13. type ThreadPool struct {
  14. ThreadPool struct {
  15. Search struct {
  16. Threads int `json:"threads"`
  17. Queue int `json:"queue"`
  18. Active int `json:"active"`
  19. Rejected int `json:"rejected"`
  20. Largest int `json:"largest"`
  21. Completed int `json:"completed"`
  22. } `json:"search"`
  23. } `json:"thread_pool"`
  24. }
  25. func job(ctx context.Context) {
  26. r, err := g.Client().Get(ctx, g.Cfg().MustGet(ctx, "curlAddr").String())
  27. if err != nil {
  28. g.Log().Errorf(ctx, "请求异常 err:%v", err)
  29. return
  30. }
  31. defer r.Close()
  32. rs := &ResponseStruct{}
  33. if err := gconv.Struct(r.ReadAllString(), rs); err != nil || rs == nil || len(rs.Nodes) == 0 {
  34. g.Log().Errorf(ctx, "请求结果异常 err:%v", err)
  35. return
  36. }
  37. maxQueue, maxActive, finalStatus := 0, 0, 2
  38. for _, val := range rs.Nodes {
  39. if val.ThreadPool.Search.Queue > maxQueue {
  40. maxQueue = val.ThreadPool.Search.Queue
  41. }
  42. if val.ThreadPool.Search.Active > maxActive {
  43. maxActive = val.ThreadPool.Search.Active
  44. }
  45. }
  46. if maxQueue == 0 && maxActive < 0 {
  47. finalStatus = 0
  48. } else if maxQueue < 6 && maxActive < 10 {
  49. finalStatus = 1
  50. }
  51. g.Log().Debugf(ctx, "now maxQueue:%d maxActive:%d finalStatus:%d", maxQueue, maxActive, finalStatus)
  52. _, _ = g.Redis().Set(ctx, "es_status", finalStatus)
  53. }
  54. func main() {
  55. ctx := gctx.New()
  56. e, err := gcron.New().Add(ctx, g.Cfg().MustGet(ctx, "cron").String(), job)
  57. if err != nil {
  58. panic(err)
  59. }
  60. e.Start()
  61. select {}
  62. }