2018-09-16 16:18:30 +08:00
|
|
|
package notification
|
2018-08-13 16:20:49 +08:00
|
|
|
|
|
|
|
import (
|
2019-09-02 18:28:40 +08:00
|
|
|
"github.com/joeslay/seaweedfs/weed/glog"
|
|
|
|
"github.com/joeslay/seaweedfs/weed/util"
|
2018-09-16 16:37:35 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2018-09-21 16:56:43 +08:00
|
|
|
"github.com/spf13/viper"
|
2018-08-13 16:20:49 +08:00
|
|
|
)
|
|
|
|
|
2018-09-16 16:37:35 +08:00
|
|
|
type MessageQueue interface {
|
|
|
|
// GetName gets the name to locate the configuration in filer.toml file
|
|
|
|
GetName() string
|
|
|
|
// Initialize initializes the file store
|
|
|
|
Initialize(configuration util.Configuration) error
|
|
|
|
SendMessage(key string, message proto.Message) error
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:20:49 +08:00
|
|
|
var (
|
|
|
|
MessageQueues []MessageQueue
|
|
|
|
|
|
|
|
Queue MessageQueue
|
|
|
|
)
|
|
|
|
|
2018-08-20 06:17:55 +08:00
|
|
|
func LoadConfiguration(config *viper.Viper) {
|
2018-08-13 16:20:49 +08:00
|
|
|
|
2018-08-23 14:54:00 +08:00
|
|
|
if config == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-06 16:44:41 +08:00
|
|
|
validateOneEnabledQueue(config)
|
2018-12-06 16:37:59 +08:00
|
|
|
|
|
|
|
for _, queue := range MessageQueues {
|
|
|
|
if config.GetBool(queue.GetName() + ".enabled") {
|
|
|
|
viperSub := config.Sub(queue.GetName())
|
|
|
|
if err := queue.Initialize(viperSub); err != nil {
|
2018-12-06 15:24:25 +08:00
|
|
|
glog.Fatalf("Failed to initialize notification for %s: %+v",
|
2018-12-06 16:37:59 +08:00
|
|
|
queue.GetName(), err)
|
2018-08-13 16:20:49 +08:00
|
|
|
}
|
2018-12-06 16:37:59 +08:00
|
|
|
Queue = queue
|
|
|
|
glog.V(0).Infof("Configure notification message queue for %s", queue.GetName())
|
2018-08-13 16:20:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-12-06 16:44:41 +08:00
|
|
|
|
|
|
|
func validateOneEnabledQueue(config *viper.Viper) {
|
|
|
|
enabledQueue := ""
|
|
|
|
for _, queue := range MessageQueues {
|
|
|
|
if config.GetBool(queue.GetName() + ".enabled") {
|
|
|
|
if enabledQueue == "" {
|
|
|
|
enabledQueue = queue.GetName()
|
|
|
|
} else {
|
|
|
|
glog.Fatalf("Notification message queue is enabled for both %s and %s", enabledQueue, queue.GetName())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|