mirror of https://github.com/qwc/backive.git
Scheduler first steps
This commit is contained in:
parent
6291ee5ea7
commit
081c6cf95c
|
@ -1,15 +1,21 @@
|
|||
package scheduler
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/qwc/backive/config"
|
||||
"github.com/qwc/backive/db"
|
||||
)
|
||||
|
||||
type backupRuns struct {
|
||||
runlist *list.List
|
||||
}
|
||||
|
||||
// Runs contains the Data for the scheduler: mapping from backups to a list of timestamps of the last 10 backups
|
||||
type Runs map[string][]time.Time
|
||||
type Runs map[string]backupRuns
|
||||
|
||||
var runs Runs
|
||||
|
||||
|
@ -27,7 +33,6 @@ func Save() {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
db.Database["runs"] = string(str)
|
||||
}
|
||||
|
||||
|
@ -36,9 +41,37 @@ func ShouldRun(backup string) bool {
|
|||
backupdata := config.Get().Backups[backup]
|
||||
freq := backupdata.Frequency
|
||||
// calculate time difference from last run, return true if no run has taken place
|
||||
if freq > 0 {
|
||||
lr, ok := LastRun(backup)
|
||||
if ok == nil {
|
||||
dur := time.Since(lr)
|
||||
days := dur.Hours() / 24
|
||||
if days > float64(freq) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if freq == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// RegisterRun saves a date of a backup run into the internal storage
|
||||
func RegisterRun(backup string) {
|
||||
nbl, ok := runs[backup]
|
||||
if !ok {
|
||||
nbl.runlist = list.New()
|
||||
runs[backup] = nbl
|
||||
}
|
||||
nbl.runlist.PushFront(time.Now())
|
||||
}
|
||||
|
||||
// LastRun returns the time.Time of the last run of the backup given.
|
||||
func LastRun(backup string) (time.Time, error) {
|
||||
_, ok := runs[backup]
|
||||
if ok {
|
||||
var t = time.Time(runs[backup].runlist.Front().Value.(time.Time))
|
||||
return t, nil
|
||||
} else {
|
||||
return time.Unix(0, 0), fmt.Errorf("Backup name not found and therefore has never run!")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue