forked from aptible/supercronic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (68 loc) · 1.68 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
package main
import (
"context"
"flag"
"fmt"
"github.com/aptible/supercronic/cron"
"github.com/aptible/supercronic/crontab"
"github.com/sirupsen/logrus"
"os"
"os/signal"
"sync"
"syscall"
)
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")
flag.Parse()
if *debug {
logrus.SetLevel(logrus.DebugLevel)
}
if *json {
logrus.SetFormatter(&logrus.JSONFormatter{})
} else {
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
}
if flag.NArg() != 1 {
Usage()
os.Exit(2)
return
}
crontabFileName := flag.Args()[0]
logrus.Infof("read crontab: %s", crontabFileName)
tab, err := readCrontabAtPath(crontabFileName)
if err != nil {
logrus.Fatal(err)
return
}
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,
})
cron.StartJob(&wg, tab.Context, job, exitCtx, cronLogger)
}
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
termSig := <-termChan
logrus.Infof("received %s, shutting down", termSig)
notifyExit()
logrus.Info("waiting for jobs to finish")
wg.Wait()
logrus.Info("exiting")
}
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)
}