From 5643fc0ab02564d2a54415c414568b850bb82ce4 Mon Sep 17 00:00:00 2001 From: dilap54 Date: Thu, 21 Dec 2023 23:52:01 +0300 Subject: [PATCH] uptime --- .env | 3 ++- cmd/cli/plati.go | 14 +++++++++++++- healthbeat/beat.go | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 healthbeat/beat.go diff --git a/.env b/.env index f60c662..5afde1d 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -POSTGRESQL_DSN="host=localhost port=5432 user=postgres password=123321Aa dbname=platiparser sslmode=disable" \ No newline at end of file +POSTGRESQL_DSN="host=localhost port=5432 user=postgres password=123321Aa dbname=platiparser sslmode=disable" +UPTIMEKUMA_URL="https://uptime.home.4it.me/api/push/lqWN0TxOMn" \ No newline at end of file diff --git a/cmd/cli/plati.go b/cmd/cli/plati.go index 0b9f256..5293998 100644 --- a/cmd/cli/plati.go +++ b/cmd/cli/plati.go @@ -10,6 +10,7 @@ import ( "time" "gitea.home.4it.me/dilap54/platiparser/gorm" + "gitea.home.4it.me/dilap54/platiparser/healthbeat" "gitea.home.4it.me/dilap54/platiparser/plati" "gitea.home.4it.me/dilap54/platiparser/proxies" uuid "github.com/satori/go.uuid" @@ -24,6 +25,8 @@ func init() { var platiCommand = &cli.Command{ Name: "plati", Action: func(c *cli.Context) error { + timeStart := time.Now() + categories := openCategories("./categories.json").Content categories = filterBySubstring("Gift", categories) @@ -34,6 +37,8 @@ var platiCommand = &cli.Command{ // printNames(categories) + beatUrl := os.Getenv("UPTIMEKUMA_URL") + wg, _ := errgroup.WithContext(ctx) wg.SetLimit(10) @@ -62,7 +67,14 @@ var platiCommand = &cli.Command{ } - return wg.Wait() + if err := wg.Wait(); err != nil { + healthbeat.Beat(beatUrl, "down", err.Error(), int(time.Since(timeStart).Milliseconds())) + return err + } + + healthbeat.Beat(beatUrl, "up", "OK", int(time.Since(timeStart).Milliseconds())) + + return nil }, } diff --git a/healthbeat/beat.go b/healthbeat/beat.go new file mode 100644 index 0000000..0541112 --- /dev/null +++ b/healthbeat/beat.go @@ -0,0 +1,15 @@ +package healthbeat + +import ( + "fmt" + "log" + "net/http" +) + +func Beat(url, status, msg string, ping int) { + resp, err := http.Get(fmt.Sprintf("%s?status=%s&msg=%s&ping=%d", url, status, msg, ping)) + if err != nil { + log.Printf("beat: %v", err) + } + defer resp.Body.Close() +}