From 7266e2afbda87b85f0c32311a4654b1cbffb8aad Mon Sep 17 00:00:00 2001 From: ssongliu Date: Mon, 6 Feb 2023 11:42:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BF=AB=E7=85=A7=E5=A2=9E=E5=8A=A0=20d?= =?UTF-8?q?ocker=20=E6=9C=8D=E5=8A=A1=E6=88=90=E5=8A=9F=E7=AD=89=E5=BE=85?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/docker.go | 10 ++++----- backend/app/service/snapshot.go | 39 +++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/backend/app/service/docker.go b/backend/app/service/docker.go index 05f3c525e..2af151321 100644 --- a/backend/app/service/docker.go +++ b/backend/app/service/docker.go @@ -12,6 +12,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/constant" + "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/pkg/errors" ) @@ -40,11 +41,10 @@ type daemonJsonItem struct { func (u *DockerService) LoadDockerStatus() string { status := constant.StatusRunning - // cmd := exec.Command("systemctl", "is-active", "docker") - // stdout, err := cmd.CombinedOutput() - // if string(stdout) != "active\n" || err != nil { - // status = constant.Stopped - // } + stdout, err := cmd.Exec("systemctl is-active docker") + if string(stdout) != "active\n" || err != nil { + status = constant.Stopped + } return status } diff --git a/backend/app/service/snapshot.go b/backend/app/service/snapshot.go index 74bc28fe4..9bf4c597b 100644 --- a/backend/app/service/snapshot.go +++ b/backend/app/service/snapshot.go @@ -312,8 +312,7 @@ func (u *SnapshotService) SnapshotRecover(req dto.SnapshotRecover) error { } _ = os.RemoveAll(rootDir) global.LOG.Info("recover successful") - _, _ = cmd.Exec("systemctl daemon-reload") - _, _ = cmd.Exec("systemctl restart 1panel.service") + _, _ = cmd.Exec("systemctl daemon-reload && systemctl restart 1panel.service") updateRecoverStatus(snap.ID, "", constant.StatusSuccess, "") }() return nil @@ -371,8 +370,7 @@ func (u *SnapshotService) SnapshotRollback(req dto.SnapshotRecover) error { } } if snap.InterruptStep == "UpdateLiveRestore" { - _, _ = cmd.Exec("systemctl daemon-reload") - _, _ = cmd.Exec("systemctl restart 1panel.service") + _, _ = cmd.Exec("systemctl restart dockere") return nil } @@ -381,8 +379,6 @@ func (u *SnapshotService) SnapshotRollback(req dto.SnapshotRecover) error { return err } if snap.InterruptStep == "1PanelBinary" { - _, _ = cmd.Exec("systemctl daemon-reload") - _, _ = cmd.Exec("systemctl restart 1panel.service") return nil } @@ -391,8 +387,6 @@ func (u *SnapshotService) SnapshotRollback(req dto.SnapshotRecover) error { return err } if snap.InterruptStep == "1PctlBinary" { - _, _ = cmd.Exec("systemctl daemon-reload") - _, _ = cmd.Exec("systemctl restart 1panel.service") return nil } @@ -401,8 +395,7 @@ func (u *SnapshotService) SnapshotRollback(req dto.SnapshotRecover) error { return err } if snap.InterruptStep == "1PanelService" { - _, _ = cmd.Exec("systemctl daemon-reload") - _, _ = cmd.Exec("systemctl restart 1panel.service") + _, _ = cmd.Exec("systemctl daemon-reload && systemctl restart 1panel.service") return nil } @@ -411,8 +404,7 @@ func (u *SnapshotService) SnapshotRollback(req dto.SnapshotRecover) error { return err } if snap.InterruptStep == "1PanelBackups" { - _, _ = cmd.Exec("systemctl daemon-reload") - _, _ = cmd.Exec("systemctl restart 1panel.service") + _, _ = cmd.Exec("systemctl daemon-reload && systemctl restart 1panel.service") return nil } @@ -421,15 +413,13 @@ func (u *SnapshotService) SnapshotRollback(req dto.SnapshotRecover) error { return err } if snap.InterruptStep == "1PanelData" { - _, _ = cmd.Exec("systemctl daemon-reload") - _, _ = cmd.Exec("systemctl restart 1panel.service") + _, _ = cmd.Exec("systemctl daemon-reload && systemctl restart 1panel.service") return nil } _ = os.RemoveAll(rootDir) global.LOG.Info("rollback successful") - _, _ = cmd.Exec("systemctl daemon-reload") - _, _ = cmd.Exec("systemctl restart 1panel.service") + _, _ = cmd.Exec("systemctl daemon-reload && systemctl restart 1panel.service") updateRollbackStatus(snap.ID, constant.StatusSuccess, "") return nil } @@ -764,7 +754,22 @@ func (u *SnapshotService) updateLiveRestore(enabled bool) error { if err != nil { return errors.New(stdout) } - time.Sleep(5 * time.Second) + + ticker := time.NewTicker(3 * time.Second) + ctx, cancle := context.WithTimeout(context.Background(), time.Second*30) + defer cancle() + for range ticker.C { + select { + case <-ctx.Done(): + return errors.New("the docker service cannot be restarted") + default: + stdout, err := cmd.Exec("systemctl is-active docker") + if string(stdout) == "active\n" && err == nil { + global.LOG.Info("docker restart with new live-restore successful!") + return nil + } + } + } return nil }