-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
80 lines (65 loc) · 1.56 KB
/
job.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bytes"
"io"
"os"
"os/exec"
"strings"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/sony/sonyflake"
)
var flake = sonyflake.NewSonyflake(sonyflake.Settings{})
// Job Struct
type Job struct {
cmd []string
timestamp int64
uuid uint64
}
func newUUID() uint64 {
id, err := flake.NextID()
if err != nil {
log.Fatalf("Error generating NextID(), %+v", err)
}
return id
}
func (j *Job) parse(c []string) {
j.cmd = c
j.timestamp = time.Now().UnixNano()
j.uuid = newUUID()
}
func timeSince(start time.Time) time.Duration {
return time.Since(start)
}
func (j *Job) exec() (int, time.Duration) {
start := time.Now()
cmd := exec.Command(j.cmd[0], j.cmd[1:]...)
var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = io.MultiWriter(os.Stdout, &stdoutBuf)
cmd.Stderr = io.MultiWriter(os.Stderr, &stderrBuf)
if err := cmd.Start(); err != nil {
log.Errorf("%+v", err)
return 127, timeSince(start)
}
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
log.Errorf(" %s: %d", err, exiterr.ExitCode())
log.Printf("\a")
return exiterr.ExitCode(), timeSince(start)
}
}
outStr, errStr := stdoutBuf.String(), stderrBuf.String()
if len(outStr) > 0 {
for _, line := range strings.Split(strings.TrimSuffix(outStr, "\n"), "\n") {
log.Info(line)
}
}
if len(errStr) > 0 {
for _, line := range strings.Split(strings.TrimSuffix(errStr, "\n"), "\n") {
log.Warn(line)
}
}
exitCode := cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
return exitCode, timeSince(start)
}