feat(watchdog): initialize watchdog connection and sync running instances
This commit is contained in:
+44
-8
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user