mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-12 10:11:51 +00:00
fix(script): kill script process and sub process on timeout
This commit is contained in:
parent
eb699dd466
commit
92ef4e4054
|
@ -7,6 +7,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ncarlier/webhookd/pkg/logger"
|
"github.com/ncarlier/webhookd/pkg/logger"
|
||||||
|
@ -42,6 +43,8 @@ func runScript(work *WorkRequest) (string, error) {
|
||||||
cmd := exec.Command(binary, work.Payload)
|
cmd := exec.Command(binary, work.Payload)
|
||||||
// with env variables...
|
// with env variables...
|
||||||
cmd.Env = append(os.Environ(), work.Args...)
|
cmd.Env = append(os.Environ(), work.Args...)
|
||||||
|
// using a process group...
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
|
|
||||||
// Open the out file for writing
|
// Open the out file for writing
|
||||||
logFilename := path.Join(workingdir, fmt.Sprintf("%s_%s.txt", tools.ToSnakeCase(work.Name), time.Now().Format("20060102_1504")))
|
logFilename := path.Join(workingdir, fmt.Sprintf("%s_%s.txt", tools.ToSnakeCase(work.Name), time.Now().Format("20060102_1504")))
|
||||||
|
@ -90,7 +93,7 @@ func runScript(work *WorkRequest) (string, error) {
|
||||||
|
|
||||||
timer := time.AfterFunc(time.Duration(work.Timeout)*time.Second, func() {
|
timer := time.AfterFunc(time.Duration(work.Timeout)*time.Second, func() {
|
||||||
logger.Warning.Printf("Timeout reached (%ds). Killing script: %s\n", work.Timeout, work.Script)
|
logger.Warning.Printf("Timeout reached (%ds). Killing script: %s\n", work.Timeout, work.Script)
|
||||||
cmd.Process.Kill()
|
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||||
})
|
})
|
||||||
err = cmd.Wait()
|
err = cmd.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user