fix(watchdog): integrate instance management with watchdog
Add watchdog instance management in Start/StopInstanceHandler to sync with watchdog service Add notification enabled check in exceptionHandle function
This commit is contained in:
+35
-8
@@ -16,6 +16,8 @@ import (
|
|||||||
"super-frpc/utils"
|
"super-frpc/utils"
|
||||||
"super-frpc/postLog"
|
"super-frpc/postLog"
|
||||||
"super-frpc/service"
|
"super-frpc/service"
|
||||||
|
"super-frpc/watchdog"
|
||||||
|
"super-frpc/global"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -626,10 +628,23 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.SendSuccessResponse(w, "Instance started successfully", map[string]interface{}{
|
if global.Is.WatchdogConnected {
|
||||||
"instanceID": instanceID,
|
if !watchdog.AddInstance(serviceName) {
|
||||||
"serviceName": serviceName,
|
postLog.Warning(fmt.Sprintf("[StartInstanceHandler] Failed to add watchdog instance %s", serviceName))
|
||||||
})
|
utils.SendSuccessResponse(w, "Instance started successfully but watchdog instance add failed", map[string]interface{}{
|
||||||
|
"instanceID": instanceID,
|
||||||
|
"serviceName": serviceName,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
utils.SendSuccessResponse(w, "Instance started successfully", map[string]interface{}{
|
||||||
|
"instanceID": instanceID,
|
||||||
|
"serviceName": serviceName,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
postLog.Info(fmt.Sprintf("[StartInstanceHandler] Instance %d started successfully", instanceID))
|
postLog.Info(fmt.Sprintf("[StartInstanceHandler] Instance %d started successfully", instanceID))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -724,10 +739,22 @@ func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.SendSuccessResponse(w, "Instance stopped successfully", map[string]interface{}{
|
if global.Is.WatchdogConnected {
|
||||||
"instanceID": instanceID,
|
if !watchdog.RemoveInstance(serviceName) {
|
||||||
"serviceName": serviceName,
|
postLog.Warning(fmt.Sprintf("[StopInstanceHandler] Failed to remove watchdog instance %s", serviceName))
|
||||||
})
|
utils.SendSuccessResponse(w, "Instance stopped successfully but watchdog instance remove failed", map[string]interface{}{
|
||||||
|
"instanceID": instanceID,
|
||||||
|
"serviceName": serviceName,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
utils.SendSuccessResponse(w, "Instance stopped successfully", map[string]interface{}{
|
||||||
|
"instanceID": instanceID,
|
||||||
|
"serviceName": serviceName,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
postLog.Info(fmt.Sprintf("[StopInstanceHandler] Instance %d stopped successfully", instanceID))
|
postLog.Info(fmt.Sprintf("[StopInstanceHandler] Instance %d stopped successfully", instanceID))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+22
-17
@@ -9,24 +9,29 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func exceptionHandle(exceptionType string, serviceName string, errorMsg string) {
|
func exceptionHandle(exceptionType string, serviceName string, errorMsg string) {
|
||||||
body := make(map[string]string)
|
if global.CurrentConfig.Notification.Enabled {
|
||||||
for k, v := range global.CurrentConfig.Webhook.Body {
|
body := make(map[string]string)
|
||||||
replaced := v
|
if global.CurrentConfig.Notification.Method == "Webhook" {
|
||||||
for strings.Contains(replaced, "{{ exceptionType }}") ||
|
for k, v := range global.CurrentConfig.Webhook.Body {
|
||||||
strings.Contains(replaced, "{{ serviceName }}") ||
|
replaced := v
|
||||||
strings.Contains(replaced, "{{ exceptionMsg }}") {
|
for strings.Contains(replaced, "{{ exceptionType }}") ||
|
||||||
replaced = strings.ReplaceAll(replaced, "{{ exceptionType }}", exceptionType)
|
strings.Contains(replaced, "{{ serviceName }}") ||
|
||||||
replaced = strings.ReplaceAll(replaced, "{{ serviceName }}", serviceName)
|
strings.Contains(replaced, "{{ exceptionMsg }}") {
|
||||||
replaced = strings.ReplaceAll(replaced, "{{ exceptionMsg }}", errorMsg)
|
replaced = strings.ReplaceAll(replaced, "{{ exceptionType }}", exceptionType)
|
||||||
|
replaced = strings.ReplaceAll(replaced, "{{ serviceName }}", serviceName)
|
||||||
|
replaced = strings.ReplaceAll(replaced, "{{ exceptionMsg }}", errorMsg)
|
||||||
|
}
|
||||||
|
body[k] = replaced
|
||||||
|
}
|
||||||
|
|
||||||
|
bodyJSON := ""
|
||||||
|
for k, v := range body {
|
||||||
|
bodyJSON = fmt.Sprintf(`{"%s": "%s"}`, k, v)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
webhook.SendHook(global.CurrentConfig.Webhook.URL, global.CurrentConfig.Webhook.Method, global.CurrentConfig.Webhook.Headers, bodyJSON)
|
||||||
}
|
}
|
||||||
body[k] = replaced
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bodyJSON := ""
|
|
||||||
for k, v := range body {
|
|
||||||
bodyJSON = fmt.Sprintf(`{"%s": "%s"}`, k, v)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
webhook.SendHook(global.CurrentConfig.Webhook.URL, global.CurrentConfig.Webhook.Method, global.CurrentConfig.Webhook.Headers, bodyJSON)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user