- Change webhook config headers and body from map to string format - Move command parsing logic to separate function in command.go - Add debug logging for webhook operations - Improve exception handling with proper JSON parsing - Simplify config loading logic with default values
87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
package watchdog
|
|
|
|
import (
|
|
"fmt"
|
|
"super-frpc/postLog"
|
|
"super-frpc/utils"
|
|
)
|
|
|
|
func AddInstance(serviceName string) bool {
|
|
postLog.Debug(fmt.Sprintf("[watchdog] Add service monitor: %s", serviceName))
|
|
if !IsConnected() {
|
|
return false
|
|
}
|
|
|
|
message := fmt.Sprintf("[monitor.add] <serviceName>%s</serviceName>", serviceName)
|
|
response, err := sendMsg(message, 3)
|
|
if err != nil {
|
|
return false
|
|
} else if response == "success" {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func RemoveInstance(serviceName string) bool {
|
|
postLog.Debug(fmt.Sprintf("[watchdog] Remove service monitor: %s", serviceName))
|
|
if !IsConnected() {
|
|
return false
|
|
}
|
|
|
|
message := fmt.Sprintf("[monitor.remove] <serviceName>%s</serviceName>", serviceName)
|
|
response, err := sendMsg(message, 3)
|
|
if err != nil {
|
|
return false
|
|
} else if response == "success" {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func CloseInstance(serviceName string) bool {
|
|
postLog.Debug(fmt.Sprintf("[watchdog] Remove service monitor: %s", serviceName))
|
|
if !IsConnected() {
|
|
return false
|
|
}
|
|
|
|
message := fmt.Sprintf("[instance.remove] <serviceName>%s</serviceName>", serviceName)
|
|
response, err := sendMsg(message, 3)
|
|
if err != nil {
|
|
return false
|
|
} else if response == "success" {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func Close() bool {
|
|
if !IsConnected() {
|
|
return false
|
|
}
|
|
|
|
message := "watchdog.shutdown"
|
|
response, err := sendMsg(message, 3)
|
|
if err != nil {
|
|
return false
|
|
} else if response == "success" {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func parseCommand(cmd string) (result string, err error) {
|
|
cmdType := utils.GetCmdType(cmd)
|
|
if cmdType == "Exception" {
|
|
// Watchdog msg: [Exception] <exceptionType>...</exceptionType> <serviceName>...</serviceName> <errorMsg>...</errorMsg>
|
|
exceptionType := utils.GetCmdParams(cmd, "exceptionType")
|
|
serviceName := utils.GetCmdParams(cmd, "serviceName")
|
|
errorMsg := utils.GetCmdParams(cmd, "errorMsg")
|
|
postLog.Error(fmt.Sprintf("[Watchdog] Exception[%s]: %s, %s", serviceName, exceptionType, errorMsg))
|
|
exceptionHandle(exceptionType, serviceName, errorMsg)
|
|
}
|
|
return "", nil
|
|
} |