2021-08-05 09:43:35 +02:00
|
|
|
package watcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.xsfx.dev/schnutibox/internal/metrics"
|
|
|
|
"go.xsfx.dev/schnutibox/pkg/mpc"
|
|
|
|
"go.xsfx.dev/schnutibox/pkg/timer"
|
|
|
|
)
|
|
|
|
|
|
|
|
const tickerTime = time.Second
|
|
|
|
|
|
|
|
// Run runs actions after tickerTime is over, over again and again.
|
|
|
|
// Right now its mostly used for setting metrics.
|
|
|
|
func Run() {
|
|
|
|
log.Debug().Msg("starting watch")
|
|
|
|
|
|
|
|
ticker := time.NewTicker(tickerTime)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-ticker.C
|
|
|
|
|
|
|
|
// Timer.
|
2021-08-09 10:49:04 +02:00
|
|
|
go timer.T.Handle()
|
2021-08-05 09:43:35 +02:00
|
|
|
|
|
|
|
// Metrics.
|
|
|
|
go func() {
|
|
|
|
m, err := mpc.Conn()
|
|
|
|
if err != nil {
|
2021-08-09 10:49:04 +02:00
|
|
|
log.Error().Err(err).Msg("could not connect")
|
|
|
|
|
2021-08-05 09:43:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uris, err := mpc.PlaylistURIS()
|
|
|
|
if err != nil {
|
2021-08-09 10:49:04 +02:00
|
|
|
log.Error().Err(err).Msg("could not get playlist uris")
|
2021-08-05 09:43:35 +02:00
|
|
|
metrics.BoxErrors.Inc()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gettings MPD state.
|
|
|
|
s, err := m.Status()
|
|
|
|
if err != nil {
|
2021-08-09 10:49:04 +02:00
|
|
|
log.Error().Err(err).Msg("could not get status")
|
2021-08-05 09:43:35 +02:00
|
|
|
metrics.BoxErrors.Inc()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the metrics.
|
|
|
|
metrics.Set(uris, s["state"])
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|