2021-02-14 09:26:56 -09:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.binarythought.com/cdramey/alrm/config"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
workers []*worker
|
|
|
|
cfg *config.Config
|
|
|
|
debuglvl int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *Server) Start() {
|
|
|
|
for _, w := range srv.workers {
|
2021-02-14 14:22:30 -09:00
|
|
|
go w.start()
|
2021-02-14 09:26:56 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-02-14 14:22:30 -09:00
|
|
|
w.wake()
|
2021-02-14 09:26:56 -09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(cfg *config.Config, debuglvl int) *Server {
|
|
|
|
srv := &Server{cfg: cfg, debuglvl: debuglvl}
|
|
|
|
for _, g := range cfg.Groups {
|
2021-02-14 14:22:30 -09:00
|
|
|
srv.workers = append(srv.workers, makeworker(g, debuglvl))
|
2021-02-14 09:26:56 -09:00
|
|
|
}
|
|
|
|
return srv
|
|
|
|
}
|