aboutsummaryrefslogtreecommitdiff
path: root/service.go
diff options
context:
space:
mode:
Diffstat (limited to 'service.go')
-rw-r--r--service.go39
1 files changed, 35 insertions, 4 deletions
diff --git a/service.go b/service.go
index 69e68a3..e9c412c 100644
--- a/service.go
+++ b/service.go
@@ -13,12 +13,19 @@ import (
"git.sr.ht/~shulhan/lilin/internal"
)
+// errorCountNotif define the number of consecutive error to trigger
+// notification.
+const errorCountNotif = 3
+
type Service struct {
httpConn *http.Client
dialer *net.Dialer
ticker *time.Ticker
cfg ServiceConfig
- isReady bool
+
+ errorCount int
+
+ isReady bool
}
// NewService create and initialize the connection to service.
@@ -81,10 +88,12 @@ func (svc *Service) Scan() (report ScanReport) {
}
// Start scanning periodically and send the report to reportq.
-func (svc *Service) Start(reportq chan<- ScanReport) {
+func (svc *Service) Start(reportq, notifq chan<- ScanReport) {
svc.ticker = time.NewTicker(svc.cfg.interval)
- reportq <- svc.Scan()
+ var report = svc.Scan()
+ svc.publishNotif(notifq, report)
+ reportq <- report
for {
_, ok := <-svc.ticker.C
@@ -92,7 +101,9 @@ func (svc *Service) Start(reportq chan<- ScanReport) {
log.Printf("Service: %s not ok", svc.cfg.ID)
return
}
- reportq <- svc.Scan()
+ report = svc.Scan()
+ svc.publishNotif(notifq, report)
+ reportq <- report
}
}
@@ -116,3 +127,23 @@ func (svc *Service) connect() (err error) {
svc.isReady = true
return nil
}
+
+func (svc *Service) publishNotif(notifq chan<- ScanReport, report ScanReport) {
+ if report.Success {
+ if svc.errorCount >= errorCountNotif {
+ // Trigger the UP notification.
+ select {
+ case notifq <- report:
+ }
+ }
+ svc.errorCount = 0
+ } else {
+ svc.errorCount++
+ if svc.errorCount >= errorCountNotif {
+ // Trigger the DOWN notification.
+ select {
+ case notifq <- report:
+ }
+ }
+ }
+}