feat: make watchdog itself configurable
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"eden-server/internal/utility"
|
||||
"orbits-server/internal/utility"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
//var watchdogStop = make(chan struct{})
|
||||
|
||||
func KickoffDatabase(workDir string) (*gorm.DB, error) {
|
||||
dbLoc := filepath.Join(workDir, "garden.db")
|
||||
dbLoc := filepath.Join(workDir, "station.db")
|
||||
db, err := gorm.Open(sqlite.Open(dbLoc), &gorm.Config{
|
||||
Logger: logger.Discard, // disable gorm logging since its not slog (yet)
|
||||
TranslateError: true,
|
||||
@@ -24,7 +24,7 @@ func KickoffDatabase(workDir string) (*gorm.DB, error) {
|
||||
|
||||
// try to use GORM automigrate if the schema changes
|
||||
if err := db.AutoMigrate(
|
||||
&State{},
|
||||
&Command{},
|
||||
&Device{},
|
||||
&File{},
|
||||
); err != nil {
|
||||
@@ -32,8 +32,9 @@ func KickoffDatabase(workDir string) (*gorm.DB, error) {
|
||||
}
|
||||
|
||||
// create the first row if it does not exist yet
|
||||
if err := db.FirstOrCreate(&State{}, State{
|
||||
if err := db.FirstOrCreate(&Command{}, Command{
|
||||
ID: 0,
|
||||
State: "idle",
|
||||
MediaType: Unspecified,
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -19,8 +19,9 @@ type Timestamps struct {
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
type State struct {
|
||||
ID int `gorm:"primaryKey;not null;"`
|
||||
type Command struct {
|
||||
ID int `gorm:"primaryKey;not null;"`
|
||||
State string
|
||||
// unspecified
|
||||
// video
|
||||
// presentation
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"eden-server/internal/utility"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"orbits-server/internal/utility"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -61,11 +61,11 @@ func BuildFileRecord(r io.Reader, origName string, contentDirectory string) (Fil
|
||||
return fData, nil
|
||||
}
|
||||
|
||||
func GetState(db *gorm.DB) (State, error) {
|
||||
var state State
|
||||
func GetState(db *gorm.DB) (Command, error) {
|
||||
var state Command
|
||||
|
||||
if err := db.First(&state).Error; err != nil {
|
||||
return State{}, err
|
||||
return Command{}, err
|
||||
}
|
||||
|
||||
return state, nil
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"eden-server/internal/utility"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"orbits-server/internal/utility"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@@ -77,7 +76,7 @@ func watchdog(env utility.Environment, db *gorm.DB) {
|
||||
case "sync":
|
||||
readerStream, err := os.Open(fp)
|
||||
if err != nil {
|
||||
|
||||
slog.Error("failed to a reader stream")
|
||||
}
|
||||
defer readerStream.Close()
|
||||
|
||||
@@ -86,8 +85,6 @@ func watchdog(env utility.Environment, db *gorm.DB) {
|
||||
slog.Error("failed to enroll local file into the database", "error", err)
|
||||
}
|
||||
|
||||
fmt.Println(fileData)
|
||||
|
||||
if err := RegisterFile(db, fileData); err != nil {
|
||||
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
slog.Debug("discarding file since its a duplicate", "error", err)
|
||||
@@ -96,6 +93,8 @@ func watchdog(env utility.Environment, db *gorm.DB) {
|
||||
}
|
||||
}
|
||||
|
||||
// to fully finalize the enrollment process, we rename the locally inserted file to a unique filename
|
||||
// this is to make all files comply, wether uploaded via the api or locally inserted with the filesystem
|
||||
if err := os.Rename(fp, fileData.FilePath); err != nil {
|
||||
slog.Error("failed to move the locally inserted file", "error", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user