feat: add Kuma push monitoring functionality with retry logic

This commit is contained in:
Space-Banane
2026-01-27 17:32:30 +01:00
parent 99281d209a
commit 7005d4588d
2 changed files with 54 additions and 1 deletions

51
bot.go
View File

@@ -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.
//

View File

@@ -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
# Optional: Set Kuma monitoring variables in .env file:
# Kuma-Push-Url=https://your-uptime-kuma-instance.com/api/push/xxxxx
# Kuma-Push-Interval=60