Fix Golang slog in uptime monitor #13
This commit is contained in:
1 parent
adb6e54c9c
commit
fb7a84cf4c
2 files changed
+36
-21
No files matched your search
+23
-15
@@ -1,19 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
import (
|
||||
"frigate/uptime/ping"
|
||||
"frigate/uptime/notify"
|
||||
"frigate/uptime/ping"
|
||||
)
|
||||
|
||||
|
||||
type CameraMonitor struct {
|
||||
pingInterval time.Duration
|
||||
pingTimeout time.Duration
|
||||
pingInterval time.Duration
|
||||
pingTimeout time.Duration
|
||||
consecutiveDownThreshold int
|
||||
downtime map[string]int
|
||||
notify.Notifier
|
||||
@@ -21,23 +22,23 @@ type CameraMonitor struct {
|
||||
|
||||
func NewCameraMonitor(pingInterval time.Duration, pingTimeout time.Duration, consecutiveDownThreshold int, notifier notify.Notifier) CameraMonitor {
|
||||
return CameraMonitor{
|
||||
pingInterval: pingInterval,
|
||||
pingTimeout: pingTimeout,
|
||||
pingInterval: pingInterval,
|
||||
pingTimeout: pingTimeout,
|
||||
consecutiveDownThreshold: consecutiveDownThreshold,
|
||||
downtime: make(map[string]int),
|
||||
Notifier: notifier,
|
||||
downtime: make(map[string]int),
|
||||
Notifier: notifier,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c CameraMonitor) onCameraUp(camera string) {
|
||||
c.SendNotification(camera, true)
|
||||
slog.Info(camera, "camera is back online!")
|
||||
slog.Info(fmt.Sprintf("%s camera is back online!", camera))
|
||||
}
|
||||
|
||||
func (c CameraMonitor) onCameraDown(camera string) {
|
||||
c.SendNotification(camera, false)
|
||||
slog.Info(camera, "camera is offline!")
|
||||
slog.Info(fmt.Sprintf("%s camera is offline!", camera))
|
||||
}
|
||||
|
||||
func (c CameraMonitor) onCameraPingResult(camera string, online bool) {
|
||||
@@ -57,7 +58,7 @@ func (c CameraMonitor) onCameraPingResult(camera string, online bool) {
|
||||
func (c CameraMonitor) Run(cameras map[string]string) {
|
||||
type pingResult struct {
|
||||
camera string
|
||||
online bool
|
||||
online bool
|
||||
}
|
||||
|
||||
var pingResultChannel = make(chan pingResult, 4)
|
||||
@@ -72,7 +73,7 @@ func (c CameraMonitor) Run(cameras map[string]string) {
|
||||
go func() {
|
||||
pingResultChannel <- pingResult{
|
||||
camera: camera,
|
||||
online: ping.VideoPing(host),
|
||||
online: ping.VideoPing(host),
|
||||
}
|
||||
}()
|
||||
unknownPingResultCameras[camera] = true
|
||||
@@ -83,11 +84,18 @@ func (c CameraMonitor) Run(cameras map[string]string) {
|
||||
for range cameras {
|
||||
select {
|
||||
case cameraPingResult := <-pingResultChannel:
|
||||
slog.Debug(cameraPingResult.camera, "camera:", cameraPingResult.online)
|
||||
slog.Debug(cameraPingResult.camera, "online", cameraPingResult.online)
|
||||
c.onCameraPingResult(cameraPingResult.camera, cameraPingResult.online)
|
||||
delete(unknownPingResultCameras, cameraPingResult.camera) // Maintain set of cameras with unknown ping status
|
||||
case <-timeoutChannel:
|
||||
slog.Info("Timed out waiting for cameras", unknownPingResultCameras)
|
||||
var b strings.Builder
|
||||
for camera := range unknownPingResultCameras {
|
||||
if b.Len() > 0 {
|
||||
b.WriteString(", ")
|
||||
}
|
||||
b.WriteString(camera)
|
||||
}
|
||||
slog.Warn("Timed out waiting for ping result", "cameras", b.String())
|
||||
break timeout
|
||||
}
|
||||
}
|
||||
@@ -98,7 +106,7 @@ func (c CameraMonitor) Run(cameras map[string]string) {
|
||||
}
|
||||
|
||||
var sleepDuration = c.pingInterval - time.Since(startTime)
|
||||
slog.Debug("Sleeping for", sleepDuration)
|
||||
slog.Debug("Sleeping", "seconds", sleepDuration)
|
||||
time.Sleep(sleepDuration)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user