From 9cecce4f2d93b15e739dfcd678c082388f3cc961 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 30 Dec 2021 12:57:58 +0100 Subject: [PATCH] feat: replace interval with a proper cron schedule --- README.md | 9 +++++---- config.go | 15 +++++---------- go.mod | 1 + go.sum | 2 ++ main.go | 12 +++++++----- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 35a829d..ff5c46f 100644 --- a/README.md +++ b/README.md @@ -46,12 +46,13 @@ Where all backup files will be stored. Default: `/backups` -### `BACKUP_INTERVAL` +### `BACKUP_SCHEDULE` -The interval at which backups will happen. Must be a parsable string -as [time.Duration](https://pkg.go.dev/time#ParseDuration). Must be positive. +The cron schedule at which the backup job runs, using the common unix cron syntax. -Default: `3h` +Check out [crontab.dev](https://crontab.dev/) for a nice explanation of the schedule. + +Default: `* */6 * * * *` (every 6 hours) ### `BACKUP_MAX` diff --git a/config.go b/config.go index 6a84550..ac567e7 100644 --- a/config.go +++ b/config.go @@ -15,7 +15,7 @@ import ( type conf struct { Folder string // Backup folder _with_ trailing slash fullCurrentBackupPath string - Interval time.Duration + Schedule string MaxBackups int } @@ -26,14 +26,14 @@ var ( const ( envBackupFolder = `BACKUP_FOLDER` - envInterval = `BACKUP_INTERVAL` + envSchedule = `BACKUP_SCHEDULE` envMax = `BACKUP_MAX` ) func init() { config = &conf{ Folder: "/backups/", - Interval: time.Hour * 6, + Schedule: "* */6 * * * *", MaxBackups: 12, } @@ -46,14 +46,9 @@ func init() { config.Folder = folder } - var err error - - interval, has := os.LookupEnv(envInterval) + schedule, has := os.LookupEnv(envSchedule) if has { - config.Interval, err = time.ParseDuration(interval) - if err != nil { - log.Fatalf("Invalid interval: %s\n", err) - } + config.Schedule = schedule } max, has := os.LookupEnv(envMax) diff --git a/go.mod b/go.mod index dfb003f..05177b2 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/gorilla/mux v1.8.0 // indirect github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect github.com/morikuni/aec v1.0.0 // indirect + github.com/robfig/cron/v3 v3.0.1 golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect google.golang.org/grpc v1.40.0 // indirect ) diff --git a/go.sum b/go.sum index cc94777..5b7ac8e 100644 --- a/go.sum +++ b/go.sum @@ -515,6 +515,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= diff --git a/main.go b/main.go index 09b05d2..89f3792 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,8 @@ package main import ( + "github.com/robfig/cron/v3" "log" - "time" ) func main() { @@ -11,7 +11,8 @@ func main() { log.Fatalf("Could not create client: %s", err) } - for { + cr := cron.New() + _, err = cr.AddFunc(config.Schedule, func() { updateFullBackupPath() containers, err := getContainers(c) @@ -32,8 +33,9 @@ func main() { } log.Println("Done.") - log.Printf("Sleeping for %s\n", config.Interval) - - time.Sleep(config.Interval) + }) + if err != nil { + log.Fatalf("Could not create cron job: %s\n", err) } + cr.Start() }