1 Star 0 Fork 0

CNCF/devstatscode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
signal.go 1.75 KB
一键复制 编辑 原始数据 按行查看 历史
Łukasz Gryglicki 提交于 2021-09-19 15:29 +08:00 . Support timeouts
package devstatscode
import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
)
// FinishAfterTimeout - finish program 'prog' after given timeout 'seconds', exit with 'status' code
func FinishAfterTimeout(prog string, seconds, status int) {
time.Sleep(time.Duration(seconds) * time.Second)
Printf("Program '%s' reached timeout after %d seconds, sending signal to exit %d\n", prog, seconds, status)
err := syscall.Kill(syscall.Getpid(), syscall.SIGALRM)
if err != nil {
Printf("Error: %+v sending '%s' timeout signal after %d seconds, exiting %d status\n", err, prog, seconds, status)
os.Exit(status)
return
}
Printf("Program '%s': sent timeout signal after %d seconds, requesting %d exit status\n", prog, seconds, status)
}
// SetupTimeoutSignal - if GHA2DB_MAX_RUN_DURATION contains configuration for 'prog'
// Then it is given as "...,prog:duration:exit_status:,..." - it means that the 'prog'
// can only run 'duration' seconds, and after that time it receives timeout, logs it
// and exists with 'exit_status'
func SetupTimeoutSignal(ctx *Ctx) {
prog := filepath.Base(os.Args[0])
ary := strings.Split(prog, ".")
lAry := len(ary)
if lAry > 1 {
prog = strings.Join(ary[:lAry-1], ".")
}
if ctx.MaxRunDuration == nil {
return
}
data, ok := ctx.MaxRunDuration[prog]
if !ok {
return
}
seconds, status := data[0], data[1]
if data[0] <= 0 {
return
}
go FinishAfterTimeout(prog, seconds, status)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGALRM)
go func() {
for {
sig := <-sigs
Printf("Program '%s': timeout %v after %d seconds, will exit with %d code\n", prog, sig, seconds, status)
os.Exit(status)
}
}()
Printf("Program '%s': timeout handler installed: exit %d after %d seconds\n", prog, status, seconds)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cncf/devstatscode.git
git@gitee.com:cncf/devstatscode.git
cncf
devstatscode
devstatscode
master

搜索帮助