feat: implement automated scan history cleanup with configurable retention period
Build and Push / build (nucleus, amd64, linux) (push) Successful in 17s

This commit is contained in:
2026-07-17 09:25:10 +01:00
parent 94dbd610e1
commit 1796c1696f
2 changed files with 34 additions and 0 deletions
+3
View File
@@ -74,6 +74,9 @@ Environment="SMTP_TO=admin@example.com"
# Environment="WEB_USER=admin" # Environment="WEB_USER=admin"
# Environment="WEB_PASS=supersecret" # Environment="WEB_PASS=supersecret"
# Optional: Number of days to retain scan history in the database (Default: 30)
# Environment="RETENTION_DAYS=30"
Restart=always Restart=always
RestartSec=5 RestartSec=5
+31
View File
@@ -1,7 +1,10 @@
package scheduler package scheduler
import ( import (
"fmt"
"log" "log"
"os"
"strconv"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
@@ -15,6 +18,34 @@ var c *cron.Cron
func InitScheduler() { func InitScheduler() {
c = cron.New() c = cron.New()
// Automated Data Cleanup Job (Runs every day at midnight)
c.AddFunc("@daily", func() {
days := os.Getenv("RETENTION_DAYS")
if days == "" {
days = "30"
}
retentionDays, err := strconv.Atoi(days)
if err != nil {
log.Printf("Invalid RETENTION_DAYS environment variable: %s", days)
retentionDays = 30
}
log.Printf("Running automated cleanup (Retention: %d days)", retentionDays)
modifier := fmt.Sprintf("-%d days", retentionDays)
res, err := db.DB.Exec("DELETE FROM scans WHERE started_at < date('now', ?)", modifier)
if err != nil {
log.Printf("Cleanup failed: %v", err)
return
}
rowsAffected, _ := res.RowsAffected()
if rowsAffected > 0 {
log.Printf("Cleanup complete: removed %d old scans.", rowsAffected)
}
})
// Load all targets // Load all targets
rows, err := db.DB.Query("SELECT id, name, address, schedule FROM targets") rows, err := db.DB.Query("SELECT id, name, address, schedule FROM targets")
if err != nil { if err != nil {