feat: add new features such as database watchdog

This commit is contained in:
DaanSelen
2026-04-21 16:27:04 +02:00
parent 610fdffdb8
commit c4a4fafb52
16 changed files with 428 additions and 158 deletions
+42 -13
View File
@@ -1,36 +1,65 @@
package database
import (
"eden-server/internal/runtime"
"path/filepath"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func KickoffDatabase() (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open("garden.db"), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
})
//var watchdogStop = make(chan struct{})
func KickoffDatabase(workDir string) (*gorm.DB, error) {
dbLoc := filepath.Join(workDir, "data", "garden.db")
db, err := gorm.Open(sqlite.Open(dbLoc), &gorm.Config{})
if err != nil {
return nil, err
}
if err := db.AutoMigrate(&AppState{}); err != nil {
if err := db.AutoMigrate(&State{}); err != nil {
return nil, err
}
if err := db.AutoMigrate(&Files{}); err != nil {
if err := db.AutoMigrate(&File{}); err != nil {
return nil, err
}
// create the first row if it does not exist yet
if err := db.FirstOrCreate(&AppState{}, AppState{
ID: 1,
Mode: "unspecified",
Running: false,
if err := db.FirstOrCreate(&State{}, State{
ID: 0,
Mode: "unspecified",
}).Error; err != nil {
return nil, err
}
return db, nil
}
func KickoffDatabaseWatchdog(env runtime.Environment, db *gorm.DB) {
timeInterval := time.Second * time.Duration(env.WatchInterval)
ticker := time.NewTicker(timeInterval)
go func() {
defer ticker.Stop()
/*
// Possible future mechanism to stop the watchdog
// must be inside a non-conditional for loop
select {
case <-ticker.C: // ticker event
watchdog(env.DataDirectory, db)
case <-watchdogStop:
return
}
*/
// run the watchdog function once to see if all is well.
watchdog(env.DataDirectory, db)
// then defer to a decoupled/disowned golang goroutine
for range ticker.C {
watchdog(env.DataDirectory, db)
}
}()
}
+43 -13
View File
@@ -1,19 +1,49 @@
package database
type AppState struct {
import (
"time"
"gorm.io/datatypes"
)
type Mode string
const (
ModeUnspecified Mode = "unspecified"
ModeVideo Mode = "video"
ModePresentation Mode = "presentation"
ModeInternet Mode = "internet"
)
type State struct {
ID int `gorm:"primaryKey"`
// Mode =
// 0: unspecified
// 1: video
// 2: presentation
// 3: internet URL
Mode string
Running bool
// unspecified
// video
// presentation
// internet URL
Mode Mode `gorm:"type:varchar(20);not null"` // Must specify what kind of file it is
Targets datatypes.JSON
Location string // Must be the location where the file is downloadable on the API
UpdatedAt time.Time
}
type Files struct {
ID int `gorm:"primaryKey"`
Mode string
Filename string
Filepath string
type Device struct {
ID int `gorm:"primaryKey"`
DeviceType string
Hostname string
RemoteAddress string
Alive bool
Compliant bool
CreatedAt time.Time
UpdatedAt time.Time
}
type File struct {
ID int `gorm:"primaryKey"`
Mode Mode `gorm:"type:varchar(20);not null"`
GivenName string
Filepath string
Checksum string // base64 encoded sha512 checksum
CreatedAt time.Time
UpdatedAt time.Time
}
+6 -14
View File
@@ -1,29 +1,21 @@
package database
import (
"path/filepath"
"gorm.io/gorm"
)
func GetAppState(db *gorm.DB) (AppState, error) {
var state AppState
func GetState(db *gorm.DB) (State, error) {
var state State
return state, db.First(&state).Error
}
func GetFiles(db *gorm.DB) ([]Files, error) {
var files []Files
func GetFiles(db *gorm.DB) ([]File, error) {
var files []File
return files, db.Find(&files).Error
}
func RegisterFile(db *gorm.DB, category, fullPath string) error {
file := Files{
Mode: category,
Filename: filepath.Base(fullPath),
Filepath: fullPath,
}
return db.Create(&file).Error
func RegisterFile(db *gorm.DB, f File) error {
return db.Create(&f).Error
}
+20
View File
@@ -0,0 +1,20 @@
package database
import (
"log"
"log/slog"
"gorm.io/gorm"
)
func watchdog(w string, db *gorm.DB) {
slog.Info("performing the watchdog cycle")
files, err := GetFiles(db)
if err != nil {
slog.Error("failed to retrieve the files indexed from the database", "error", err)
return
}
log.Println(files)
}