wolbodge/cron.go
2025-03-22 13:19:55 +01:00

66 lines
1.5 KiB
Go

package main
import (
"log"
"time"
"git.1e99.eu/1e99/wolbodge/devices"
"git.1e99.eu/1e99/wolbodge/sessions"
)
func DeviceCheck(logger *log.Logger, device devices.Device, wantStatus func() (bool, error), exit <-chan bool) {
for {
select {
case <-exit:
break
default:
status, err := device.Status()
if err != nil {
logger.Printf("Failed to fetch device status, waiting 1 minute until next attempt: %s", err)
time.Sleep(1 * time.Minute)
continue
}
want, err := wantStatus()
if err != nil {
logger.Printf("Failed to fetch want status, waiting 1 minute until next attempt: %s", err)
time.Sleep(1 * time.Minute)
continue
}
if status == want {
// Don't lock up our single-threaded session storage too much
time.Sleep(2 * time.Second)
continue
}
err = device.PushPowerButton()
if err != nil {
logger.Printf("Failed to push power button, waiting 2 minutes until next attempt: %s", err)
// Cooldown to not overwhelm the device with start/stop requests
time.Sleep(2 * time.Minute)
return
}
logger.Printf("Pushed power button")
time.Sleep(2 * time.Minute)
}
}
}
func PruneStorage(logger *log.Logger, storage sessions.Storage, interval time.Duration, exit <-chan bool) {
for {
select {
case <-exit:
break
default:
/*pruned, err := storage.PruneTickets()
if err != nil {
logger.Printf("Failed to prune tickets: %s", err)
}
logger.Printf("Pruned %d tickets", pruned)
time.Sleep(interval)*/
}
}
}