Compare commits

..

5 Commits

Author SHA1 Message Date
Frederik Ring
106fa2f204 Also log number of objects in heap 2024-02-11 12:17:13 +01:00
Frederik Ring
d3e82c0c5c Possible error on scheduling profiling job is unchecked 2024-02-10 12:19:40 +01:00
Frederik Ring
01e41f45d1 Periodically collect runtime info when requested 2024-02-10 12:10:16 +01:00
Frederik Ring
68ba6ce5a1 Docker client expects to be closed after usage in long running program 2024-02-09 20:55:37 +01:00
Frederik Ring
7cfea62933 Hoist control for exiting script a level up (#348)
* 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
2024-02-09 10:24:28 +01:00
4 changed files with 79 additions and 11 deletions

View File

@@ -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

29
cmd/backup/cron.go Normal file
View File

@@ -0,0 +1,29 @@
// 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

@@ -9,6 +9,7 @@ import (
"log/slog"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/robfig/cron/v3"
@@ -122,7 +123,7 @@ func runScript(c *Config) (err error) {
return runErr
}
func (c *command) runInForeground() error {
func (c *command) runInForeground(profileCronExpression string) error {
cr := cron.New(
cron.WithParser(
cron.NewParser(
@@ -131,11 +132,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 +154,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 +175,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,13 +183,32 @@ 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)
}
}
}
if profileCronExpression != "" {
if _, err := cr.AddFunc(profileCronExpression, func() {
memStats := runtime.MemStats{}
runtime.ReadMemStats(&memStats)
c.logger.Info(
fmt.Sprintf("Successfully scheduled backup with expression %s", config.BackupCronExpression),
"Collecting runtime information",
"num_goroutines",
runtime.NumGoroutine(),
"memory_heap_alloc",
formatBytes(memStats.HeapAlloc, false),
"memory_heap_inuse",
formatBytes(memStats.HeapInuse, false),
"memory_heap_sys",
formatBytes(memStats.HeapSys, false),
"memory_heap_objects",
memStats.HeapObjects,
)
}); err != nil {
return fmt.Errorf("runInForeground: error adding profiling job: %w", err)
}
}
@@ -210,11 +237,12 @@ func (c *command) runAsCommand() error {
func main() {
foreground := flag.Bool("foreground", false, "run the tool in the foreground")
profile := flag.String("profile", "", "collect runtime metrics and log them periodically on the given cron expression")
flag.Parse()
c := newCommand()
if *foreground {
c.must(c.runInForeground())
c.must(c.runInForeground(*profile))
} else {
c.must(c.runAsCommand())
}

View File

@@ -112,6 +112,12 @@ func newScript(c *Config) (*script, error) {
return nil, fmt.Errorf("newScript: failed to create docker client")
}
s.cli = cli
s.registerHook(hookLevelPlumbing, func(err error) error {
if err := s.cli.Close(); err != nil {
return fmt.Errorf("newScript: failed to close docker client: %w", err)
}
return nil
})
}
logFunc := func(logType storage.LogLevel, context string, msg string, params ...any) {