2021-02-14 09:26:56 -09:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.binarythought.com/cdramey/alrm/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type worker struct {
|
2021-02-14 14:22:30 -09:00
|
|
|
wakec chan bool
|
|
|
|
shutdownc chan bool
|
|
|
|
group *config.Group
|
|
|
|
debuglvl int
|
2021-02-14 09:26:56 -09:00
|
|
|
}
|
|
|
|
|
2021-02-14 14:22:30 -09:00
|
|
|
func (w *worker) start() {
|
2021-02-14 09:26:56 -09:00
|
|
|
for {
|
2021-02-14 14:22:30 -09:00
|
|
|
if w.debuglvl > 2 {
|
2021-02-14 09:26:56 -09:00
|
|
|
fmt.Printf("%s worker waiting.. \n", w.group.Name)
|
|
|
|
}
|
2021-02-14 14:22:30 -09:00
|
|
|
<-w.wakec
|
|
|
|
if w.debuglvl > 2 {
|
2021-02-14 09:26:56 -09:00
|
|
|
fmt.Printf("%s worker wake.. \n", w.group.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range w.group.Hosts {
|
|
|
|
for _, c := range h.Checks {
|
2021-02-14 14:22:30 -09:00
|
|
|
err := c.Check(w.debuglvl)
|
2021-02-14 09:26:56 -09:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("check error: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 14:22:30 -09:00
|
|
|
|
|
|
|
// Wake this worker with a non-blocking push
|
|
|
|
// into the channel
|
|
|
|
func (w *worker) wake() {
|
|
|
|
select {
|
|
|
|
case w.wakec <- true:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown this worker with a non-blocking push
|
|
|
|
// into the channel
|
|
|
|
func (w *worker) shutdown() {
|
|
|
|
select {
|
|
|
|
case w.shutdownc <- true:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeworker(g *config.Group, d int) *worker {
|
|
|
|
return &worker{
|
2021-02-14 16:55:56 -09:00
|
|
|
group: g,
|
|
|
|
debuglvl: d,
|
2021-02-14 14:22:30 -09:00
|
|
|
// This channel is unbuffered so that checks that take
|
|
|
|
// over the set interval don't backlog
|
2021-02-14 16:55:56 -09:00
|
|
|
wakec: make(chan bool),
|
2021-02-14 14:22:30 -09:00
|
|
|
// This channel is buffered because we want it to remember
|
|
|
|
// an order to shutdown
|
|
|
|
shutdownc: make(chan bool, 1),
|
|
|
|
}
|
|
|
|
}
|