From 081c6cf95ca49743d3666cafdd2ac83c73cc4186 Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Tue, 2 Nov 2021 14:36:07 +0100 Subject: [PATCH] Scheduler first steps --- scheduler/scheduler.go | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 1dd2b1b..5021647 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -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!") + } +}