feat: add working watchdog cycle

This commit is contained in:
DaanSelen
2026-04-22 11:51:57 +02:00
parent bf04e97850
commit 0c287cc917
10 changed files with 166 additions and 82 deletions
+53 -16
View File
@@ -1,36 +1,73 @@
package database
import (
"eden-server/internal/utility"
"log/slog"
"os"
"path/filepath"
"gorm.io/gorm"
)
func watchdog(w string, db *gorm.DB) {
func watchdog(env utility.Environment, db *gorm.DB) {
slog.Info("performing the watchdog cycle")
files, err := GetFiles(db)
fsFiles, err := os.ReadDir(env.ContentDirectory)
if err != nil {
slog.Error("failed to read the content directory on the filesystem", "error", err)
return
}
dbFiles, err := GetFiles(db)
if err != nil {
slog.Error("failed to retrieve the files indexed from the database", "error", err)
return
}
var purgeList []string
for _, f := range files {
i, err := os.Stat(f.Filepath)
if err != nil {
if os.IsNotExist(err) {
purgeList = append(purgeList, f.Filepath)
continue
}
slog.Warn("stat failed", "file", f.Filepath, "error", err)
continue
}
// generate a set of filesystem contents
fsSet := make(map[string]struct{}) // cool name for the files that are (now) marked for annihilation
for _, f := range fsFiles {
// absolute path creation
fullPath := filepath.Join(env.ContentDirectory, f.Name())
fsSet[fullPath] = struct{}{}
}
if i.IsDir() {
purgeList = append(purgeList, f.Filepath) // also mark it for purger if its a directory. We do not want that here
// generate a set of Database contents
dbSet := make(map[string]File) // cool name for the files that are going to be deregistered from the database
for _, f := range dbFiles {
dbSet[f.FilePath] = f
}
// FS -> DB
// check for orphaned filesystem files
var fsPurgeScroll []string
for path := range fsSet {
if _, exists := dbSet[path]; !exists {
fsPurgeScroll = append(fsPurgeScroll, path)
}
}
// DB -> FS
// check stale database records
var dbPurgeScroll []File
for path, f := range dbSet {
if _, exists := fsSet[path]; !exists {
dbPurgeScroll = append(dbPurgeScroll, f)
}
}
if len(fsPurgeScroll) > 0 {
slog.Info("filesystem purge scroll is populated, engaging purge")
//filepath is stored in the slice, the filename or file object, see above
for _, fp := range fsPurgeScroll {
utility.RemoveFile(fp)
}
}
if len(dbPurgeScroll) > 0 {
slog.Info("database purge scroll is populated, engaging purge")
for _, f := range dbPurgeScroll {
DeregisterFile(db, f)
}
}
slog.Info("purge list", "files", purgeList)
}