mirror of
https://github.com/JustKato/drive-health.git
synced 2026-04-30 01:42:29 +03:00
Cleanup Service
* Updated .env + Implemented automatical removal of old logs
This commit is contained in:
@@ -14,15 +14,16 @@ import (
|
||||
|
||||
var db *gorm.DB
|
||||
|
||||
// Initialize the database connection
|
||||
func InitDB() {
|
||||
var err error
|
||||
dbPath := config.GetConfiguration().DatabaseFilePath
|
||||
if dbPath == "" {
|
||||
dbPath = "./data.sqlite"
|
||||
}
|
||||
|
||||
db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
// This should basically never happen, unless the path to the database
|
||||
// is inaccessible, doesn't exist or there's no permission to it, which
|
||||
// should and will crash the program
|
||||
panic("failed to connect database")
|
||||
}
|
||||
|
||||
@@ -30,10 +31,12 @@ func InitDB() {
|
||||
db.AutoMigrate(&hardware.HardDrive{}, &hardware.HardDriveTemperature{})
|
||||
}
|
||||
|
||||
// Fetch the open database pointer
|
||||
func GetDatabaseRef() *gorm.DB {
|
||||
return db
|
||||
}
|
||||
|
||||
// Log the temperature of the disks
|
||||
func LogDriveTemps() error {
|
||||
drives, err := hardware.GetSystemHardDrives(db, nil, nil)
|
||||
if err != nil {
|
||||
@@ -52,8 +55,9 @@ func LogDriveTemps() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run the logging service, this will periodically log the temperature of the disks with the LogDriveTemps function
|
||||
func RunLoggerService() {
|
||||
fmt.Println("Initializing Temperature Logging Service...")
|
||||
fmt.Println("[🦝] Initializing Temperature Logging Service...")
|
||||
|
||||
tickTime := time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second
|
||||
|
||||
@@ -63,12 +67,13 @@ func RunLoggerService() {
|
||||
time.Sleep(tickTime)
|
||||
err := LogDriveTemps()
|
||||
if err != nil {
|
||||
fmt.Printf("🛑 Temperature logging failed: %s\n", err)
|
||||
fmt.Printf("[🛑] Temperature logging failed: %s\n", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Generate a PNG based upon a HDD id and a date range
|
||||
func GetDiskGraphImage(hddID int, newerThan *time.Time, olderThan *time.Time) (*bytes.Buffer, error) {
|
||||
var hdd hardware.HardDrive
|
||||
// Fetch by a combination of fields
|
||||
|
||||
45
lib/svc/service_cleanup.go
Normal file
45
lib/svc/service_cleanup.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/config"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/hardware"
|
||||
)
|
||||
|
||||
// Delete all thermal entries that are older than X amount of seconds
|
||||
func CleanupOldData() error {
|
||||
cfg := config.GetConfiguration()
|
||||
|
||||
beforeDate := time.Now().Add(-1 * time.Duration(cfg.MaxHistoryAge) * time.Second)
|
||||
|
||||
deleteResult := db.Where("time_stamp < ?", beforeDate).Delete(&hardware.HardDriveTemperature{})
|
||||
if deleteResult.Error != nil {
|
||||
fmt.Printf("[🛑] Error during cleanup: %s\n", deleteResult.Error)
|
||||
return db.Error
|
||||
}
|
||||
|
||||
if deleteResult.RowsAffected > 0 {
|
||||
fmt.Printf("[🛑] Cleaned up %v entries before %s\n", deleteResult.RowsAffected, beforeDate)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunCleanupService() {
|
||||
fmt.Println("[🦝] Initializing Log Cleanup Service...")
|
||||
|
||||
tickTime := time.Duration(config.GetConfiguration().CleanupServiceFrequency) * time.Second
|
||||
|
||||
// Snapshot taking routine
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(tickTime)
|
||||
err := CleanupOldData()
|
||||
if err != nil {
|
||||
fmt.Printf("🛑 Cleanup process failed: %s\n", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user