Skip to content

Commit

Permalink
Add --print-delta-and-exit, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Jul 10, 2020
1 parent 76804ab commit b8b99c9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Usage of wait-for-cron-expression-match:
Print dots to stdout while waiting
-format string
Timestamp format (default "2006-01-02T15:04:05Z07:00")
-print-delta-and-exit
Only print the duration (in seconds) until the next expression match and exit (without waiting)
-print-next-match-and-exit
Only print the timestamp of the next expression match and exit (without waiting)
-q (alias for -quiet)
Expand Down
57 changes: 38 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ var config struct {
Quiet bool
PrintDots bool
PrintNextTimestampAndExit bool
PrintDeltaAndExit bool
TimestampFormat string
CronExpressions []string

cronExpressions []string
schedules []cron.Schedule
evaluationInterval time.Duration
}
Expand All @@ -27,6 +28,7 @@ func init() {
log.SetFlags(log.Ltime | log.Ldate | log.Lmicroseconds)
log.SetPrefix(fmt.Sprintf("[%s] ", app))
config.TimestampFormat = time.RFC3339
flag.BoolVar(&config.PrintDeltaAndExit, "print-delta-and-exit", config.PrintDeltaAndExit, "Only print the duration (in seconds) until the next expression match and exit (without waiting)")
flag.BoolVar(&config.PrintNextTimestampAndExit, "print-next-match-and-exit", config.PrintNextTimestampAndExit, "Only print the timestamp of the next expression match and exit (without waiting)")
flag.StringVar(&config.TimestampFormat, "format", config.TimestampFormat, "Timestamp format")
flag.BoolVar(&config.Quiet, "quiet", config.Quiet, "Suppress all output")
Expand All @@ -35,13 +37,13 @@ func init() {
flag.Parse()

config.evaluationInterval = 1 * time.Second
config.cronExpressions = flag.Args()
config.CronExpressions = flag.Args()

if len(config.cronExpressions) == 0 {
if len(config.CronExpressions) == 0 {
log.Fatal("at least one expression is required")
}
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)
for _, e := range config.cronExpressions {
for _, e := range config.CronExpressions {
schedule, err := parser.Parse(e)
if err != nil {
log.Fatalf("parse cron expression %q: %v", e, err)
Expand All @@ -57,39 +59,56 @@ func init() {

func main() {
now := time.Now()
tickInterval := config.evaluationInterval
var next time.Time
for i, s := range config.schedules {
nextNew := s.Next(now)
if i == 0 || nextNew.Before(next) {
next = nextNew
}
next := nextMatch(now)
if config.PrintDeltaAndExit {
fmt.Println(time.Until(next).Seconds())
return
}
if config.PrintNextTimestampAndExit {
fmt.Println(next.Format(config.TimestampFormat))
return
}
delta := next.Sub(now)
waitUntil(next)
}

func waitUntil(next time.Time) {
defer func() {
if config.PrintDots {
fmt.Println()
}
log.Print("done")
}()

tickInterval := config.evaluationInterval
delta := time.Until(next)
if delta < tickInterval {
tickInterval = delta
}
tick := time.Tick(tickInterval)

plural := ""
if len(config.cronExpressions) > 1 {
if len(config.CronExpressions) > 1 {
plural = "s"
}
log.Printf("waiting %v until next match (%v) of cron expression%s %q", delta, next.Format(time.RFC3339Nano), plural, config.cronExpressions)
defer log.Print("done")
for now = range tick {
log.Printf("waiting %v until next match (%v) of cron expression%s %q", time.Until(next), next.Format(config.TimestampFormat), plural, config.CronExpressions)

for now := range tick {
if config.PrintDots {
fmt.Print(".")
}
if now.After(next) {
if config.PrintDots {
fmt.Println()
}
return
}
}
}

func nextMatch(now time.Time) time.Time {
var next time.Time
for i, s := range config.schedules {
nextNew := s.Next(now)
if i == 0 || nextNew.Before(next) {
next = nextNew
}
}
return next
}

0 comments on commit b8b99c9

Please sign in to comment.