Skip to content

Commit

Permalink
Hoist control for exiting script a level up (#348)
Browse files Browse the repository at this point in the history
* Hoist control for exiting script a level up

* Do not accidentally nil out errors

* Log when running schedule

* Remove duplicate log line

* Warn on cron schedule that will never run
  • Loading branch information
m90 committed Feb 12, 2024
1 parent 69eceb3 commit 65626dd
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 119 deletions.
44 changes: 25 additions & 19 deletions cmd/backup/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func loadConfig(lookup envProxy) (*Config, error) {

var c = &Config{}
if err := envconfig.Process("", c); err != nil {
return nil, fmt.Errorf("failed to process configuration values, error: %w", err)
return nil, fmt.Errorf("loadConfig: failed to process configuration values: %w", err)
}

return c, nil
Expand All @@ -48,33 +48,39 @@ 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) {
return nil, err
}
return nil, fmt.Errorf("failed to read files from env directory, error: %w", err)
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() {
p := filepath.Join(directory, item.Name())
envFile, err := godotenv.Read(p)
if err != nil {
return nil, fmt.Errorf("error reading config file %s, error: %w", p, err)
}
lookup := func(key string) (string, bool) {
val, ok := envFile[key]
return val, ok
}
c, err := loadConfig(lookup)
if err != nil {
return nil, fmt.Errorf("error loading config from file %s, error: %w", p, err)
}
cs = append(cs, c)
if item.IsDir() {
continue
}
p := filepath.Join(directory, item.Name())
envFile, err := godotenv.Read(p)
if err != nil {
return nil, fmt.Errorf("loadEnvFiles: error reading config file %s: %w", p, err)
}
lookup := func(key string) (string, bool) {
val, ok := envFile[key]
return val, ok
}
c, err := loadConfig(lookup)
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()})
}

return cs, nil
Expand Down
29 changes: 29 additions & 0 deletions cmd/backup/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 - Offen Authors <[email protected]>
// 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
}
15 changes: 10 additions & 5 deletions cmd/backup/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,18 @@ func (s *script) withLabeledCommands(step lifecyclePhase, cb func() error) func(
if s.cli == nil {
return cb
}
return func() error {
if err := s.runLabeledCommands(fmt.Sprintf("docker-volume-backup.%s-pre", step)); err != nil {
return fmt.Errorf("withLabeledCommands: %s: error running pre commands: %w", step, err)
return func() (err error) {
if err = s.runLabeledCommands(fmt.Sprintf("docker-volume-backup.%s-pre", step)); err != nil {
err = fmt.Errorf("withLabeledCommands: %s: error running pre commands: %w", step, err)
return
}
defer func() {
s.must(s.runLabeledCommands(fmt.Sprintf("docker-volume-backup.%s-post", step)))
derr := s.runLabeledCommands(fmt.Sprintf("docker-volume-backup.%s-post", step))
if err == nil && derr != nil {
err = derr
}
}()
return cb()
err = cb()
return
}
}
Loading

0 comments on commit 65626dd

Please sign in to comment.