2021-10-27 23:05:36 +02:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/qwc/backive/config"
|
|
|
|
"github.com/qwc/backive/db"
|
|
|
|
)
|
|
|
|
|
2021-10-27 23:17:18 +02:00
|
|
|
// Runs contains the Data for the scheduler: mapping from backups to a list of timestamps of the last 10 backups
|
2021-10-27 23:05:36 +02:00
|
|
|
type Runs map[string][]time.Time
|
|
|
|
|
|
|
|
var runs Runs
|
|
|
|
|
2021-10-27 23:17:18 +02:00
|
|
|
// Load loads the data from the json database
|
2021-10-27 23:05:36 +02:00
|
|
|
func Load() {
|
|
|
|
runerr := json.Unmarshal([]byte(db.Database["runs"]), &runs)
|
|
|
|
if runerr != nil {
|
|
|
|
panic(runerr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 23:17:18 +02:00
|
|
|
// Save saves the data into the json database
|
2021-10-27 23:05:36 +02:00
|
|
|
func Save() {
|
|
|
|
str, err := json.Marshal(runs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.Database["runs"] = string(str)
|
|
|
|
}
|
|
|
|
|
2021-10-27 23:17:18 +02:00
|
|
|
// ShouldRun Takes a backup key and returns a bool if a backup should run now.
|
2021-10-27 23:05:36 +02:00
|
|
|
func ShouldRun(backup string) bool {
|
2021-10-27 23:17:18 +02:00
|
|
|
backupdata := config.Get().Backups[backup]
|
|
|
|
freq := backupdata.Frequency
|
2021-10-27 23:05:36 +02:00
|
|
|
// calculate time difference from last run, return true if no run has taken place
|
|
|
|
if freq > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|