-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
96 lines (86 loc) · 1.82 KB
/
runtime.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package jobsrunner
import (
"context"
"fmt"
"io/ioutil"
"log"
"os/exec"
"sync"
"time"
)
// Runtime of jobrunner.
type Runtime struct {
// TODO: Logger dependency.
Conf Config
//Logger log.Logger
ctx context.Context
}
// NewFromFile creates Runtime with Config parsed from the specified file.
func NewFromFile(filename string) (Runtime, error) {
conf, err := NewConfigFromFile(filename)
if err != nil {
return Runtime{}, fmt.Errorf("can't create config: %s", err)
}
return Runtime{
Conf: conf,
}, nil
}
// Run the configured jobs.
func (r *Runtime) Run(ctx context.Context) {
var wg sync.WaitGroup
r.ctx = ctx
for i, job := range r.Conf.Jobs {
wg.Add(1)
// TODO: Write a test that all jobs are started.
go func(job ConfigJob) {
r.runWithInterval(job)
log.Printf("Job #%d finished", i)
wg.Done()
}(job)
}
wg.Wait()
}
func (r *Runtime) runWithInterval(job ConfigJob) {
r.runCmd(job.Cmd)
for {
select {
case <-r.ctx.Done():
return
case <-time.Tick(time.Duration(job.Interval)):
r.runCmd(job.Cmd)
}
}
}
func (r *Runtime) runCmd(cmd string) {
c := exec.Command("sh", "-c", cmd)
stderr, err := c.StderrPipe()
if err != nil {
log.Printf(`"%v" didn'r run: failed to pipe stderr: %s`, cmd, err)
return
}
stdout, err := c.StdoutPipe()
if err != nil {
log.Printf(`"%v" didn'r run: failed to pipe stdout: %s`, cmd, err)
return
}
if err := c.Start(); err != nil {
log.Printf(`"%v" didn'r run: failed to start: %s`, cmd, err)
return
}
b := time.Now()
var (
slurp, _ = ioutil.ReadAll(stderr)
output, _ = ioutil.ReadAll(stdout)
)
err = c.Wait()
log.Printf(`"%v" took %s`, cmd, time.Now().Sub(b))
if err != nil {
log.Printf(`finished with %s`, err)
}
if len(slurp) != 0 {
log.Printf("STDERR: \n%s", slurp)
}
if len(output) != 0 {
log.Printf("STDOUT: \n%s", output)
}
}