[dashboard 0.4.1 & agent 0.3.4] 🚸 添加计划任务文案、计划任务超时

This commit is contained in:
naiba
2021-01-29 09:22:24 +08:00
parent 70e5336fc1
commit 67230ee4b8
4 changed files with 22 additions and 9 deletions

View File

@@ -232,19 +232,32 @@ func doTask(task *pb.Task) {
case model.TaskTypeCommand:
startedAt := time.Now()
var cmd *exec.Cmd
var resChan chan string
var errChan chan string
timeout := time.NewTimer(time.Minute * 30)
if utils.IsWindows() {
cmd = exec.Command("cmd", "/c", task.GetData())
} else {
cmd = exec.Command("sh", "-c", task.GetData())
}
output, err := cmd.Output()
result.Delay = float32(time.Now().Sub(startedAt).Seconds())
if err != nil {
result.Data = fmt.Sprintf("%s\n%s", string(output), err.Error())
} else {
result.Data = string(output)
go func(resChan, errChan chan string) {
output, err := cmd.Output()
if err != nil {
errChan <- fmt.Sprintf("%s\n%s", string(output), err.Error())
return
}
resChan <- string(output)
}(resChan, errChan)
select {
case <-timeout.C:
result.Data = "任务执行超时30分钟"
case output := <-resChan:
result.Data = output
result.Successful = true
case errString := <-errChan:
result.Data = errString
}
result.Delay = float32(time.Now().Sub(startedAt).Seconds())
default:
log.Printf("Unknown action: %v", task)
}