From 21bbadc37b7891d1b53404579fbf2c313b89f7c9 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 26 Jun 2025 19:19:22 +0800 Subject: [PATCH] fix: Resolve abnormal state after Docker restart (#9305) --- backend/app/service/docker.go | 2 +- backend/utils/systemctl/handle.go | 110 --------------------------- backend/utils/systemctl/managers.go | 15 ---- backend/utils/systemctl/systemctl.go | 64 ---------------- 4 files changed, 1 insertion(+), 190 deletions(-) diff --git a/backend/app/service/docker.go b/backend/app/service/docker.go index 55d4e847e..84b7916b5 100644 --- a/backend/app/service/docker.go +++ b/backend/app/service/docker.go @@ -372,7 +372,7 @@ func (u *DockerService) OperateDocker(req dto.DockerOperation) error { if req.Operation == "stop" { socketHandle, err := systemctl.DefaultHandler("docker.socket") if err == nil { - status, err := socketHandle.CheckStatus() + status, err := socketHandle.IsActive() if err == nil && status.IsActive { if std, err := socketHandle.ExecuteAction("stop"); err != nil { global.LOG.Errorf("handle stop docker.socket failed, err: %v", std) diff --git a/backend/utils/systemctl/handle.go b/backend/utils/systemctl/handle.go index 9cdf9126d..323d3228a 100644 --- a/backend/utils/systemctl/handle.go +++ b/backend/utils/systemctl/handle.go @@ -6,7 +6,6 @@ import ( "fmt" "path/filepath" "strings" - "sync" "time" "github.com/1Panel-dev/1Panel/backend/global" @@ -148,102 +147,6 @@ func (h *ServiceHandler) ExecuteAction(action string) (ServiceResult, error) { return h.executeAction(action, successMsg) } -func (h *ServiceHandler) CheckStatus() (ServiceStatus, error) { - manager := GetGlobalManager() - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - type result struct { - isActive bool - isEnabled bool - output string - err error - } - var status ServiceStatus - var errs []error - - results := make(chan result, 2) - var wg sync.WaitGroup - wg.Add(2) - - go func() { - defer wg.Done() - res := result{} - cmd, err := manager.BuildCommand("status", h.config) - if err != nil { - res.err = fmt.Errorf("build status command failed: %w", err) - results <- res - return - } - - output, err := executeCommand(ctx, cmd[0], cmd[1:]...) - if err != nil { - res.err = fmt.Errorf("status check failed: %w", err) - results <- res - return - } - - isActive, err := manager.ParseStatus(string(output), h.config, "active") - if err != nil { - res.err = fmt.Errorf("parse status failed: %w", err) - results <- res - return - } - res.isActive = isActive - res.output = string(output) - results <- res - }() - - go func() { - defer wg.Done() - res := result{} - cmd, err := manager.BuildCommand("is-enabled", h.config) - if err != nil { - res.err = fmt.Errorf("build is-enabled command failed: %w", err) - results <- res - return - } - - output, err := executeCommand(ctx, cmd[0], cmd[1:]...) - if err != nil { - res.err = fmt.Errorf("enabled check failed: %w", err) - results <- res - return - } - - isEnabled, err := manager.ParseStatus(string(output), h.config, "enabled") - if err != nil { - res.err = fmt.Errorf("parse enabled status failed: %w", err) - results <- res - return - } - res.isEnabled = isEnabled - results <- res - }() - - go func() { - wg.Wait() - close(results) - }() - - for res := range results { - if res.err != nil { - errs = append(errs, res.err) - continue - } - status.IsActive = res.isActive - status.IsEnabled = res.isEnabled - if res.output != "" { - status.Output = res.output - } - } - - if len(errs) > 0 { - return status, errors.Join(errs...) - } - return status, nil -} - func (h *ServiceHandler) IsExists() (ServiceStatus, error) { manager := GetGlobalManager() isExist, _ := manager.ServiceExists(h.config) @@ -378,16 +281,3 @@ func (h *ServiceHandler) executeAction(action, successMsg string) (ServiceResult Output: string(output), }, nil } - -func (h *ServiceHandler) ReloadManager() error { - if err := ReinitializeManager(); err != nil { - global.LOG.Errorf("Failed to reload service manager: %v", err) - return fmt.Errorf("failed to reload service manager: %w", err) - } - global.LOG.Info("Service manager reloaded successfully") - return nil -} - -var ( - ExecuteCommand = executeCommand -) diff --git a/backend/utils/systemctl/managers.go b/backend/utils/systemctl/managers.go index f0ea29225..73e7c4273 100644 --- a/backend/utils/systemctl/managers.go +++ b/backend/utils/systemctl/managers.go @@ -438,18 +438,3 @@ func (e CommandError) Error() string { return fmt.Sprintf("command %q failed: %v \nOutput: %s", e.Cmd, e.Err, e.Output) } - -func (e CommandError) Unwrap() error { return e.Err } - -func ReinitializeManager() error { - mu.Lock() - defer mu.Unlock() - globalManager = nil - return InitializeGlobalManager() -} - -func SetManagerPriority(order []string) { - mu.Lock() - defer mu.Unlock() - managerPriority = order -} diff --git a/backend/utils/systemctl/systemctl.go b/backend/utils/systemctl/systemctl.go index ea9793678..b3e81b745 100644 --- a/backend/utils/systemctl/systemctl.go +++ b/backend/utils/systemctl/systemctl.go @@ -1,10 +1,8 @@ package systemctl import ( - "context" "fmt" "os" - "time" "github.com/1Panel-dev/1Panel/backend/global" ) @@ -98,36 +96,6 @@ func Restart(serviceName string) error { return nil } -func SafeRestart(service string, configPaths []string) error { - for _, path := range configPaths { - if !FileExist(path) { - global.LOG.Errorf("Config file missing: %s", path) - return fmt.Errorf("config file missing: %s", path) - } - } - - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - if _, err := executeCommand(ctx, "check", service); err != nil { - global.LOG.Errorf("Config test failed: %v", err) - return fmt.Errorf("config test failed: %w", err) - } - - if err := Restart(service); err != nil { - global.LOG.Errorf("SafeRestart failed: %v", err) - return err - } - - isActive, _, err := Status(service) - if err != nil || !isActive { - global.LOG.Error("Service not active after safe restart") - return fmt.Errorf("service not active after restart") - } - - return nil -} - func Enable(serviceName string) error { handler, err := DefaultHandler(serviceName) if err != nil { @@ -152,20 +120,6 @@ func Disable(serviceName string) error { return nil } -func Status(serviceName string) (isActive bool, isEnabled bool, err error) { - handler, err := DefaultHandler(serviceName) - if err != nil { - global.LOG.Errorf("Status handler init failed: %v", err) - return false, false, fmt.Errorf("%s is not exist", serviceName) - } - status, err := handler.CheckStatus() - if err != nil { - global.LOG.Errorf("Status check failed: %v", err) - return false, false, fmt.Errorf("status check failed: %v | Output: %s", err, status.Output) - } - return status.IsActive, status.IsEnabled, nil -} - func IsActive(serviceName string) (bool, error) { handler, err := DefaultHandler(serviceName) if err != nil { @@ -194,24 +148,6 @@ type LogOption struct { TailLines string } -func ViewLog(path string, opt LogOption) (string, error) { - if !FileExist(path) { - return "", fmt.Errorf("log file not found: %s", path) - } - args := []string{"-n", opt.TailLines, path} - if opt.TailLines == "+1" { - args = []string{"-n", "1", path} - } - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - - output, err := executeCommand(ctx, "tail", args...) - if err != nil { - return "", fmt.Errorf("tail failed: %w | Output: %s", err, string(output)) - } - return string(output), nil -} - func FileExist(path string) bool { _, err := os.Stat(path) return err == nil