Maybe fix sleep time and add it to status page
This commit is contained in:
parent
bccc177374
commit
7cfcfa330c
38
main.go
38
main.go
@ -23,14 +23,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
checkInterval = 10 * time.Second
|
checkInterval = 10 * time.Second
|
||||||
monitoringPeriod = 5 * time.Minute
|
monitoringPeriod = 5 * time.Minute
|
||||||
resumeGracePeriod = 5 * time.Minute // Time to wait after resume before allowing sleep again
|
cpuThreshold = 20.0 // percentage
|
||||||
cpuThreshold = 20.0 // percentage
|
gpuThreshold = 20.0 // percentage
|
||||||
gpuThreshold = 20.0 // percentage
|
diskThreshold = 5 * 1024 * 1024 // 5 MB/s
|
||||||
diskThreshold = 5 * 1024 * 1024 // 5 MB/s
|
networkThreshold = 1 * 1024 * 1024 // 1 MB/s
|
||||||
networkThreshold = 1 * 1024 * 1024 // 1 MB/s
|
httpPort = 8081
|
||||||
httpPort = 8081
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ResourceUsage struct {
|
type ResourceUsage struct {
|
||||||
@ -48,16 +47,17 @@ type StatusResponse struct {
|
|||||||
Usage ResourceUsage `json:"usage"`
|
Usage ResourceUsage `json:"usage"`
|
||||||
Blockers []string `json:"blockers"`
|
Blockers []string `json:"blockers"`
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
SleepAt time.Time `json:"sleep_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
statusMutex sync.RWMutex
|
statusMutex sync.RWMutex
|
||||||
blockers []string
|
blockers []string
|
||||||
currentStatus ResourceUsage
|
currentStatus ResourceUsage
|
||||||
nvmlAvailable bool
|
nvmlAvailable bool
|
||||||
networkBytesIO uint64
|
networkBytesIO uint64
|
||||||
diskBytesIO uint64
|
diskBytesIO uint64
|
||||||
lastBlockedTime time.Time
|
nextSleepTime time.Time
|
||||||
)
|
)
|
||||||
|
|
||||||
func Must(err error) {
|
func Must(err error) {
|
||||||
@ -105,7 +105,6 @@ func main() {
|
|||||||
log.Printf("- No active SSH connections\n")
|
log.Printf("- No active SSH connections\n")
|
||||||
log.Printf("- No active user sessions\n")
|
log.Printf("- No active user sessions\n")
|
||||||
log.Printf("- Over the last %v\n", monitoringPeriod)
|
log.Printf("- Over the last %v\n", monitoringPeriod)
|
||||||
log.Printf("- System will not suspend for %v after resuming from sleep\n", resumeGracePeriod)
|
|
||||||
log.Printf("HTTP status endpoint available at http://localhost:%d/status\n", httpPort)
|
log.Printf("HTTP status endpoint available at http://localhost:%d/status\n", httpPort)
|
||||||
log.Printf("Press Ctrl+C to exit\n")
|
log.Printf("Press Ctrl+C to exit\n")
|
||||||
|
|
||||||
@ -119,7 +118,7 @@ mainLoop:
|
|||||||
updateCurrentUsage()
|
updateCurrentUsage()
|
||||||
updateSystemStatus()
|
updateSystemStatus()
|
||||||
|
|
||||||
if time.Now().Sub(lastBlockedTime) >= monitoringPeriod {
|
if time.Now().After(nextSleepTime) {
|
||||||
log.Printf("System status before suspend:\n")
|
log.Printf("System status before suspend:\n")
|
||||||
log.Printf("- CPU: %.1f%%\n", usageHistory[len(usageHistory)-1].CpuUsage)
|
log.Printf("- CPU: %.1f%%\n", usageHistory[len(usageHistory)-1].CpuUsage)
|
||||||
if nvmlAvailable {
|
if nvmlAvailable {
|
||||||
@ -131,7 +130,7 @@ mainLoop:
|
|||||||
if err := suspendSystem(); err != nil {
|
if err := suspendSystem(); err != nil {
|
||||||
log.Printf("Failed to suspend system: %v", err)
|
log.Printf("Failed to suspend system: %v", err)
|
||||||
}
|
}
|
||||||
lastBlockedTime = time.Now()
|
nextSleepTime = time.Now().Add(monitoringPeriod)
|
||||||
log.Printf("Resumed")
|
log.Printf("Resumed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -172,6 +171,7 @@ func handleStatus(w http.ResponseWriter, _ *http.Request) {
|
|||||||
Usage: currentStatus,
|
Usage: currentStatus,
|
||||||
Blockers: blockers,
|
Blockers: blockers,
|
||||||
Timestamp: time.Now(),
|
Timestamp: time.Now(),
|
||||||
|
SleepAt: nextSleepTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
@ -218,7 +218,7 @@ func updateSystemStatus() {
|
|||||||
if len(blockers) == 0 {
|
if len(blockers) == 0 {
|
||||||
blockers = append(blockers, "No blockers - system can sleep")
|
blockers = append(blockers, "No blockers - system can sleep")
|
||||||
} else {
|
} else {
|
||||||
lastBlockedTime = time.Now()
|
nextSleepTime = time.Now().Add(monitoringPeriod)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user