mirror of https://github.com/status-im/consul.git
consul: Adding user event handler for callbacks
This commit is contained in:
parent
1c341e6766
commit
be78e355de
|
@ -300,14 +300,22 @@ func (c *Client) localEvent(event serf.UserEvent) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch event.Name {
|
switch name := event.Name; {
|
||||||
case newLeaderEvent:
|
case name == newLeaderEvent:
|
||||||
c.logger.Printf("[INFO] consul: New leader elected: %s", event.Payload)
|
c.logger.Printf("[INFO] consul: New leader elected: %s", event.Payload)
|
||||||
|
|
||||||
// Trigger the callback
|
// Trigger the callback
|
||||||
if c.config.ServerUp != nil {
|
if c.config.ServerUp != nil {
|
||||||
c.config.ServerUp()
|
c.config.ServerUp()
|
||||||
}
|
}
|
||||||
|
case isUserEvent(name):
|
||||||
|
event.Name = rawUserEventName(name)
|
||||||
|
c.logger.Printf("[DEBUG] consul: user event: %s", event.Name)
|
||||||
|
|
||||||
|
// Trigger the callback
|
||||||
|
if c.config.UserEventHandler != nil {
|
||||||
|
c.config.UserEventHandler(event)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
c.logger.Printf("[WARN] consul: Unhandled local event: %v", event)
|
c.logger.Printf("[WARN] consul: Unhandled local event: %v", event)
|
||||||
}
|
}
|
||||||
|
|
|
@ -163,6 +163,10 @@ type Config struct {
|
||||||
// ServerUp callback can be used to trigger a notification that
|
// ServerUp callback can be used to trigger a notification that
|
||||||
// a Consul server is now up and known about.
|
// a Consul server is now up and known about.
|
||||||
ServerUp func()
|
ServerUp func()
|
||||||
|
|
||||||
|
// UserEventHandler callback can be used to handle incoming
|
||||||
|
// user events. This function should not block.
|
||||||
|
UserEventHandler func(serf.UserEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckVersion is used to check if the ProtocolVersion is valid
|
// CheckVersion is used to check if the ProtocolVersion is valid
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package consul
|
package consul
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -12,11 +11,24 @@ const (
|
||||||
// StatusReap is used to update the status of a node if we
|
// StatusReap is used to update the status of a node if we
|
||||||
// are handling a EventMemberReap
|
// are handling a EventMemberReap
|
||||||
StatusReap = serf.MemberStatus(-1)
|
StatusReap = serf.MemberStatus(-1)
|
||||||
|
|
||||||
|
// userEventPrefix is pre-pended to a user event to distinguish it
|
||||||
|
userEventPrefix = "consul:event:"
|
||||||
)
|
)
|
||||||
|
|
||||||
// userEventName computes the name of a user event
|
// userEventName computes the name of a user event
|
||||||
func userEventName(name string) string {
|
func userEventName(name string) string {
|
||||||
return fmt.Sprintf("consul:event:%s")
|
return userEventPrefix + name
|
||||||
|
}
|
||||||
|
|
||||||
|
// isUserEvent checks if a serf event is a user event
|
||||||
|
func isUserEvent(name string) bool {
|
||||||
|
return strings.HasPrefix(name, userEventPrefix)
|
||||||
|
}
|
||||||
|
|
||||||
|
// rawUserEventName is used to get the raw user event name
|
||||||
|
func rawUserEventName(name string) string {
|
||||||
|
return strings.TrimPrefix(name, userEventPrefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
// lanEventHandler is used to handle events from the lan Serf cluster
|
// lanEventHandler is used to handle events from the lan Serf cluster
|
||||||
|
@ -108,14 +120,22 @@ func (s *Server) localEvent(event serf.UserEvent) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch event.Name {
|
switch name := event.Name; {
|
||||||
case newLeaderEvent:
|
case name == newLeaderEvent:
|
||||||
s.logger.Printf("[INFO] consul: New leader elected: %s", event.Payload)
|
s.logger.Printf("[INFO] consul: New leader elected: %s", event.Payload)
|
||||||
|
|
||||||
// Trigger the callback
|
// Trigger the callback
|
||||||
if s.config.ServerUp != nil {
|
if s.config.ServerUp != nil {
|
||||||
s.config.ServerUp()
|
s.config.ServerUp()
|
||||||
}
|
}
|
||||||
|
case isUserEvent(name):
|
||||||
|
event.Name = rawUserEventName(name)
|
||||||
|
s.logger.Printf("[DEBUG] consul: user event: %s", event.Name)
|
||||||
|
|
||||||
|
// Trigger the callback
|
||||||
|
if s.config.UserEventHandler != nil {
|
||||||
|
s.config.UserEventHandler(event)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
s.logger.Printf("[WARN] consul: Unhandled local event: %v", event)
|
s.logger.Printf("[WARN] consul: Unhandled local event: %v", event)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue