refactor(instance): change IsInstanceRunning to return error instead of bool
Modify IsInstanceRunning function to return error for better error handling and status reporting. Update all related handler functions to properly check the error and set isRunning flag accordingly. Also adjust log levels for service status messages to be more appropriate.
This commit is contained in:
@@ -317,13 +317,13 @@ func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if modifyType == "configFile" {
|
||||
handleConfigFileModify(w, instance, modifiedData, user.Username)
|
||||
handleConfigFileModify(w, instance, modifiedData)
|
||||
} else {
|
||||
handleSystemConfigModify(w, r, instance, modifiedData, user)
|
||||
}
|
||||
}
|
||||
|
||||
func handleConfigFileModify(w http.ResponseWriter, instance FrpcInstance, modifiedData map[string]interface{}, username string) {
|
||||
func handleConfigFileModify(w http.ResponseWriter, instance FrpcInstance, modifiedData map[string]interface{}) {
|
||||
configPath := instance.ConfigPath
|
||||
|
||||
for key, value := range modifiedData {
|
||||
@@ -524,7 +524,14 @@ func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
"name": inst.Name,
|
||||
"createdAt": inst.CreatedAt,
|
||||
"createdBy": inst.CreatedBy,
|
||||
"isRunning": IsInstanceRunning(inst.ID),
|
||||
"isRunning": false,
|
||||
}
|
||||
|
||||
err = IsInstanceRunning(inst.ID)
|
||||
if err != nil {
|
||||
instanceData["isRunning"] = false
|
||||
} else {
|
||||
instanceData["isRunning"] = true
|
||||
}
|
||||
|
||||
instanceList[i] = instanceData
|
||||
@@ -602,7 +609,7 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start Windows service: %v", err))
|
||||
return
|
||||
}
|
||||
postLog.Info(fmt.Sprintf("[StartInstanceHandler] Windows service %s started successfully", serviceName))
|
||||
postLog.Debug(fmt.Sprintf("[StartInstanceHandler] Windows service %s started successfully", serviceName))
|
||||
|
||||
case "systemd":
|
||||
if err := StartSystemdService(serviceName); err != nil {
|
||||
@@ -610,7 +617,7 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start systemd service: %v", err))
|
||||
return
|
||||
}
|
||||
postLog.Info(fmt.Sprintf("[StartInstanceHandler] Systemd service %s started successfully", serviceName))
|
||||
postLog.Debug(fmt.Sprintf("[StartInstanceHandler] Systemd service %s started successfully", serviceName))
|
||||
|
||||
case "init.d":
|
||||
if err := StartInitDService(serviceName); err != nil {
|
||||
@@ -618,7 +625,7 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start init.d service: %v", err))
|
||||
return
|
||||
}
|
||||
postLog.Info(fmt.Sprintf("[StartInstanceHandler] Init.d service %s started successfully", serviceName))
|
||||
postLog.Debug(fmt.Sprintf("[StartInstanceHandler] Init.d service %s started successfully", serviceName))
|
||||
|
||||
default:
|
||||
postLog.Error(fmt.Sprintf("[StartInstanceHandler] Unsupported init system: %s", initType))
|
||||
@@ -634,6 +641,13 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := IsInstanceRunning(instanceID); err != nil {
|
||||
SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start instance %d: %v", instanceID, err))
|
||||
postLog.Warning(fmt.Sprintf("[StartInstanceHandler] Instance %d may not be running: %v", instanceID, err))
|
||||
return
|
||||
}
|
||||
|
||||
postLog.Debug(fmt.Sprintf("[StartInstanceHandler] Instance %d started successfully", instanceID))
|
||||
SendSuccessResponse(w, "Instance started successfully", nil)
|
||||
}
|
||||
|
||||
@@ -893,11 +907,15 @@ func GetInstanceStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
"isRunning": false,
|
||||
}
|
||||
|
||||
isRunning := IsInstanceRunning(instanceID)
|
||||
responseData["isRunning"] = isRunning
|
||||
err = IsInstanceRunning(instanceID)
|
||||
if err != nil {
|
||||
responseData["isRunning"] = false
|
||||
} else {
|
||||
responseData["isRunning"] = true
|
||||
}
|
||||
|
||||
SendSuccessResponse(w, "Instance status retrieved successfully", responseData)
|
||||
postLog.Info(fmt.Sprintf("[GetInstanceStatusHandler] Retrieved status for instance %d (name: %s), isRunning: %v", instanceID, instance.Name, isRunning))
|
||||
postLog.Info(fmt.Sprintf("[GetInstanceStatusHandler] Retrieved status for instance %d (name: %s), isRunning: %v", instanceID, instance.Name, responseData["isRunning"]))
|
||||
}
|
||||
|
||||
func GetInstanceInfoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -953,20 +971,25 @@ func GetInstanceInfoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
isRunning := IsInstanceRunning(instanceID)
|
||||
|
||||
responseData := map[string]interface{}{
|
||||
"name": instance.Name,
|
||||
"serviceName": serviceName,
|
||||
"createdAt": instance.CreatedAt,
|
||||
"createdBy": instance.CreatedBy,
|
||||
"isRunning": isRunning,
|
||||
"isRunning": false,
|
||||
"auth_method": "",
|
||||
"bootAtStart": instance.BootAtStart,
|
||||
"runUser": instance.RunUser,
|
||||
"configPath": instance.ConfigPath,
|
||||
}
|
||||
|
||||
err = IsInstanceRunning(instanceID)
|
||||
if err != nil {
|
||||
responseData["isRunning"] = false
|
||||
} else {
|
||||
responseData["isRunning"] = true
|
||||
}
|
||||
|
||||
configContent, err := os.ReadFile(instance.ConfigPath)
|
||||
if err == nil {
|
||||
config, err := decodeFrpcConfig(string(configContent))
|
||||
|
||||
Reference in New Issue
Block a user