From 71ca8016e1d7ff21f475429b71313bf08216a0aa Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Tue, 5 Mar 2024 18:06:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=B6=85=E6=97=B6=E6=9C=AA=E7=94=9F=E6=95=88?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20(#4070)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/dashboard.go | 5 +- backend/app/service/snapshot_recover.go | 2 +- backend/utils/cmd/cmd.go | 90 ++++++++++++++----------- 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/backend/app/service/dashboard.go b/backend/app/service/dashboard.go index b4407d71d..5504b95a9 100644 --- a/backend/app/service/dashboard.go +++ b/backend/app/service/dashboard.go @@ -205,7 +205,10 @@ func loadDiskInfo() []dto.DiskInfo { var datas []dto.DiskInfo stdout, err := cmd.ExecWithTimeOut("df -hT -P|grep '/'|grep -v tmpfs|grep -v 'snap/core'|grep -v udev", 2*time.Second) if err != nil { - return datas + stdout, err = cmd.ExecWithTimeOut("df -lhT -P|grep '/'|grep -v tmpfs|grep -v 'snap/core'|grep -v udev", 1*time.Second) + if err != nil { + return datas + } } lines := strings.Split(stdout, "\n") diff --git a/backend/app/service/snapshot_recover.go b/backend/app/service/snapshot_recover.go index f38a6be33..d81f5a54f 100644 --- a/backend/app/service/snapshot_recover.go +++ b/backend/app/service/snapshot_recover.go @@ -37,7 +37,7 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b global.LOG.Debug("handle backup before recover successful!") req.IsNew = true } - if req.IsNew || snap.InterruptStep == "Download" || (!req.IsNew || req.ReDownload) { + if req.IsNew || snap.InterruptStep == "Download" || req.ReDownload { if err := handleDownloadSnapshot(snap, baseDir); err != nil { updateRecoverStatus(snap.ID, isRecover, "Backup", constant.StatusFailed, err.Error()) return diff --git a/backend/utils/cmd/cmd.go b/backend/utils/cmd/cmd.go index 6ac65aada..ec1e03436 100644 --- a/backend/utils/cmd/cmd.go +++ b/backend/utils/cmd/cmd.go @@ -2,7 +2,6 @@ package cmd import ( "bytes" - "context" "fmt" "os" "os/exec" @@ -14,20 +13,7 @@ import ( ) func Exec(cmdStr string) (string, error) { - ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) - defer cancel() - cmd := exec.Command("bash", "-c", cmdStr) - var stdout, stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - err := cmd.Run() - if ctx.Err() == context.DeadlineExceeded { - return "", buserr.New(constant.ErrCmdTimeout) - } - if err != nil { - return handleErr(stdout, stderr, err) - } - return stdout.String(), nil + return ExecWithTimeOut(cmdStr, 20*time.Second) } func handleErr(stdout, stderr bytes.Buffer, err error) (string, error) { @@ -46,19 +32,28 @@ func handleErr(stdout, stderr bytes.Buffer, err error) (string, error) { } func ExecWithTimeOut(cmdStr string, timeout time.Duration) (string, error) { - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() cmd := exec.Command("bash", "-c", cmdStr) var stdout, stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr - err := cmd.Run() - if ctx.Err() == context.DeadlineExceeded { + if err := cmd.Start(); err != nil { + return "", err + } + done := make(chan error, 1) + go func() { + done <- cmd.Wait() + }() + after := time.After(timeout) + select { + case <-after: + _ = cmd.Process.Kill() return "", buserr.New(constant.ErrCmdTimeout) + case err := <-done: + if err != nil { + return handleErr(stdout, stderr, err) + } } - if err != nil { - return handleErr(stdout, stderr, err) - } + return stdout.String(), nil } @@ -75,9 +70,6 @@ func ExecContainerScript(containerName, cmdStr string, timeout time.Duration) er } func ExecCronjobWithTimeOut(cmdStr, workdir, outPath string, timeout time.Duration) error { - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - file, err := os.OpenFile(outPath, os.O_WRONLY|os.O_CREATE, 0666) if err != nil { return err @@ -88,13 +80,24 @@ func ExecCronjobWithTimeOut(cmdStr, workdir, outPath string, timeout time.Durati cmd.Dir = workdir cmd.Stdout = file cmd.Stderr = file - - err = cmd.Run() - if ctx.Err() == context.DeadlineExceeded { - return buserr.New(constant.ErrCmdTimeout) + if err := cmd.Start(); err != nil { + return err } - - return err + done := make(chan error, 1) + go func() { + done <- cmd.Wait() + }() + after := time.After(timeout) + select { + case <-after: + _ = cmd.Process.Kill() + return buserr.New(constant.ErrCmdTimeout) + case err := <-done: + if err != nil { + return err + } + } + return nil } func Execf(cmdStr string, a ...interface{}) (string, error) { @@ -122,20 +125,29 @@ func ExecWithCheck(name string, a ...string) (string, error) { } func ExecScript(scriptPath, workDir string) (string, error) { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) - defer cancel() cmd := exec.Command("bash", scriptPath) - cmd.Dir = workDir var stdout, stderr bytes.Buffer + cmd.Dir = workDir cmd.Stdout = &stdout cmd.Stderr = &stderr - err := cmd.Run() - if ctx.Err() == context.DeadlineExceeded { + if err := cmd.Start(); err != nil { + return "", err + } + done := make(chan error, 1) + go func() { + done <- cmd.Wait() + }() + after := time.After(10 * time.Minute) + select { + case <-after: + _ = cmd.Process.Kill() return "", buserr.New(constant.ErrCmdTimeout) + case err := <-done: + if err != nil { + return handleErr(stdout, stderr, err) + } } - if err != nil { - return handleErr(stdout, stderr, err) - } + return stdout.String(), nil }