changed server to use channels instead of cond for wakeup, removed config hashing, added config path to config struct
This commit is contained in:
47
server/server.go
Normal file
47
server/server.go
Normal file
@ -0,0 +1,47 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.binarythought.com/cdramey/alrm/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
workers []*worker
|
||||
cfg *config.Config
|
||||
debuglvl int
|
||||
httpsrv http.Server
|
||||
}
|
||||
|
||||
func (srv *Server) Start() {
|
||||
for _, w := range srv.workers {
|
||||
go w.start(srv.debuglvl)
|
||||
}
|
||||
|
||||
t := time.NewTicker(srv.cfg.Interval)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case r := <-t.C:
|
||||
if srv.debuglvl > 0 {
|
||||
fmt.Printf("interval check at %s\n", r)
|
||||
}
|
||||
|
||||
for _, w := range srv.workers {
|
||||
select {
|
||||
case w.wake <- true:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewServer(cfg *config.Config, debuglvl int) *Server {
|
||||
srv := &Server{cfg: cfg, debuglvl: debuglvl}
|
||||
for _, g := range cfg.Groups {
|
||||
w := &worker{group: g, wake: make(chan bool)}
|
||||
srv.workers = append(srv.workers, w)
|
||||
}
|
||||
return srv
|
||||
}
|
32
server/worker.go
Normal file
32
server/worker.go
Normal file
@ -0,0 +1,32 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.binarythought.com/cdramey/alrm/config"
|
||||
)
|
||||
|
||||
type worker struct {
|
||||
wake chan bool
|
||||
group *config.Group
|
||||
}
|
||||
|
||||
func (w *worker) start(debuglvl int) {
|
||||
for {
|
||||
if debuglvl > 2 {
|
||||
fmt.Printf("%s worker waiting.. \n", w.group.Name)
|
||||
}
|
||||
<-w.wake
|
||||
if debuglvl > 2 {
|
||||
fmt.Printf("%s worker wake.. \n", w.group.Name)
|
||||
}
|
||||
|
||||
for _, h := range w.group.Hosts {
|
||||
for _, c := range h.Checks {
|
||||
err := c.Check(debuglvl)
|
||||
if err != nil {
|
||||
fmt.Printf("check error: %s\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user