fix(instance): add status verification after service operations

Add verification checks to ensure service is running/stopped after start/stop/restart operations
Fix import order and remove unnecessary whitespace
This commit is contained in:
2026-05-05 12:31:59 +08:00
parent 20c906c8cc
commit e72c142b16

View File

@@ -13,11 +13,11 @@ import (
"super-frpc/config"
"super-frpc/database"
"super-frpc/utils"
"super-frpc/global"
"super-frpc/postLog"
"super-frpc/service"
"super-frpc/utils"
"super-frpc/watchdog"
"super-frpc/global"
)
func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
@@ -606,6 +606,12 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
postLog.Debug(fmt.Sprintf("[StartInstanceHandler] Windows service %s started successfully", serviceName))
time.Sleep(time.Millisecond * 200)
if err := service.IsInstanceRunning(instanceID); err != nil {
postLog.Error(fmt.Sprintf("[StartInstanceHandler] Windows service %s failed to start: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start Windows service: %v", err))
return
}
case "systemd":
if err := service.StartSystemdService(serviceName); err != nil {
@@ -614,6 +620,12 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
postLog.Debug(fmt.Sprintf("[StartInstanceHandler] Systemd service %s started successfully", serviceName))
time.Sleep(time.Millisecond * 200)
if err := service.IsInstanceRunning(instanceID); err != nil {
postLog.Error(fmt.Sprintf("[StartInstanceHandler] Systemd service %s failed to start: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start systemd service: %v", err))
return
}
case "init.d":
if err := service.StartInitDService(serviceName); err != nil {
@@ -622,6 +634,12 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
return
}
postLog.Debug(fmt.Sprintf("[StartInstanceHandler] Init.d service %s started successfully", serviceName))
time.Sleep(time.Millisecond * 200)
if err := service.IsInstanceRunning(instanceID); err != nil {
postLog.Error(fmt.Sprintf("[StartInstanceHandler] Init.d service %s failed to start: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start init.d service: %v", err))
return
}
default:
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Unsupported init system: %s", initType))
@@ -632,19 +650,18 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
if !watchdog.AddInstance(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,
"instanceID": instanceID,
"serviceName": serviceName,
})
return
} else {
utils.SendSuccessResponse(w, "Instance started successfully", map[string]interface{}{
"instanceID": instanceID,
"instanceID": instanceID,
"serviceName": serviceName,
})
}
}
postLog.Info(fmt.Sprintf("[StartInstanceHandler] Instance %d started successfully", instanceID))
}
@@ -716,6 +733,12 @@ func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to stop Windows service: %v", err))
return
}
time.Sleep(time.Millisecond * 200)
if err := service.IsInstanceRunning(instanceID); err == nil {
postLog.Error(fmt.Sprintf("[StopInstanceHandler] Windows service %s failed to stop: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to stop Windows service: %v", err))
return
}
postLog.Debug(fmt.Sprintf("[StopInstanceHandler] Windows service %s stopped successfully", serviceName))
case "systemd":
@@ -728,6 +751,7 @@ func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {
case "init.d":
if err := service.StopInitDService(serviceName); err != nil {
time.Sleep(time.Millisecond * 200)
postLog.Error(fmt.Sprintf("[StopInstanceHandler] Failed to stop init.d service %s: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to stop init.d service: %v", err))
return
@@ -743,18 +767,17 @@ func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {
if !watchdog.RemoveInstance(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,
"instanceID": instanceID,
"serviceName": serviceName,
})
} else {
utils.SendSuccessResponse(w, "Instance stopped successfully", map[string]interface{}{
"instanceID": instanceID,
"instanceID": instanceID,
"serviceName": serviceName,
})
}
}
postLog.Info(fmt.Sprintf("[StopInstanceHandler] Instance %d stopped successfully", instanceID))
}
@@ -823,26 +846,36 @@ func RestartInstanceHandler(w http.ResponseWriter, r *http.Request) {
case "windows":
if err := service.RestartWindowsService(serviceName); err != nil {
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] Failed to restart Windows service %s: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to restart Windows service: %v", err))
return
time.Sleep(time.Millisecond * 200)
if err := service.IsInstanceRunning(instanceID); err != nil {
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] Windows service %s restarted but is not running: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Windows service restarted but is not running: %v", err))
return
}
postLog.Debug(fmt.Sprintf("[RestartInstanceHandler] Windows service %s restarted successfully", serviceName))
}
postLog.Debug(fmt.Sprintf("[RestartInstanceHandler] Windows service %s restarted successfully", serviceName))
case "systemd":
if err := service.RestartSystemdService(serviceName); err != nil {
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] Failed to restart systemd service %s: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to restart systemd service: %v", err))
return
time.Sleep(time.Millisecond * 200)
if err := service.IsInstanceRunning(instanceID); err != nil {
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] Failed to check if systemd service %s is running after restart: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to check if systemd service is running after restart: %v", err))
return
}
postLog.Debug(fmt.Sprintf("[RestartInstanceHandler] Systemd service %s restarted successfully", serviceName))
}
postLog.Debug(fmt.Sprintf("[RestartInstanceHandler] Systemd service %s restarted successfully", serviceName))
case "init.d":
if err := service.RestartInitDService(serviceName); err != nil {
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] Failed to restart init.d service %s: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to restart init.d service: %v", err))
return
time.Sleep(time.Millisecond * 200)
if err := service.IsInstanceRunning(instanceID); err != nil {
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] Failed to check init.d service %s status after restart: %v", serviceName, err))
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to check init.d service status after restart: %v", err))
return
}
postLog.Debug(fmt.Sprintf("[RestartInstanceHandler] Init.d service %s restarted successfully", serviceName))
}
postLog.Debug(fmt.Sprintf("[RestartInstanceHandler] Init.d service %s restarted successfully", serviceName))
default:
utils.SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Unsupported init system: %s", initType))
@@ -850,7 +883,7 @@ func RestartInstanceHandler(w http.ResponseWriter, r *http.Request) {
}
utils.SendSuccessResponse(w, "Instance restarted successfully", map[string]interface{}{
"instanceID": instanceID,
"instanceID": instanceID,
"serviceName": serviceName,
})
postLog.Info(fmt.Sprintf("[RestartInstanceHandler] Instance %d restarted successfully", instanceID))