Quellcode durchsuchen

进一步优化updateServiceStatus()函数

niujiuru vor 2 Tagen
Ursprung
Commit
3aeed4160e
1 geänderte Dateien mit 18 neuen und 4 gelöschten Zeilen
  1. 18 4
      web-ui.service/page1_handler.go

+ 18 - 4
web-ui.service/page1_handler.go

@@ -366,6 +366,7 @@ func updateServiceStatus() {
 		"-p", "LoadState",
 		"-p", "ActiveState",
 		"-p", "SubState",
+		"-p", "Result",
 	}
 
 	for _, s := range services {
@@ -376,7 +377,7 @@ func updateServiceStatus() {
 
 	out, _ := exec.Command("systemctl", args...).CombinedOutput()
 
-	var name, loadState, activeState, subState string
+	var name, loadState, activeState, subState, result string
 
 	save := func() {
 		if name == "" {
@@ -392,25 +393,38 @@ func updateServiceStatus() {
 			status = "🔵已完成"
 		case loadState == "loaded" && activeState == "inactive":
 			status = "🟡已停止"
+		case activeState == "failed" && result == "timeout":
+			status = "🔴超时停"
+		case activeState == "failed":
+			status = "🔴异常停"
 		}
 
 		statusMap[name] = status
 	}
 
 	for line := range strings.SplitSeq(string(out), "\n") {
-		if strings.HasPrefix(line, "Id=") {
-			save() // Save the previous service status before starting a new one
+		if line == "" { // Empty line indicates the end of a service status block
+			save() ///// Save the previous service status before starting a new one
 
-			name = strings.TrimPrefix(line, "Id=")
+			name = ""
 			loadState = ""
 			activeState = ""
 			subState = ""
+			result = ""
+
+			continue
+		}
+
+		if v, ok := strings.CutPrefix(line, "Id="); ok {
+			name = v
 		} else if v, ok := strings.CutPrefix(line, "LoadState="); ok {
 			loadState = v
 		} else if v, ok := strings.CutPrefix(line, "ActiveState="); ok {
 			activeState = v
 		} else if v, ok := strings.CutPrefix(line, "SubState="); ok {
 			subState = v
+		} else if v, ok := strings.CutPrefix(line, "Result="); ok {
+			result = v
 		}
 	}