-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexecution.go
51 lines (47 loc) · 1.28 KB
/
execution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"os"
"os/exec"
"log"
"io"
"time"
"syscall"
)
type JobExecutor func(jobName, jobPath string, timeout time.Duration)
func JobExecutorFromScript(jobExecutorScript string, output io.Writer) JobExecutor {
if len(jobExecutorScript) == 0 {
log.Fatal("Job executor is not defined")
}
jobExecutorScript = os.ExpandEnv(jobExecutorScript)
return func(jobName, jobPath string, timeout time.Duration) {
cmd := exec.Command(jobExecutorScript, jobName, jobPath)
log.Printf("Running comand line: %s '%s' '%s' Timeout: %s", jobExecutorScript, jobName, jobPath, timeout)
cmd.Stdout = output
cmd.Stderr = output
err := runWithTimout(cmd, timeout)
if err != nil {
log.Printf("ERROR: Failed to execute executor script %s %s %s", jobExecutorScript, jobName, jobPath)
}
}
}
func runWithTimout(cmd *exec.Cmd, timeout time.Duration) error {
if timeout.Seconds() <= 0 {
return cmd.Run()
} else {
done := make(chan error, 1)
go func() {
cmd.Start()
done <- cmd.Wait()
}()
select {
case <-time.After(timeout):
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
log.Printf("ERROR: Failed to terminate job %s error: %s", cmd.Path, err)
}
log.Printf("Job %s timed out", cmd.Path)
return nil
case err := <-done:
return err
}
}
}