command responses

This commit is contained in:
boyska 2018-11-11 19:10:53 +01:00
parent 3f216f12f8
commit 7c17c971a0
3 changed files with 16 additions and 2 deletions

View file

@ -13,6 +13,7 @@ import (
"gopkg.in/mcuadros/go-syslog.v2/format" "gopkg.in/mcuadros/go-syslog.v2/format"
) )
// TODO: return a server
func setupHTTP(hub circolog.Hub) { func setupHTTP(hub circolog.Hub) {
http.HandleFunc("/", getHTTPHandler(hub)) http.HandleFunc("/", getHTTPHandler(hub))
http.HandleFunc("/ws", getWSHandler(hub)) http.HandleFunc("/ws", getWSHandler(hub))

View file

@ -89,12 +89,18 @@ func main() {
select { select {
case sig := <-interrupt: case sig := <-interrupt:
if sig == syscall.SIGHUP { if sig == syscall.SIGHUP {
log.Println("Clearing queue")
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear} hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear}
<-hub.Responses
log.Println("Queue cleared")
} }
if sig == syscall.SIGUSR1 { if sig == syscall.SIGUSR1 {
log.Println("Pause/resume")
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle} hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle}
resp := <-hub.Responses
if resp.Value.(bool) {
log.Println("resumed")
} else {
log.Println("paused")
}
} }
if sig == syscall.SIGTERM || sig == syscall.SIGINT { if sig == syscall.SIGTERM || sig == syscall.SIGINT {
log.Println("Quitting because of signal", sig) log.Println("Quitting because of signal", sig)

7
hub.go
View file

@ -37,12 +37,16 @@ const (
type HubFullCommand struct { type HubFullCommand struct {
Command HubCommand Command HubCommand
} }
type CommandResponse struct {
Value interface{}
}
type Hub struct { type Hub struct {
Register chan Client Register chan Client
Unregister chan Client Unregister chan Client
LogMessages chan format.LogParts LogMessages chan format.LogParts
Commands chan HubFullCommand Commands chan HubFullCommand
Responses chan CommandResponse
clients map[Client]bool clients map[Client]bool
circbuf *ring.Ring circbuf *ring.Ring
@ -55,6 +59,7 @@ func NewHub(ringBufSize int) Hub {
Unregister: make(chan Client), Unregister: make(chan Client),
LogMessages: make(chan format.LogParts), LogMessages: make(chan format.LogParts),
Commands: make(chan HubFullCommand), Commands: make(chan HubFullCommand),
Responses: make(chan CommandResponse),
circbuf: ring.New(ringBufSize), circbuf: ring.New(ringBufSize),
} }
} }
@ -120,9 +125,11 @@ func (h *Hub) Run() {
case cmd := <-h.Commands: case cmd := <-h.Commands:
if cmd.Command == CommandClear { if cmd.Command == CommandClear {
h.clear() h.clear()
h.Responses <- CommandResponse{Value: true}
} }
if cmd.Command == CommandPauseToggle { if cmd.Command == CommandPauseToggle {
active = !active active = !active
h.Responses <- CommandResponse{Value: active}
} }
} }
} }