-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrunner.go
48 lines (38 loc) · 791 Bytes
/
runner.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
package main
import (
"github.com/robfig/cron"
"log"
"os/exec"
)
type Runner struct {
cron *cron.Cron
}
func NewRunner() *Runner {
r := &Runner{
cron: cron.New(),
}
return r
}
func (r *Runner) Add(spec string, cmd string) error {
err := r.cron.AddFunc(spec, r.cmdFunc(cmd))
log.Printf("Add cron job spec:%v cmd:%v err:%v", spec, cmd, err)
return err
}
func (r *Runner) Len() int {
return len(r.cron.Entries())
}
func (r *Runner) Start() {
log.Println("Start runner")
r.cron.Start()
}
func (r *Runner) Stop() {
r.cron.Stop()
log.Println("Stop runner")
}
func (r *Runner) cmdFunc(cmd string) func() {
cmdFunc := func() {
out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
log.Printf("cmd:%v out:%v err:%v", cmd, string(out), err)
}
return cmdFunc
}