-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
250 lines (221 loc) · 10.1 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"context"
"fmt"
"io"
"math"
"os"
"os/signal"
"runtime"
"syscall"
"time"
log "github.com/go-pkgz/lgr"
ntf "github.com/go-pkgz/notify"
"github.com/go-pkgz/repeater"
"github.com/go-pkgz/repeater/strategy"
"github.com/robfig/cron/v3"
"github.com/umputun/go-flags"
"gopkg.in/natefinch/lumberjack.v2"
"github.com/umputun/cronn/app/crontab"
"github.com/umputun/cronn/app/notify"
"github.com/umputun/cronn/app/resumer"
"github.com/umputun/cronn/app/service"
)
var opts struct {
CrontabFile string `short:"f" long:"file" env:"CRONN_FILE" default:"crontab" description:"crontab file"`
Command string `short:"c" long:"command" env:"CRONN_COMMAND" description:"crontab single command"`
Resume string `short:"r" long:"resume" env:"CRONN_RESUME" description:"auto-resume location"`
ResumeConcur int `long:"resume-concur" env:"CRONN_RESUME_CONCUR" default:"1" description:"auto-resume concurrency level"`
UpdateEnable bool `short:"u" long:"update" env:"CRONN_UPDATE" description:"auto-update mode"`
UpdateInterval time.Duration `long:"update-interval" env:"CRONN_UPDATE_INTERVAL" default:"10s" description:"auto-update interval"`
JitterEnable bool `short:"j" long:"jitter" env:"CRONN_JITTER" description:"enable jitter"`
JitterDuration time.Duration `long:"jitter-duration" env:"CRONN_JITTER_DURATION" default:"10s" description:"jitter duration"`
DeDup bool `long:"dedup" env:"CRONN_DEDUP" description:"prevent duplicated jobs"`
Repeater struct {
Attempts int `long:"attempts" env:"ATTEMPTS" default:"1" description:"how many time repeat failed job"`
Duration time.Duration `long:"duration" env:"DURATION" default:"1s" description:"initial duration"`
Factor float64 `long:"factor" env:"FACTOR" default:"3" description:"backoff factor"`
Jitter bool `long:"jitter" env:"JITTER" description:"jitter"`
} `group:"repeater" namespace:"repeater" env-namespace:"CRONN_REPEATER"`
Notify struct {
EnabledError bool `long:"enabled-error" env:"ENABLED_ERROR" description:"enable email notifications on errors"`
EnabledCompletion bool `long:"enabled-complete" env:"ENABLED_COMPLETE" description:"enable completion notifications"`
ErrorTemplate string `long:"err-template" env:"ERR_TEMPLATE" description:"error template file"`
CompletionTemplate string `long:"complete-template" env:"COMPLET_TEMPLATE" description:"completion template file"`
SMTPHost string `long:"smtp-host" env:"SMTP_HOST" description:"SMTP host"`
SMTPPort int `long:"smtp-port" env:"SMTP_PORT" description:"SMTP port"`
SMTPUsername string `long:"smtp-username" env:"SMTP_USERNAME" description:"SMTP user name"`
SMTPPassword string `long:"smtp-password" env:"SMTP_PASSWORD" description:"SMTP password"`
SMTPTLS bool `long:"smtp-tls" env:"SMTP_TLS" description:"enable SMTP TLS"`
SMTPStartTLS bool `long:"smtp-starttls" env:"SMTP_STARTTLS" description:"enable SMTP StartTLS"`
SMTPTimeOut time.Duration `long:"smtp-timeout" env:"SMTP_TIMEOUT" default:"10s" description:"SMTP TCP connection timeout"`
FromEmail string `long:"from" env:"FROM" description:"SMTP from email"`
ToEmails []string `long:"to" env:"TO" description:"SMTP to email(s)" env-delim:","`
MaxLogLines int `long:"max-log" env:"MAX_LOG" default:"100" description:"max number of log lines name"`
HostName string `long:"host" env:"HOSTNAME" description:"host name running cronn"`
SlackToken string `long:"slack-token" env:"SLACK_TOKEN" description:"API token for the Slack bot"`
SlackChannels []string `long:"slack-channels" env:"SLACK_CHANNELS" description:"List of Slack channels the bot will post messages to" env-delim:","`
TelegramToken string `long:"telegram-token" env:"TELEGRAM_TOKEN" description:"API token for the Telegram bot"`
TelegramDestinations []string `long:"telegram-destinations" env:"TELEGRAM_DESTINATIONS" description:"List of Telegram chat IDs the bot will post messages to" env-delim:","`
WebhookURLs []string `long:"webhook-urls" env:"WEBHOOK_URLS" description:"List of webhook URLs the bot will post messages to" env-delim:","`
TimeOut time.Duration `long:"timeout" env:"TIMEOUT" default:"10s" description:"timeout for notification"`
} `group:"notify" namespace:"notify" env-namespace:"CRONN_NOTIFY"`
Log struct {
Enabled bool `long:"enabled" env:"ENABLED" description:"enable logging"`
Debug bool `long:"debug" env:"DEBUG" description:"debug mode"`
EnablePrefix bool `long:"prefix" env:"PREFIX" description:"enable log prefix with current command"`
Filename string `long:"filename" env:"FILENAME" description:"file to write logs to. Log to stdout if not specified"`
MaxSize int `long:"max-size" env:"MAX_SIZE" default:"100" description:"maximum size in megabytes of the log file before it gets rotated"`
MaxAge int `long:"max-age" env:"MAX_AGE" default:"0" description:"maximum number of days to retain old log files"`
MaxBackups int `long:"max-backups" env:"MAX_BACKUPS" default:"7" description:"maximum number of old log files to retain"`
EnabledCompress bool `long:"enabled-compress" env:"ENABLED_COMPRESS" description:"determines if the rotated log files should be compressed using gzip"`
} `group:"log" namespace:"log" env-namespace:"CRONN_LOG"`
}
var revision = "unknown"
func main() {
fmt.Printf("cronn %s\n", revision)
p := flags.NewParser(&opts, flags.PassDoubleDash|flags.HelpFlag)
if _, err := p.Parse(); err != nil {
if err.(*flags.Error).Type != flags.ErrHelp {
fmt.Printf("%v\n", err)
os.Exit(1)
}
p.WriteHelp(os.Stderr)
os.Exit(2)
}
stdout := setupLogs()
defer func() {
if x := recover(); x != nil {
log.Printf("[WARN] run time panic:\n%v", x)
panic(x)
}
}()
ctx, cancel := context.WithCancel(context.Background())
hupCh := signals(cancel) // handle SIGQUIT and SIGTERM
updateInterval := time.Duration(math.MaxInt64) // very long interval, effectively disabling automatic refresh
if opts.UpdateEnable && opts.UpdateInterval > 0 {
updateInterval = opts.UpdateInterval
} else {
log.Printf("[INFO] auto-update disabled")
}
var crontabParser service.CrontabParser
if opts.Command != "" {
crontabParser = crontab.NewSingle(opts.Command)
} else {
crontabParser = crontab.New(opts.CrontabFile, updateInterval, hupCh)
}
rptr := repeater.New(&strategy.Backoff{Repeats: opts.Repeater.Attempts, Duration: opts.Repeater.Duration,
Factor: opts.Repeater.Factor, Jitter: opts.Repeater.Jitter})
var jitterDuration time.Duration
if opts.JitterEnable {
jitterDuration = opts.JitterDuration
}
cronService := service.Scheduler{
Cron: cron.New(),
Resumer: resumer.New(opts.Resume, opts.Resume != ""),
ResumeConcurrency: opts.ResumeConcur,
CrontabParser: crontabParser,
UpdatesEnabled: opts.UpdateEnable,
Jitter: jitterDuration,
Repeater: rptr,
Notifier: makeNotifier(),
HostName: makeHostName(),
MaxLogLines: opts.Notify.MaxLogLines,
EnableLogPrefix: opts.Log.EnablePrefix,
Stdout: stdout,
DeDup: service.NewDeDup(opts.DeDup),
NotifyTimeout: opts.Notify.TimeOut,
}
cronService.Do(ctx)
}
func makeNotifier() *notify.Service {
if !opts.Notify.EnabledError && !opts.Notify.EnabledCompletion {
return nil
}
if opts.Notify.FromEmail == "" {
opts.Notify.FromEmail = "cronn@" + makeHostName()
}
return notify.NewService(
notify.Params{
EnabledError: opts.Notify.EnabledError,
EnabledCompletion: opts.Notify.EnabledCompletion,
ErrorTemplate: opts.Notify.ErrorTemplate,
CompletionTemplate: opts.Notify.CompletionTemplate,
},
notify.SendersParams{
SMTPParams: ntf.SMTPParams{
Host: opts.Notify.SMTPHost,
Port: opts.Notify.SMTPPort,
TLS: opts.Notify.SMTPTLS,
StartTLS: opts.Notify.SMTPStartTLS,
Username: opts.Notify.SMTPUsername,
Password: opts.Notify.SMTPPassword,
TimeOut: opts.Notify.SMTPTimeOut,
ContentType: "text/html",
Charset: "utf-8",
},
FromEmail: opts.Notify.FromEmail,
ToEmails: opts.Notify.ToEmails,
SlackToken: opts.Notify.SlackToken,
SlackChannels: opts.Notify.SlackChannels,
TelegramToken: opts.Notify.TelegramToken,
TelegramDestinations: opts.Notify.TelegramDestinations,
WebhookURLs: opts.Notify.WebhookURLs,
})
}
func makeHostName() string {
if opts.Notify.HostName != "" {
return opts.Notify.HostName
}
host, err := os.Hostname()
if err != nil {
return "unknown"
}
return host
}
func setupLogs() io.Writer {
if !opts.Log.Enabled {
log.Setup(log.Out(io.Discard), log.Err(io.Discard))
return os.Stdout
}
log.Setup(log.Msec)
if opts.Log.Debug {
log.Setup(log.Debug, log.CallerFunc, log.CallerPkg, log.CallerFile)
}
if opts.Log.Filename != "" {
fileLogger := &lumberjack.Logger{
Filename: opts.Log.Filename,
MaxSize: opts.Log.MaxSize,
MaxBackups: opts.Log.MaxBackups,
MaxAge: opts.Log.MaxAge,
Compress: opts.Log.EnabledCompress,
LocalTime: true, // as log files have content in local time format
}
log.Setup(
log.Out(fileLogger),
log.Err(fileLogger),
)
return fileLogger
}
return os.Stdout
}
func signals(cancel context.CancelFunc) (hupCh chan struct{}) {
sigChan := make(chan os.Signal, 1)
hupCh = make(chan struct{})
go func() {
stacktrace := make([]byte, 8192)
for sig := range sigChan {
switch sig {
case syscall.SIGQUIT: // catch SIGQUIT and print stack traces
length := runtime.Stack(stacktrace, true)
fmt.Println(string(stacktrace[:length]))
case syscall.SIGHUP: // reload crontab file on SIGHUP
hupCh <- struct{}{}
case syscall.SIGTERM: // terminate on SIGTERM
cancel()
}
}
}()
signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGHUP)
return hupCh
}