Compare commits

..

5 Commits

Author SHA1 Message Date
Frederik Ring
4c74313222 Periodically collect runtime info when requested 2024-02-12 16:04:12 +01:00
Frederik Ring
de03d4f704 Docker client expects to be closed after usage in long running program 2024-02-12 16:04:12 +01:00
Frederik Ring
65626dd3d4 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-12 16:04:12 +01:00
Frederik Ring
69eceb3982 Entrypoint script is not needed anymore (#346) 2024-02-12 16:04:12 +01:00
pixxon
1d45062100 Move cron scheduling inside application (#338)
* Move cron scheduling inside application

* Make envvar a fallback and check for errors

* Panic significantly less

* propagate error out of runBackup

* Add structured logging

* FIx error propagation to exit

* Enable the new scheduler by default

* Review fixes

* Added docs and better error propagation
2024-02-12 16:04:12 +01:00
2 changed files with 32 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"os/signal" "os/signal"
"runtime"
"syscall" "syscall"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
@@ -122,7 +123,7 @@ func runScript(c *Config) (err error) {
return runErr return runErr
} }
func (c *command) runInForeground() error { func (c *command) runInForeground(profileCronExpression string) error {
cr := cron.New( cr := cron.New(
cron.WithParser( cron.WithParser(
cron.NewParser( cron.NewParser(
@@ -189,6 +190,28 @@ func (c *command) runInForeground() error {
} }
} }
if profileCronExpression != "" {
if _, err := cr.AddFunc(profileCronExpression, func() {
memStats := runtime.MemStats{}
runtime.ReadMemStats(&memStats)
c.logger.Info(
"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)
}
}
var quit = make(chan os.Signal, 1) var quit = make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT) signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
cr.Start() cr.Start()
@@ -214,11 +237,12 @@ func (c *command) runAsCommand() error {
func main() { func main() {
foreground := flag.Bool("foreground", false, "run the tool in the foreground") 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() flag.Parse()
c := newCommand() c := newCommand()
if *foreground { if *foreground {
c.must(c.runInForeground()) c.must(c.runInForeground(*profile))
} else { } else {
c.must(c.runAsCommand()) 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") return nil, fmt.Errorf("newScript: failed to create docker client")
} }
s.cli = cli 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) { logFunc := func(logType storage.LogLevel, context string, msg string, params ...any) {