Browse Source

wip:cron 定时任务提交

wangkaiyue 2 years ago
parent
commit
1541cb64cc
3 changed files with 26 additions and 6 deletions
  1. 5 1
      config.yaml
  2. 7 1
      go.mod
  3. 14 4
      main.go

+ 5 - 1
config.yaml

@@ -1,3 +1,7 @@
 cron: "*/5 * * * * *"
 cron: "*/5 * * * * *"
-curlAddr: ""
+curlAddr: "http://192.168.3.241:9205/_nodes/stats?pretty&human&filter_path=nodes.*.thread_pool.search"
+
+redis:
+  default: # 任意redis
+    address: 192.168.3.11:1712
 
 

+ 7 - 1
go.mod

@@ -2,15 +2,21 @@ module elasticsearch_status
 
 
 go 1.19
 go 1.19
 
 
-require github.com/gogf/gf/v2 v2.5.0
+require (
+	github.com/gogf/gf/contrib/nosql/redis/v2 v2.5.0
+	github.com/gogf/gf/v2 v2.5.0
+)
 
 
 require (
 require (
 	github.com/BurntSushi/toml v1.1.0 // indirect
 	github.com/BurntSushi/toml v1.1.0 // indirect
+	github.com/cespare/xxhash/v2 v2.1.2 // indirect
 	github.com/clbanning/mxj/v2 v2.5.5 // indirect
 	github.com/clbanning/mxj/v2 v2.5.5 // indirect
+	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
 	github.com/fatih/color v1.13.0 // indirect
 	github.com/fatih/color v1.13.0 // indirect
 	github.com/fsnotify/fsnotify v1.5.4 // indirect
 	github.com/fsnotify/fsnotify v1.5.4 // indirect
 	github.com/go-logr/logr v1.2.3 // indirect
 	github.com/go-logr/logr v1.2.3 // indirect
 	github.com/go-logr/stdr v1.2.2 // indirect
 	github.com/go-logr/stdr v1.2.2 // indirect
+	github.com/go-redis/redis/v8 v8.11.5 // indirect
 	github.com/gorilla/websocket v1.5.0 // indirect
 	github.com/gorilla/websocket v1.5.0 // indirect
 	github.com/grokify/html-strip-tags-go v0.0.1 // indirect
 	github.com/grokify/html-strip-tags-go v0.0.1 // indirect
 	github.com/magiconair/properties v1.8.6 // indirect
 	github.com/magiconair/properties v1.8.6 // indirect

+ 14 - 4
main.go

@@ -3,20 +3,30 @@ package main
 import (
 import (
 	"context"
 	"context"
 	"fmt"
 	"fmt"
+	_ "github.com/gogf/gf/contrib/nosql/redis/v2"
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/os/gcron"
 	"github.com/gogf/gf/v2/os/gcron"
 	"github.com/gogf/gf/v2/os/gctx"
 	"github.com/gogf/gf/v2/os/gctx"
-	"time"
+	"log"
 )
 )
 
 
 func main() {
 func main() {
 	ctx := gctx.New()
 	ctx := gctx.New()
-	e, err := gcron.New().Add(ctx, g.Cfg().MustGet(ctx, "cron").String(), func(ctx context.Context) {
-		fmt.Println("cc", time.Now().Unix())
-	})
+	e, err := gcron.New().Add(ctx, g.Cfg().MustGet(ctx, "cron").String(), job)
 	if err != nil {
 	if err != nil {
 		panic(err)
 		panic(err)
 	}
 	}
 	e.Start()
 	e.Start()
 	select {}
 	select {}
 }
 }
+
+func job(ctx context.Context) {
+	log.Println("do curl")
+	r, err := g.Client().Get(ctx, g.Cfg().MustGet(ctx, "curlAddr").String())
+	if err != nil {
+		g.Log().Errorf(ctx, "请求异常", err)
+	}
+	defer r.Close()
+	fmt.Println(r.ReadAllString())
+
+}