fix: Resolve abnormal state after Docker restart (#9305)
Some checks failed
SonarCloud Scan / SonarCloud (push) Failing after 4s

This commit is contained in:
ssongliu 2025-06-26 19:19:22 +08:00 committed by GitHub
parent 3061d640ce
commit 21bbadc37b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 1 additions and 190 deletions

View file

@ -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)

View file

@ -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
)

View file

@ -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
}

View file

@ -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