-
Notifications
You must be signed in to change notification settings - Fork 1
/
acron.go
133 lines (110 loc) · 3.31 KB
/
acron.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"cron/cron"
"cron/utils"
"flag"
"fmt"
"github.com/getsentry/raven-go"
"github.com/joho/godotenv"
"github.com/logrusorgru/aurora"
"log"
"os"
"time"
)
func main() {
var config Config
c := cron.New()
if err := godotenv.Load(); err != nil {
log.Printf("Error loading .env file: %s (using system ENV)", err)
}
if err := raven.SetDSN(os.Getenv("SENTRY_DSN")); err != nil {
log.Printf("Sentry error: %s", err)
}
var configName = flag.String("config", "config", "filename without ext")
var configPath = flag.String("path", ".", "path to config")
flag.Parse()
config.init(*configPath, *configName)
config.addOnConfigChangeHandler(func(before *Config, after *Config) {
log.Printf("Config updated. Reseting jobs.")
runCron(c, &config)
})
runCron(c, &config)
forever := make(chan bool)
utils.PrintStdOut("Cron loaded and running. To exit press CTRL+C")
<-forever
}
func runCron(c *cron.Cron, config *Config) {
c.Stop()
c.CleanAll()
if config.Timezone != nil {
if tz, err := time.LoadLocation(*config.Timezone); err != nil {
raven.CaptureErrorAndWait(err, nil)
log.Printf("Cant find timezone: %s", err)
} else {
c.SetLocation(tz)
}
} else {
c.SetLocation(nil)
}
for _, e := range config.Tasks {
task := e
if task.Time == "@reboot" {
if err := c.AddOnceFunc("* * * * * *", taskFunction(config, &task), task.Name); err != nil {
raven.CaptureErrorAndWait(err, nil)
log.Printf("Error adding job '%s' to cron: %s", task.Name, err)
}
continue
}
if task.Time[:5] == "@once" {
if err := c.AddOnceFunc(task.Time[6:], taskFunction(config, &task), task.Name); err != nil {
raven.CaptureErrorAndWait(err, nil)
log.Printf("Error adding job '%s' to cron: %s", task.Name, err)
}
continue
}
if err := c.AddFunc(task.Time, taskFunction(config, &task), task.Name); err != nil {
raven.CaptureErrorAndWait(err, nil)
log.Printf("Error adding job '%s' to cron: %s", task.Name, err)
}
}
c.Start()
}
func taskFunction(config *Config, task *ConfigTask) func() {
return func() {
logTime := time.Now().Local().Format("2006-01-02 15:04:05")
// Choose user
user := config.User
if task.Shell != nil {
user = task.User
}
// Choose shell
shell := config.Shell
if task.Shell != nil {
shell = task.Shell
}
timeBefore := time.Now()
out, jobErr := utils.RunCMD(task.Command, user, shell)
elapsedTime := time.Now().Sub(timeBefore)
if jobErr != nil {
raven.CaptureErrorAndWait(jobErr, map[string]string{"command": task.Command, "time": task.Time})
utils.PrintStdOut("Job '%s' finished in %fs - %s: %s", task.Name, elapsedTime.Seconds(), aurora.Red("Failed"), jobErr)
} else {
utils.PrintStdOut("Job '%s' finished in %fs - %s", task.Name, elapsedTime.Seconds(), aurora.Green("Success"))
}
if task.Output != nil {
f, err := os.OpenFile(*task.Output, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
panic(err)
}
if _, err = f.WriteString(fmt.Sprintf("%s %s", logTime, string(out[:]))); err != nil {
raven.CaptureErrorAndWait(err, nil)
log.Printf("Cant write log of '%s' to file: %s", task.Name, err)
}
if err := f.Close(); err != nil {
raven.CaptureErrorAndWait(err, nil)
log.Printf("Cant close log file of '%s': %s", task.Name, err)
}
}
}
}