Files
AetherForge/server/internal/db/stale_agents.go
AetherForge 72ae457cca
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Add scale limit constants, stale-agent indexed query, and tests.
Extract fleet caps (128 hosts, sem=16, 250ms coalesce, syscheck=20) into
named constants with Go tests; wire ListStaleOnlineAgents for stale sweeps
instead of full ListAgents scans.
2026-06-07 06:40:12 -07:00

31 lines
770 B
Go

package db
import (
"time"
"crypto-miner-server/internal/models"
)
// ListStaleOnlineAgents returns online agents whose last_seen is older than
// the given threshold. Uses indexed status+last_seen filters instead of a full
// table scan.
func (d *Database) ListStaleOnlineAgents(olderThan time.Duration) ([]*models.Agent, error) {
cutoff := time.Now().Add(-olderThan)
query := `SELECT ` + agentSelectCols + ` FROM agents WHERE status = 'online' AND last_seen < ? ORDER BY last_seen ASC`
rows, err := d.Query(query, cutoff)
if err != nil {
return nil, err
}
defer rows.Close()
var agents []*models.Agent
for rows.Next() {
a, err := d.scanAgent(rows)
if err != nil {
return nil, err
}
agents = append(agents, a)
}
return agents, rows.Err()
}