mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-06-20 11:22:19 +08:00
32 lines
541 B
Go
32 lines
541 B
Go
package timeutils2
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"runtime/debug"
|
|
"time"
|
|
|
|
"yunion.io/x/log"
|
|
)
|
|
|
|
func AddTimeout(second time.Duration, callback func()) {
|
|
go func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
log.Errorln(r)
|
|
debug.PrintStack()
|
|
}
|
|
}()
|
|
|
|
<-time.NewTimer(second).C
|
|
callback()
|
|
}()
|
|
}
|
|
|
|
func CommandWithTimeout(timeout int, cmds ...string) *exec.Cmd {
|
|
if timeout > 0 {
|
|
cmds = append([]string{"timeout", "--signal=KILL", fmt.Sprintf("%ds", timeout)}, cmds...)
|
|
}
|
|
return exec.Command(cmds[0], cmds[1:]...)
|
|
}
|