forked from aptible/supercronic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
174 lines (144 loc) · 4.04 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/aptible/supercronic/cron"
"github.com/aptible/supercronic/crontab"
"github.com/aptible/supercronic/log/hook"
"github.com/aptible/supercronic/prometheus_metrics"
"github.com/evalphobia/logrus_sentry"
"github.com/sirupsen/logrus"
)
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] CRONTAB\n\nAvailable options:\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
debug := flag.Bool("debug", false, "enable debug logging")
json := flag.Bool("json", false, "enable JSON logging")
test := flag.Bool("test", false, "test crontab (does not run jobs)")
prometheusListen := flag.String("prometheus-listen-address", "", "give a valid ip:port address to expose Prometheus metrics at /metrics")
splitLogs := flag.Bool("split-logs", false, "split log output into stdout/stderr")
sentry := flag.String("sentry-dsn", "", "enable Sentry error logging, using provided DSN")
sentryAlias := flag.String("sentryDsn", "", "alias for sentry-dsn")
overlapping := flag.Bool("overlapping", false, "enable tasks overlapping")
delay := flag.Int("delay", 0, "delay in seconds after executing @start")
flag.Parse()
var sentryDsn string
if *sentryAlias != "" {
sentryDsn = *sentryAlias
}
if *sentry != "" {
sentryDsn = *sentry
}
if *debug {
logrus.SetLevel(logrus.DebugLevel)
}
if *json {
logrus.SetFormatter(&logrus.JSONFormatter{})
} else {
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
}
if *splitLogs {
hook.RegisterSplitLogger(
logrus.StandardLogger(),
os.Stdout,
os.Stderr,
)
}
if flag.NArg() != 1 {
Usage()
os.Exit(2)
return
}
crontabFileName := flag.Args()[0]
var sentryHook *logrus_sentry.SentryHook
if sentryDsn != "" {
sentryLevels := []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
}
sh, err := logrus_sentry.NewSentryHook(sentryDsn, sentryLevels)
if err != nil {
logrus.Fatalf("Could not init sentry logger: %s", err)
} else {
sh.Timeout = 5 * time.Second
sentryHook = sh
}
if sentryHook != nil {
logrus.StandardLogger().AddHook(sentryHook)
}
}
promMetrics := prometheus_metrics.NewPrometheusMetrics()
if *prometheusListen != "" {
promServerShutdownClosure, err := prometheus_metrics.InitHTTPServer(*prometheusListen, context.Background())
if err != nil {
logrus.Fatalf("prometheus http startup failed: %s", err.Error())
}
defer func() {
if err := promServerShutdownClosure(); err != nil {
logrus.Fatalf("prometheus http shutdown failed: %s", err.Error())
}
}()
}
for true {
promMetrics.Reset()
logrus.Infof("read crontab: %s", crontabFileName)
tab, err := readCrontabAtPath(crontabFileName)
if err != nil {
logrus.Fatal(err)
break
}
if *test {
logrus.Info("crontab is valid")
os.Exit(0)
break
}
var wg sync.WaitGroup
exitCtx, notifyExit := context.WithCancel(context.Background())
for _, job := range tab.Jobs {
cronLogger := logrus.WithFields(logrus.Fields{
"job.schedule": job.Schedule,
"job.command": job.Command,
"job.position": job.Position,
})
if job.Position != 0 && job.Schedule == crontab.Start {
logrus.Fatalf(crontab.StartErr)
}
cron.StartJob(&wg, tab.Context, job, exitCtx, cronLogger, *overlapping, &promMetrics)
if job.Schedule == crontab.Start {
time.Sleep(time.Duration(*delay) * time.Second)
}
}
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR2)
termSig := <-termChan
if termSig == syscall.SIGUSR2 {
logrus.Infof("received %s, reloading crontab", termSig)
} else {
logrus.Infof("received %s, shutting down", termSig)
}
notifyExit()
logrus.Info("waiting for jobs to finish")
wg.Wait()
if termSig != syscall.SIGUSR2 {
logrus.Info("exiting")
break
}
}
}
func readCrontabAtPath(path string) (*crontab.Crontab, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return crontab.ParseCrontab(file)
}