This commit is contained in:
2021-04-11 15:02:27 +02:00
parent 5d92b5bcc1
commit 8a1fe41825
6 changed files with 91 additions and 3 deletions

12
internal/cron.go Normal file
View File

@@ -0,0 +1,12 @@
package internal
func RunCron() error {
c := GetConfig()
for _, l := range c.Locations {
err := l.RunCron()
if err != nil {
return err
}
}
return nil
}

View File

@@ -5,6 +5,10 @@ import (
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/robfig/cron"
)
type HookArray = []string
@@ -163,3 +167,26 @@ func (l Location) Restore(to, from string, force bool) error {
}
return nil
}
func (l Location) RunCron() error {
if l.Cron == "" {
return nil
}
schedule, err := cron.ParseStandard(l.Cron)
if err != nil {
return err
}
last := lock.GetCron("test")
fmt.Println(last)
next := schedule.Next(time.Unix(last, 0))
fmt.Println(next)
now := time.Now()
if now.After(next) {
fmt.Println("Running")
lock.SetCron("test", now.Unix())
} else {
fmt.Println("Not due yet")
}
return nil
}

View File

@@ -28,7 +28,7 @@ func getLock() *viper.Viper {
return lock
}
func set(locked bool) error {
func setLock(locked bool) error {
lock := getLock()
if locked {
running := lock.GetBool("running")
@@ -43,10 +43,20 @@ func set(locked bool) error {
return nil
}
func GetCron(location string) int64 {
lock := getLock()
return lock.GetInt64("cron." + location)
}
func SetCron(location string, value int64) {
lock.Set("cron."+location, value)
lock.WriteConfigAs(file)
}
func Lock() error {
return set(true)
return setLock(true)
}
func Unlock() error {
return set(false)
return setLock(false)
}