Delete old checkpoints

This commit is contained in:
Locria Cyber 2023-10-08 09:54:36 +00:00
parent 5e2dde384d
commit 6d8ca4c4d4
Signed by: iacore
GPG key ID: F8C16E5157A63006
2 changed files with 32 additions and 6 deletions

View file

@ -8,7 +8,6 @@ import (
"os"
"path"
"sort"
"strings"
"sync"
"time"
@ -17,6 +16,8 @@ import (
. "git.exozy.me/exozyme/status/scanner"
)
const NumberOfCheckpointsToKeep = 3
type Row struct {
time time.Time
kind EventType
@ -123,9 +124,6 @@ func OpenDatabase(dir string) (Database, error) {
})
for _, file := range files {
filename := file.Name()
if strings.HasSuffix(filename, ".tmp") {
continue
}
_, err := ulid.Parse(filename)
if err != nil {
continue // because database files have ULID as names
@ -165,10 +163,37 @@ func (db *dumbDatabase) Persist() error {
defer db.mutex.Unlock()
db.sortRows()
id := ulid.Make().String() // checkpoint filename is ULID
dir := db.rootDir
// [section] clean up old checkpoints
files, err := os.ReadDir(dir)
if err != nil {
return err
}
// sort the file name desc. Since the file names are ULID, newer files are sorted first
sort.Slice(files, func(i, j int) bool {
return files[i].Name() > files[j].Name()
})
// delete old files
if len(files) > NumberOfCheckpointsToKeep {
for _, file := range files[NumberOfCheckpointsToKeep:] {
filename := file.Name()
_, err := ulid.Parse(filename)
if err != nil {
continue // because database files have ULID as names
}
fullpath := path.Join(dir, filename)
err = os.Remove(fullpath)
if err != nil {
log.Printf("Error when os.Delete: %v", err)
}
}
}
id := ulid.Make().String() // checkpoint filename is ULID
filename_final := path.Join(db.rootDir, id)
filename_tmp := path.Join(db.rootDir, id+".tmp")
file, err := os.Create(filename_tmp)
if err != nil {
return err
@ -187,6 +212,7 @@ func (db *dumbDatabase) Persist() error {
return err
}
log.Printf("Saved %d rows to: %v", len(db.rows), filename_final)
return nil
}

View file

@ -32,7 +32,7 @@ Services
- [x] self boot up/shutdown
- [x] check service status every 5 minutes since boot
- [x] persist data on disk
- [ ] clean up old checkpoints
- [x] clean up old checkpoints
- [x] Matrix notification
## Environment Variables