Compare commits

..

3 Commits

Author SHA1 Message Date
Frederik Ring
abfeb93628 Log when running schedule 2024-02-07 21:39:18 +01:00
Frederik Ring
cb31554791 Do not accidentally nil out errors 2024-02-07 21:29:02 +01:00
Frederik Ring
749ca85db9 Hoist control for exiting script a level up 2024-02-07 20:15:14 +01:00
3 changed files with 11 additions and 49 deletions

View File

@@ -48,12 +48,7 @@ func loadEnvVars() (*Config, error) {
return loadConfig(os.LookupEnv)
}
type configFile struct {
name string
config *Config
}
func loadEnvFiles(directory string) ([]configFile, error) {
func loadEnvFiles(directory string) ([]*Config, error) {
items, err := os.ReadDir(directory)
if err != nil {
if os.IsNotExist(err) {
@@ -62,7 +57,7 @@ func loadEnvFiles(directory string) ([]configFile, error) {
return nil, fmt.Errorf("loadEnvFiles: failed to read files from env directory: %w", err)
}
cs := []configFile{}
var cs = make([]*Config, 0)
for _, item := range items {
if item.IsDir() {
continue
@@ -80,7 +75,7 @@ func loadEnvFiles(directory string) ([]configFile, error) {
if err != nil {
return nil, fmt.Errorf("loadEnvFiles: error loading config from file %s: %w", p, err)
}
cs = append(cs, configFile{config: c, name: item.Name()})
cs = append(cs, c)
}
return cs, nil

View File

@@ -1,29 +0,0 @@
// Copyright 2024 - Offen Authors <hioffen@posteo.de>
// SPDX-License-Identifier: MPL-2.0
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
}

View File

@@ -131,11 +131,11 @@ func (c *command) runInForeground() error {
),
)
addJob := func(config *Config, name string) error {
addJob := func(config *Config) error {
if _, err := cr.AddFunc(config.BackupCronExpression, func() {
c.logger.Info(
fmt.Sprintf(
"Now running script on schedule %s",
"Now running schedule %s",
config.BackupCronExpression,
),
)
@@ -153,14 +153,7 @@ 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 %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),
)
}
c.logger.Info(fmt.Sprintf("Successfully scheduled backup with expression %s", config.BackupCronExpression))
return nil
}
@@ -174,7 +167,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, "from environment")
err = addJob(c)
if err != nil {
return fmt.Errorf("runInForeground: error adding job from env: %w", err)
}
@@ -182,10 +175,13 @@ 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.config, config.name)
err = addJob(config)
if err != nil {
return fmt.Errorf("runInForeground: error adding jobs from conf files: %w", err)
}
c.logger.Info(
fmt.Sprintf("Successfully scheduled backup with expression %s", config.BackupCronExpression),
)
}
}