Skip to content

Commit

Permalink
fix: 解决命令执行超时未生效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ssongliu committed Mar 5, 2024
1 parent 5633bdd commit 894a355
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
5 changes: 4 additions & 1 deletion backend/app/service/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", 2*time.Second)
if err != nil {
return datas
}
}
lines := strings.Split(stdout, "\n")

Expand Down
2 changes: 1 addition & 1 deletion backend/app/service/snapshot_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions backend/utils/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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)
cmd := exec.CommandContext(ctx, "bash", "-c", cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
Expand Down Expand Up @@ -48,7 +48,7 @@ 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)
cmd := exec.CommandContext(ctx, "bash", "-c", cmdStr)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
Expand Down Expand Up @@ -84,7 +84,7 @@ func ExecCronjobWithTimeOut(cmdStr, workdir, outPath string, timeout time.Durati
}
defer file.Close()

cmd := exec.Command("bash", "-c", cmdStr)
cmd := exec.CommandContext(ctx, "bash", "-c", cmdStr)
cmd.Dir = workdir
cmd.Stdout = file
cmd.Stderr = file
Expand Down Expand Up @@ -124,7 +124,7 @@ 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 := exec.CommandContext(ctx, "bash", scriptPath)
cmd.Dir = workDir
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
Expand Down

0 comments on commit 894a355

Please sign in to comment.