USR1 on circologd pause/resume

This commit is contained in:
boyska 2018-11-10 18:21:42 +01:00
parent a990422953
commit 3f216f12f8
2 changed files with 41 additions and 11 deletions

View file

@ -31,7 +31,7 @@ func main() {
flag.Parse()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM)
signal.Notify(interrupt, syscall.SIGINT, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGTERM)
hub := circolog.NewHub(*bufsize)
handler := syslog.NewChannelHandler(hub.LogMessages)
@ -90,7 +90,11 @@ func main() {
case sig := <-interrupt:
if sig == syscall.SIGHUP {
log.Println("Clearing queue")
hub.Clear()
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandClear}
}
if sig == syscall.SIGUSR1 {
log.Println("Pause/resume")
hub.Commands <- circolog.HubFullCommand{Command: circolog.CommandPauseToggle}
}
if sig == syscall.SIGTERM || sig == syscall.SIGINT {
log.Println("Quitting because of signal", sig)

28
hub.go
View file

@ -24,10 +24,25 @@ type ClientOptions struct {
// The channel "register" and "unregister" can be seen as "command"
// keep in mind that "registering" is what you do also to get messages in a one-time fashion. In fact, Client
// has "options", such as Nofollow, to explain the Hub what should be given
// An HubCommand is an "enum" of different commands
type HubCommand int
const (
CommandClear = iota
CommandPauseToggle = iota
)
// An HubFullCommand is a Command, complete with arguments
type HubFullCommand struct {
Command HubCommand
}
type Hub struct {
Register chan Client
Unregister chan Client
LogMessages chan format.LogParts
Commands chan HubFullCommand
clients map[Client]bool
circbuf *ring.Ring
@ -39,6 +54,7 @@ func NewHub(ringBufSize int) Hub {
Register: make(chan Client),
Unregister: make(chan Client),
LogMessages: make(chan format.LogParts),
Commands: make(chan HubFullCommand),
circbuf: ring.New(ringBufSize),
}
}
@ -77,6 +93,7 @@ func (h *Hub) register(cl Client) {
// Run is hub main loop; keeps everything going
func (h *Hub) Run() {
active := true
for {
select {
case cl := <-h.Register:
@ -88,6 +105,7 @@ func (h *Hub) Run() {
delete(h.clients, cl)
}
case msg := <-h.LogMessages:
if active == true {
h.circbuf.Value = msg
h.circbuf = h.circbuf.Next()
for client := range h.clients {
@ -99,11 +117,19 @@ func (h *Hub) Run() {
}
}
}
case cmd := <-h.Commands:
if cmd.Command == CommandClear {
h.clear()
}
if cmd.Command == CommandPauseToggle {
active = !active
}
}
}
}
// Clear removes every all elements from the buffer
func (h *Hub) Clear() {
func (h *Hub) clear() {
buf := h.circbuf
for i := 0; i < buf.Len(); i++ {
buf.Value = nil