From 7005d4588d942a91db547dcd3b7f569ec6fae559 Mon Sep 17 00:00:00 2001 From: Space-Banane Date: Tue, 27 Jan 2026 17:32:30 +0100 Subject: [PATCH] feat: add Kuma push monitoring functionality with retry logic --- bot.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 4 +++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/bot.go b/bot.go index 345b5a7..a23a0d2 100644 --- a/bot.go +++ b/bot.go @@ -4,11 +4,14 @@ import ( "bytes" "encoding/json" "fmt" + "log" "net/http" "os" "os/signal" + "strconv" "strings" "syscall" + "time" "thoughtful-dcbob/commands" @@ -51,6 +54,20 @@ func main() { // continue running; commands may be registered later } + // Start Kuma push monitor if configured + kumaPushURL := os.Getenv("Kuma-Push-Url") + if kumaPushURL != "" { + intervalStr := os.Getenv("Kuma-Push-Interval") + interval := 60 // default to 60 seconds + if intervalStr != "" { + if parsed, err := strconv.Atoi(intervalStr); err == nil && parsed > 0 { + interval = parsed + } + } + go startKumaPush(kumaPushURL, interval) + log.Printf("Kuma push monitor started (URL: %s, Interval: %ds)", kumaPushURL, interval) + } + // Wait here until CTRL-C or other term signal is received. fmt.Println("Bot is now running. Press CTRL-C to exit.") sc := make(chan os.Signal, 1) @@ -61,6 +78,40 @@ func main() { dg.Close() } +// startKumaPush periodically sends GET requests to the Kuma push URL +func startKumaPush(url string, intervalSeconds int) { + ticker := time.NewTicker(time.Duration(intervalSeconds) * time.Second) + defer ticker.Stop() + + for range ticker.C { + pushToKuma(url) + } +} + +// pushToKuma sends a GET request to the Kuma push URL with retry logic +func pushToKuma(url string) { + client := &http.Client{Timeout: 10 * time.Second} + + // First attempt + resp, err := client.Get(url) + if err != nil { + log.Printf("Kuma push failed: %v (retrying in 15s)", err) + time.Sleep(15 * time.Second) + + // Retry attempt + resp, err = client.Get(url) + if err != nil { + log.Printf("Kuma push retry failed: %v", err) + return + } + defer resp.Body.Close() + log.Printf("Kuma push retry succeeded: status %d", resp.StatusCode) + return + } + defer resp.Body.Close() + log.Printf("Kuma push: status %d", resp.StatusCode) +} + // This function will be called (due to AddHandler above) every time a new // message is created on any channel that the authenticated bot has access to. // diff --git a/docker-compose.yml b/docker-compose.yml index c442d3f..b56fd67 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,4 +7,6 @@ services: command: sh -c "git pull && if [ ! -f .env ]; then echo \".env file not found! Please create one based on .env.example and set your DISCORD_BOT_TOKEN.\"; exit 1; fi && go mod tidy && go run ." env_file: - .env - \ No newline at end of file + # Optional: Set Kuma monitoring variables in .env file: + # Kuma-Push-Url=https://your-uptime-kuma-instance.com/api/push/xxxxx + # Kuma-Push-Interval=60