From c978e5c6b4714d033b28f26e0c094cf5564bdfff Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Tue, 21 Apr 2026 14:39:30 +0800 Subject: [PATCH] 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. --- frpAct.go | 47 +++++++++++++++++++++++++++++++++++------------ os.go | 42 +++++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 31 deletions(-) diff --git a/frpAct.go b/frpAct.go index 3ad8a11..90cdaa9 100644 --- a/frpAct.go +++ b/frpAct.go @@ -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)) diff --git a/os.go b/os.go index 9b9e232..7b520f2 100644 --- a/os.go +++ b/os.go @@ -391,18 +391,18 @@ func removeBootService(instanceID int) error { return nil } -func IsInstanceRunning(instanceID int) bool { +func IsInstanceRunning(instanceID int) error { initType := GetInitSystem() serviceName, err := GetServiceNameByInstanceID(instanceID) if err != nil { postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to get service name: %v", err)) - return false + return err } instance, err := DBQueryFrpcInstanceByID(instanceID) if err != nil { postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to query instance: %v", err)) - return false + return err } switch initType { @@ -415,15 +415,15 @@ func IsInstanceRunning(instanceID int) bool { if err != nil { postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to check Windows service status: %s, output: %s", err, output)) - return false + return err } if strings.Contains(outputStr, "RUNNING") { postLog.Info(fmt.Sprintf("[IsInstanceRunning] Windows service %s is running", instance.Name)) - return true + return nil } else { postLog.Info(fmt.Sprintf("[IsInstanceRunning] Windows service %s is stopped", instance.Name)) - return false + return nil } case "systemd": @@ -436,32 +436,36 @@ func IsInstanceRunning(instanceID int) bool { exitError, ok := err.(*exec.ExitError) if ok { exitCode := exitError.ExitCode() + if exitCode == 1 && status == "inactive" { + postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is stopped", serviceName)) + return fmt.Errorf("service %s is stopped", serviceName) + } if exitCode == 3 && status == "inactive" { - postLog.Info(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is stopped", serviceName)) - return false + postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is inactive", serviceName)) + return fmt.Errorf("service %s is inactive", serviceName) } if exitCode == 4 && status == "unknown" { - postLog.Warning(fmt.Sprintf("[IsInstanceRunning] Systemd service %s does not exist", serviceName)) - return false + postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s does not exist", serviceName)) + return fmt.Errorf("service %s does not exist", serviceName) } } postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to check systemd service status: %s, output: %s", err, output)) - return false + return err } if status == "active" { postLog.Info(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is running", serviceName)) - return true + return nil } else { postLog.Info(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is stopped", serviceName)) - return false + return nil } case "init.d": servicePath := fmt.Sprintf("/etc/init.d/%s", serviceName) if _, err := os.Stat(servicePath); err != nil { postLog.Warning(fmt.Sprintf("[IsInstanceRunning] Init.d service %s not found", serviceName)) - return false + return nil } cmd := exec.Command(servicePath, "status") @@ -475,22 +479,22 @@ func IsInstanceRunning(instanceID int) bool { exitCode := exitError.ExitCode() if exitCode != 0 && !strings.Contains(outputStr, "is running") { postLog.Info(fmt.Sprintf("[IsInstanceRunning] Init.d service %s is stopped", serviceName)) - return false + return nil } } postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to check init.d service status: %s, output: %s", err, output)) - return false + return err } if strings.Contains(outputStr, "is running") { - return true + return nil } else { - return false + return nil } default: postLog.Error(fmt.Sprintf("[IsInstanceRunning] Unsupported init system: %s", initType)) - return false + return nil } }