diff --git a/cmd/backup/config_provider.go b/cmd/backup/config_provider.go index 9f1441f7..50a588d4 100644 --- a/cmd/backup/config_provider.go +++ b/cmd/backup/config_provider.go @@ -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) { @@ -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 @@ -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 diff --git a/cmd/backup/cron.go b/cmd/backup/cron.go new file mode 100644 index 00000000..d087ea96 --- /dev/null +++ b/cmd/backup/cron.go @@ -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 +} diff --git a/cmd/backup/main.go b/cmd/backup/main.go index 3ee3fb48..34211ee5 100644 --- a/cmd/backup/main.go +++ b/cmd/backup/main.go @@ -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, ), ) @@ -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 } @@ -167,7 +174,7 @@ 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) } @@ -175,7 +182,7 @@ func (c *command) runInForeground() error { } 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) }