mirror of
https://github.com/aptible/supercronic.git
synced 2025-12-24 12:12:27 +08:00
Add flag handling (debug, json logging)
This commit is contained in:
0
integration/noop.crontab
Normal file
0
integration/noop.crontab
Normal file
@@ -1,7 +1,8 @@
|
||||
function run_supercronic() {
|
||||
local crontab="$1"
|
||||
local timeout="${2:-2s}"
|
||||
timeout --preserve-status --kill-after "30s" "$timeout" "${BATS_TEST_DIRNAME}/../supercronic" "$crontab" 2>&1
|
||||
timeout --preserve-status --kill-after "30s" "$timeout" \
|
||||
"${BATS_TEST_DIRNAME}/../supercronic" ${SUPERCRONIC_ARGS:-} "$crontab" 2>&1
|
||||
}
|
||||
|
||||
@test "it runs a cron job" {
|
||||
@@ -23,3 +24,11 @@ function run_supercronic() {
|
||||
@test "it warns when a job is falling behind" {
|
||||
run_supercronic "${BATS_TEST_DIRNAME}/timeout.crontab" 5s | grep -iE "job took too long to run"
|
||||
}
|
||||
|
||||
@test "it supports debug logging " {
|
||||
SUPERCRONIC_ARGS="-debug" run_supercronic "${BATS_TEST_DIRNAME}/hello.crontab" | grep -iE "debug"
|
||||
}
|
||||
|
||||
@test "it supports JSON logging " {
|
||||
SUPERCRONIC_ARGS="-json" run_supercronic "${BATS_TEST_DIRNAME}/noop.crontab" | grep -iE "^{"
|
||||
}
|
||||
|
||||
31
main.go
31
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/aptible/supercronic/cron"
|
||||
"github.com/aptible/supercronic/crontab"
|
||||
@@ -11,19 +12,33 @@ import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// TODO: debug flag instead
|
||||
// TODO: JSON logging?
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
|
||||
var Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] CRONTAB\n\nAvailable options:\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s CRONTAB\n", os.Args[0])
|
||||
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 := os.Args[1]
|
||||
crontabFileName := flag.Args()[0]
|
||||
logrus.Infof("read crontab: %s", crontabFileName)
|
||||
|
||||
file, err := os.Open(crontabFileName)
|
||||
|
||||
Reference in New Issue
Block a user