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" {
|
if modifyType == "configFile" {
|
||||||
handleConfigFileModify(w, instance, modifiedData, user.Username)
|
handleConfigFileModify(w, instance, modifiedData)
|
||||||
} else {
|
} else {
|
||||||
handleSystemConfigModify(w, r, instance, modifiedData, user)
|
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
|
configPath := instance.ConfigPath
|
||||||
|
|
||||||
for key, value := range modifiedData {
|
for key, value := range modifiedData {
|
||||||
@@ -524,7 +524,14 @@ func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
"name": inst.Name,
|
"name": inst.Name,
|
||||||
"createdAt": inst.CreatedAt,
|
"createdAt": inst.CreatedAt,
|
||||||
"createdBy": inst.CreatedBy,
|
"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
|
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))
|
SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start Windows service: %v", err))
|
||||||
return
|
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":
|
case "systemd":
|
||||||
if err := StartSystemdService(serviceName); err != nil {
|
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))
|
SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start systemd service: %v", err))
|
||||||
return
|
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":
|
case "init.d":
|
||||||
if err := StartInitDService(serviceName); err != nil {
|
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))
|
SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start init.d service: %v", err))
|
||||||
return
|
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:
|
default:
|
||||||
postLog.Error(fmt.Sprintf("[StartInstanceHandler] Unsupported init system: %s", initType))
|
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)
|
SendSuccessResponse(w, "Instance started successfully", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -893,11 +907,15 @@ func GetInstanceStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
"isRunning": false,
|
"isRunning": false,
|
||||||
}
|
}
|
||||||
|
|
||||||
isRunning := IsInstanceRunning(instanceID)
|
err = IsInstanceRunning(instanceID)
|
||||||
responseData["isRunning"] = isRunning
|
if err != nil {
|
||||||
|
responseData["isRunning"] = false
|
||||||
|
} else {
|
||||||
|
responseData["isRunning"] = true
|
||||||
|
}
|
||||||
|
|
||||||
SendSuccessResponse(w, "Instance status retrieved successfully", responseData)
|
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) {
|
func GetInstanceInfoHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -953,20 +971,25 @@ func GetInstanceInfoHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
isRunning := IsInstanceRunning(instanceID)
|
|
||||||
|
|
||||||
responseData := map[string]interface{}{
|
responseData := map[string]interface{}{
|
||||||
"name": instance.Name,
|
"name": instance.Name,
|
||||||
"serviceName": serviceName,
|
"serviceName": serviceName,
|
||||||
"createdAt": instance.CreatedAt,
|
"createdAt": instance.CreatedAt,
|
||||||
"createdBy": instance.CreatedBy,
|
"createdBy": instance.CreatedBy,
|
||||||
"isRunning": isRunning,
|
"isRunning": false,
|
||||||
"auth_method": "",
|
"auth_method": "",
|
||||||
"bootAtStart": instance.BootAtStart,
|
"bootAtStart": instance.BootAtStart,
|
||||||
"runUser": instance.RunUser,
|
"runUser": instance.RunUser,
|
||||||
"configPath": instance.ConfigPath,
|
"configPath": instance.ConfigPath,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = IsInstanceRunning(instanceID)
|
||||||
|
if err != nil {
|
||||||
|
responseData["isRunning"] = false
|
||||||
|
} else {
|
||||||
|
responseData["isRunning"] = true
|
||||||
|
}
|
||||||
|
|
||||||
configContent, err := os.ReadFile(instance.ConfigPath)
|
configContent, err := os.ReadFile(instance.ConfigPath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
config, err := decodeFrpcConfig(string(configContent))
|
config, err := decodeFrpcConfig(string(configContent))
|
||||||
|
|||||||
@@ -391,18 +391,18 @@ func removeBootService(instanceID int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsInstanceRunning(instanceID int) bool {
|
func IsInstanceRunning(instanceID int) error {
|
||||||
initType := GetInitSystem()
|
initType := GetInitSystem()
|
||||||
serviceName, err := GetServiceNameByInstanceID(instanceID)
|
serviceName, err := GetServiceNameByInstanceID(instanceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to get service name: %v", err))
|
postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to get service name: %v", err))
|
||||||
return false
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
instance, err := DBQueryFrpcInstanceByID(instanceID)
|
instance, err := DBQueryFrpcInstanceByID(instanceID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to query instance: %v", err))
|
postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to query instance: %v", err))
|
||||||
return false
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch initType {
|
switch initType {
|
||||||
@@ -415,15 +415,15 @@ func IsInstanceRunning(instanceID int) bool {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to check Windows service status: %s, output: %s", err, output))
|
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") {
|
if strings.Contains(outputStr, "RUNNING") {
|
||||||
postLog.Info(fmt.Sprintf("[IsInstanceRunning] Windows service %s is running", instance.Name))
|
postLog.Info(fmt.Sprintf("[IsInstanceRunning] Windows service %s is running", instance.Name))
|
||||||
return true
|
return nil
|
||||||
} else {
|
} else {
|
||||||
postLog.Info(fmt.Sprintf("[IsInstanceRunning] Windows service %s is stopped", instance.Name))
|
postLog.Info(fmt.Sprintf("[IsInstanceRunning] Windows service %s is stopped", instance.Name))
|
||||||
return false
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
case "systemd":
|
case "systemd":
|
||||||
@@ -436,32 +436,36 @@ func IsInstanceRunning(instanceID int) bool {
|
|||||||
exitError, ok := err.(*exec.ExitError)
|
exitError, ok := err.(*exec.ExitError)
|
||||||
if ok {
|
if ok {
|
||||||
exitCode := exitError.ExitCode()
|
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" {
|
if exitCode == 3 && status == "inactive" {
|
||||||
postLog.Info(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is stopped", serviceName))
|
postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is inactive", serviceName))
|
||||||
return false
|
return fmt.Errorf("service %s is inactive", serviceName)
|
||||||
}
|
}
|
||||||
if exitCode == 4 && status == "unknown" {
|
if exitCode == 4 && status == "unknown" {
|
||||||
postLog.Warning(fmt.Sprintf("[IsInstanceRunning] Systemd service %s does not exist", serviceName))
|
postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s does not exist", serviceName))
|
||||||
return false
|
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))
|
postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to check systemd service status: %s, output: %s", err, output))
|
||||||
return false
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if status == "active" {
|
if status == "active" {
|
||||||
postLog.Info(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is running", serviceName))
|
postLog.Info(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is running", serviceName))
|
||||||
return true
|
return nil
|
||||||
} else {
|
} else {
|
||||||
postLog.Info(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is stopped", serviceName))
|
postLog.Info(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is stopped", serviceName))
|
||||||
return false
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
case "init.d":
|
case "init.d":
|
||||||
servicePath := fmt.Sprintf("/etc/init.d/%s", serviceName)
|
servicePath := fmt.Sprintf("/etc/init.d/%s", serviceName)
|
||||||
if _, err := os.Stat(servicePath); err != nil {
|
if _, err := os.Stat(servicePath); err != nil {
|
||||||
postLog.Warning(fmt.Sprintf("[IsInstanceRunning] Init.d service %s not found", serviceName))
|
postLog.Warning(fmt.Sprintf("[IsInstanceRunning] Init.d service %s not found", serviceName))
|
||||||
return false
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(servicePath, "status")
|
cmd := exec.Command(servicePath, "status")
|
||||||
@@ -475,22 +479,22 @@ func IsInstanceRunning(instanceID int) bool {
|
|||||||
exitCode := exitError.ExitCode()
|
exitCode := exitError.ExitCode()
|
||||||
if exitCode != 0 && !strings.Contains(outputStr, "is running") {
|
if exitCode != 0 && !strings.Contains(outputStr, "is running") {
|
||||||
postLog.Info(fmt.Sprintf("[IsInstanceRunning] Init.d service %s is stopped", serviceName))
|
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))
|
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") {
|
if strings.Contains(outputStr, "is running") {
|
||||||
return true
|
return nil
|
||||||
} else {
|
} else {
|
||||||
return false
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
postLog.Error(fmt.Sprintf("[IsInstanceRunning] Unsupported init system: %s", initType))
|
postLog.Error(fmt.Sprintf("[IsInstanceRunning] Unsupported init system: %s", initType))
|
||||||
return false
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user