mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2025-09-02 00:30:40 +00:00
cron
This commit is contained in:
12
internal/cron.go
Normal file
12
internal/cron.go
Normal 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
|
||||
}
|
@@ -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
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user