refactor(socket): decouple command handling and add message sending

- Move command handler to variable for better flexibility
- Add SendMsg function for sending messages through socket
- Fix missing return statements in command execution
- Improve error handling in monitor exception reporting
This commit is contained in:
2026-04-28 19:55:45 +08:00
parent 3a0b591d5e
commit 58a8efc17a
4 changed files with 67 additions and 28 deletions

View File

@@ -2,6 +2,7 @@ package monitor
import (
"Watchdog_Linux-systemd/postLog"
"Watchdog_Linux-systemd/socket"
"fmt"
"os/exec"
"strings"
@@ -123,7 +124,11 @@ func checkServiceLogs(serviceName string) (bool, error) {
func throwException(serviceName, errorContent string) error {
postLog.Error(fmt.Sprintf("[Monitor] Service: %s - Exception: %s", serviceName, errorContent))
return fmt.Errorf("service %s exception: %s", serviceName, errorContent)
err := socket.SendMsg(fmt.Sprintf("service %s exception: %s", serviceName, errorContent))
if err != nil {
return fmt.Errorf("failed to send exception message: %v", err)
}
return nil
}
func GetActiveMonitors() []string {