logmania/notify/all/internal.go

55 lines
984 B
Go
Raw Normal View History

2017-06-16 10:33:35 +02:00
package all
import (
2017-10-25 00:36:16 +02:00
log "github.com/sirupsen/logrus"
"dev.sum7.eu/genofire/logmania/bot"
"dev.sum7.eu/genofire/logmania/database"
"dev.sum7.eu/genofire/logmania/lib"
"dev.sum7.eu/genofire/logmania/notify"
2017-06-16 10:33:35 +02:00
)
type Notifier struct {
notify.Notifier
2017-08-09 08:45:45 +02:00
list []notify.Notifier
channelNotify chan *log.Entry
2017-06-16 10:33:35 +02:00
}
func Init(config *lib.NotifyConfig, db *database.DB, bot *bot.Bot) notify.Notifier {
2017-06-16 10:33:35 +02:00
var list []notify.Notifier
for _, init := range notify.NotifyRegister {
notify := init(config, db, bot)
2017-06-16 10:33:35 +02:00
if notify == nil {
continue
}
list = append(list, notify)
}
2017-08-09 08:45:45 +02:00
n := &Notifier{
list: list,
channelNotify: make(chan *log.Entry),
2017-06-16 10:33:35 +02:00
}
2017-08-09 08:45:45 +02:00
go n.sender()
return n
2017-06-16 10:33:35 +02:00
}
2017-08-09 08:45:45 +02:00
func (n *Notifier) sender() {
for c := range n.channelNotify {
for _, item := range n.list {
item.Send(c)
2017-08-09 08:45:45 +02:00
}
2017-06-16 10:33:35 +02:00
}
}
func (n *Notifier) Send(e *log.Entry) error {
2017-08-09 08:45:45 +02:00
n.channelNotify <- e
2017-10-25 00:36:16 +02:00
return nil
2017-08-09 08:45:45 +02:00
}
2017-06-16 10:33:35 +02:00
func (n *Notifier) Close() {
for _, item := range n.list {
item.Close()
}
}