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:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user