Skip to content

Commit

Permalink
Warn on cron schedule that will never run
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Feb 9, 2024
1 parent 58d417d commit 9b05183
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
11 changes: 8 additions & 3 deletions cmd/backup/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func loadEnvVars() (*Config, error) {
return loadConfig(os.LookupEnv)
}

func loadEnvFiles(directory string) ([]*Config, error) {
type configFile struct {
name string
config *Config
}

func loadEnvFiles(directory string) ([]configFile, error) {
items, err := os.ReadDir(directory)
if err != nil {
if os.IsNotExist(err) {
Expand All @@ -57,7 +62,7 @@ func loadEnvFiles(directory string) ([]*Config, error) {
return nil, fmt.Errorf("loadEnvFiles: failed to read files from env directory: %w", err)
}

var cs = make([]*Config, 0)
cs := []configFile{}
for _, item := range items {
if item.IsDir() {
continue
Expand All @@ -75,7 +80,7 @@ func loadEnvFiles(directory string) ([]*Config, error) {
if err != nil {
return nil, fmt.Errorf("loadEnvFiles: error loading config from file %s: %w", p, err)
}
cs = append(cs, c)
cs = append(cs, configFile{config: c, name: item.Name()})
}

return cs, nil
Expand Down
26 changes: 26 additions & 0 deletions cmd/backup/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"time"

"github.com/robfig/cron/v3"
)

// checkCronSchedule detects whether the given cron expression will actually
// ever be executed or not.
func checkCronSchedule(expression string) (ok bool) {
defer func() {
if err := recover(); err != nil {
ok = false
}
}()
sched, err := cron.ParseStandard(expression)
if err != nil {
ok = false
return
}
now := time.Now()
sched.Next(now) // panics when the cron would never run
ok = true
return
}
17 changes: 12 additions & 5 deletions cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ func (c *command) runInForeground() error {
),
)

addJob := func(config *Config) error {
addJob := func(config *Config, name string) error {
if _, err := cr.AddFunc(config.BackupCronExpression, func() {
c.logger.Info(
fmt.Sprintf(
"Now running schedule %s",
"Now running script on schedule %s",
config.BackupCronExpression,
),
)
Expand All @@ -153,7 +153,14 @@ func (c *command) runInForeground() error {
}); err != nil {
return fmt.Errorf("addJob: error adding schedule %s: %w", config.BackupCronExpression, err)
}
c.logger.Info(fmt.Sprintf("Successfully scheduled backup with expression %s", config.BackupCronExpression))

c.logger.Info(fmt.Sprintf("Successfully scheduled backup %s with expression %s", name, config.BackupCronExpression))
if ok := checkCronSchedule(config.BackupCronExpression); !ok {
c.logger.Warn(
fmt.Sprintf("Scheduled cron expression %s will never run, is this intentional?", config.BackupCronExpression),
)
}

return nil
}

Expand All @@ -167,15 +174,15 @@ func (c *command) runInForeground() error {
if err != nil {
return fmt.Errorf("runInForeground: could not load config from environment variables: %w", err)
} else {
err = addJob(c)
err = addJob(c, "from environment")
if err != nil {
return fmt.Errorf("runInForeground: error adding job from env: %w", err)
}
}
} else {
c.logger.Info("/etc/dockervolumebackup/conf.d was found, using configuration files from this directory.")
for _, config := range cs {
err = addJob(config)
err = addJob(config.config, config.name)
if err != nil {
return fmt.Errorf("runInForeground: error adding jobs from conf files: %w", err)
}
Expand Down

0 comments on commit 9b05183

Please sign in to comment.