-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
105 lines (79 loc) · 1.78 KB
/
cron.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
97
98
99
100
101
102
103
104
105
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"github.com/robfig/cron"
yaml "gopkg.in/yaml.v2"
)
type command struct {
name string
args []string
}
// CronAction parses and schedules jobs, waiting for SIGINT signal to stop
func CronAction(configFilepath string, verbose bool) error {
// get config file content
fileContent, err := ioutil.ReadFile(configFilepath)
if err != nil {
return err
}
// parse config file
config := ConfigFile{}
err = yaml.Unmarshal(fileContent, &config)
if err != nil {
return err
}
// prepare cron
c := cron.New()
// creating jobs
for i := range config.Jobs {
job := config.Jobs[i]
c.AddFunc(job.Cron, func() {
if verbose {
log.Println(job)
}
cmdArgs := newCommand(job.Command)
cmd := exec.Command(cmdArgs.name, cmdArgs.args...)
if job.WorkingDir != "" {
cmd.Dir = job.WorkingDir
}
// if job.User != 0 && job.Group != 0 {
// fmt.Println(job.User, ":", job.Group)
// cmd.SysProcAttr = &syscall.SysProcAttr{}
// cmd.SysProcAttr.Credential = &syscall.Credential{Uid: job.User, Gid: job.Group}
// }
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Printf("[ERROR] cmd: %v: %v\n", job.Command, err)
}
})
}
log.Printf("%v job(s) scheduled\n", len(c.Entries()))
// start cron
c.Start()
// prepare chan for catching signals
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, os.Kill)
// waiting for signal to stop
<-done
c.Stop()
return nil
}
func newCommand(rawCommand []string) command {
var (
name string
args []string
)
if len(rawCommand) > 1 {
name = rawCommand[0]
args = rawCommand[1:]
} else {
name = rawCommand[0]
args = []string{}
}
return command{name: name, args: args}
}