From e063a36e2898b5ec544f0cd3a7c981e6aa05c3b1 Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Sun, 10 May 2026 17:40:01 +0800 Subject: [PATCH] feat(watchdog): initialize watchdog connection and sync running instances --- main.go | 1 + sys/core.go | 12 +++++------ watchdog/client.go | 52 ++++++++++++++++++++++++++++++++++++++------- watchdog/connect.go | 7 +++--- 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index e9f2fe8..1add544 100644 --- a/main.go +++ b/main.go @@ -72,6 +72,7 @@ func main() { setupRoutes() + // Initialize watchdog connection err = watchdog.Init() if err != nil { postLog.Error(fmt.Sprintf("Unable to initialize Watchdog: %s", err)) diff --git a/sys/core.go b/sys/core.go index 8589103..f923586 100644 --- a/sys/core.go +++ b/sys/core.go @@ -394,11 +394,11 @@ func IsInstanceRunning(instanceID int) error { if strings.Contains(outputStr, "RUNNING") { postLog.Info(fmt.Sprintf("[IsInstanceRunning] Windows service %s is running", instance.Name)) return nil - } else { - postLog.Info(fmt.Sprintf("[IsInstanceRunning] Windows service %s is stopped", instance.Name)) - return nil } + postLog.Info(fmt.Sprintf("[IsInstanceRunning] Windows service %s is stopped", instance.Name)) + return fmt.Errorf("service %s is not running", serviceName) + case "systemd": cmd := exec.Command("systemctl", "status", serviceName) @@ -465,13 +465,13 @@ func IsInstanceRunning(instanceID int) error { if strings.Contains(outputStr, "is running") { return nil - } else { - return nil } + return fmt.Errorf("service %s is not running", serviceName) + default: postLog.Error(fmt.Sprintf("[IsInstanceRunning] Unsupported init system: %s", initType)) - return nil + return fmt.Errorf("unsupported init system: %s", initType) } } diff --git a/watchdog/client.go b/watchdog/client.go index 270b496..01245e4 100644 --- a/watchdog/client.go +++ b/watchdog/client.go @@ -8,6 +8,7 @@ import ( "os/exec" "path/filepath" "strings" + "super-frpc/database" "super-frpc/global" "super-frpc/postLog" "super-frpc/sys" @@ -21,7 +22,6 @@ const socketPath = "/tmp/super-frpc-watchdog.sock" var ( localConn net.Conn localConnMutex sync.Mutex - isConnected bool recvChan chan string stopRecvChan chan struct{} recvWg sync.WaitGroup @@ -47,15 +47,51 @@ func Init() error { } localConnMutex.Lock() - defer localConnMutex.Unlock() - recvChan = make(chan string, 100) stopRecvChan = make(chan struct{}) - isConnected = false + global.Is.WatchdogConnected = false + localConnMutex.Unlock() + + if global.CurrentConfig.Watchdog.Enabled { + if !Connect() { + postLog.Warning("[watchdog] initial connect failed; caller may retry with Connect") + } + } return nil } +func syncRunningInstancesToWatchdog() { + instances, err := database.DBListFrpcInstances() + if err != nil { + postLog.Error(fmt.Sprintf("[watchdog] failed to list frpc instances: %v", err)) + return + } + + added := 0 + for _, instance := range instances { + if err := sys.IsInstanceRunning(instance.ID); err != nil { + postLog.Debug(fmt.Sprintf("[watchdog] instance %d is not running: %v", instance.ID, err)) + continue + } + + serviceName, err := database.GetServiceNameByInstanceID(instance.ID) + if err != nil { + postLog.Warning(fmt.Sprintf("[watchdog] failed to get service name for instance %d: %v", instance.ID, err)) + continue + } + + if !AddInstance(serviceName) { + postLog.Warning(fmt.Sprintf("[watchdog] failed to add running instance %s to monitor list", serviceName)) + continue + } + + added++ + } + + postLog.Info(fmt.Sprintf("[watchdog] synced %d running instance(s) to monitor list", added)) +} + func ensureWatchdogProcess() error { watchdogName, err := getWatchdogBinaryName() if err != nil { @@ -151,7 +187,7 @@ func localSocketConnect() error { } localConn = conn - isConnected = true + global.Is.WatchdogConnected = true recvWg.Add(1) go recvMsg() @@ -231,7 +267,7 @@ func recvMsg() { if localConn != nil { localConn.Close() localConn = nil - isConnected = false + global.Is.WatchdogConnected = false } localConnMutex.Unlock() return @@ -262,7 +298,7 @@ func Destroy() error { if localConn != nil { err := localConn.Close() localConn = nil - isConnected = false + global.Is.WatchdogConnected = false if err != nil { return fmt.Errorf("failed to close connection: %v", err) } @@ -279,5 +315,5 @@ func Destroy() error { func IsConnected() bool { localConnMutex.Lock() defer localConnMutex.Unlock() - return isConnected && localConn != nil + return global.Is.WatchdogConnected && localConn != nil } diff --git a/watchdog/connect.go b/watchdog/connect.go index cb7cca3..2686bbd 100644 --- a/watchdog/connect.go +++ b/watchdog/connect.go @@ -11,10 +11,6 @@ func Connect() bool { return true } - if err := Init(); err != nil { - return false - } - deadline := time.Now().Add(5 * time.Second) var lastErr error for { @@ -45,7 +41,10 @@ func Connect() bool { return false } + syncRunningInstancesToWatchdog() + return true + } func Disconnect() bool {