logmania/notify/all/internal.go

77 lines
1.5 KiB
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
)
2018-04-26 21:05:27 +02:00
var logger = log.WithField("notify", "all")
2017-06-16 10:33:35 +02:00
type Notifier struct {
notify.Notifier
2017-08-09 08:45:45 +02:00
list []notify.Notifier
2018-04-26 21:05:27 +02:00
db *database.DB
2017-08-09 08:45:45 +02:00
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
var defaults []*database.Notify
2017-06-16 10:33:35 +02:00
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)
def := notify.Default()
if def != nil {
continue
}
defaults = append(defaults, def...)
2017-06-16 10:33:35 +02:00
}
2017-08-09 08:45:45 +02:00
db.DefaultNotify = defaults
2017-08-09 08:45:45 +02:00
n := &Notifier{
2018-04-26 21:05:27 +02:00
db: db,
2017-08-09 08:45:45 +02:00
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 {
2018-04-26 21:05:27 +02:00
e, _, tos := n.db.SendTo(c)
for _, to := range tos {
send := false
for _, item := range n.list {
send = item.Send(e, to)
if send {
break
}
}
if !send {
logger.Warnf("notify not send to %s: [%s] %s", to.Address(), c.Level.String(), c.Message)
2018-04-26 21:05:27 +02:00
}
2017-08-09 08:45:45 +02:00
}
2017-06-16 10:33:35 +02:00
}
}
2018-04-26 21:05:27 +02:00
func (n *Notifier) Send(e *log.Entry, to *database.Notify) bool {
2017-08-09 08:45:45 +02:00
n.channelNotify <- e
2018-04-26 21:05:27 +02:00
return true
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()
}
}