improve notifier performance

master
Norwin 5 years ago
parent 331852b56c
commit 43aeb83069

@ -53,11 +53,14 @@ func (results BoxCheckResults) filterChangedFromCache() BoxCheckResults {
return remaining
}
func updateCache(box *Box, results []CheckResult) error {
func updateCache(box *Box, results []CheckResult) {
for _, result := range results {
key := fmt.Sprintf("watchcache.%s.%s", box.Id, result.EventID())
cache.Set(key+".laststatus", result.Status)
}
}
func writeCache() error {
return cache.WriteConfig()
}

@ -8,6 +8,8 @@ import (
"github.com/spf13/viper"
)
var client = &xmpp.Client{} // @Hacky
// box config required for the XmppNotifier (TransportConfig.Options)
type XmppNotifier struct {
Recipients []string
@ -23,6 +25,16 @@ func (n XmppNotifier) New(config TransportConfig) (AbstractNotifier, error) {
}
}
// establish connection with server once, and share it accross instances
// @Hacky
if client.JID() == "" {
c, err := connectXmpp()
if err != nil {
return nil, err
}
client = c
}
// assign configuration to the notifier after ensuring the correct type.
// lesson of this project: golang requires us to fuck around with type
// assertions, instead of providing us with proper inheritance.
@ -53,26 +65,12 @@ func (n XmppNotifier) New(config TransportConfig) (AbstractNotifier, error) {
}
func (n XmppNotifier) Submit(notification Notification) error {
// :TransportConfSourceHack
xmppOpts := xmpp.Options{
Host: viper.GetString("xmpp.host"),
User: viper.GetString("xmpp.user"),
Password: viper.GetString("xmpp.pass"),
Resource: "osem_notify",
}
if viper.GetBool("xmpp.starttls") {
xmppOpts.NoTLS = true
xmppOpts.StartTLS = true
}
client, err := xmppOpts.NewClient()
if err != nil {
return err
if client.JID() == "" {
return fmt.Errorf("xmpp client not correctly initialized!")
}
for _, recipient := range n.Recipients {
_, err = client.Send(xmpp.Chat{
_, err := client.Send(xmpp.Chat{
Remote: recipient,
Subject: notification.Subject,
Text: fmt.Sprintf("%s\n\n%s", notification.Subject, notification.Body),
@ -83,5 +81,22 @@ func (n XmppNotifier) Submit(notification Notification) error {
}
}
return err
return nil
}
func connectXmpp() (*xmpp.Client, error) {
// :TransportConfSourceHack
xmppOpts := xmpp.Options{
Host: viper.GetString("xmpp.host"),
User: viper.GetString("xmpp.user"),
Password: viper.GetString("xmpp.pass"),
Resource: "osem_notify",
}
if viper.GetBool("xmpp.starttls") {
xmppOpts.NoTLS = true
xmppOpts.StartTLS = true
}
return xmppOpts.NewClient()
}

@ -98,11 +98,7 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache
// update cache (with /all/ changed results to reset status)
if useCache {
notifyLog.Debug("updating cache")
cacheError := updateCache(box, resultsBox)
if cacheError != nil {
notifyLog.Error("could not cache notification results: ", cacheError)
errs = append(errs, cacheError.Error())
}
updateCache(box, resultsBox)
}
if len(resultsDue) != 0 {
@ -110,6 +106,15 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache
}
}
// persist changes to cache
if useCache {
err := writeCache()
if err != nil {
log.Error("could not write cache of notification results: ", err)
errs = append(errs, err.Error())
}
}
if len(errs) != 0 {
return fmt.Errorf(strings.Join(errs, "\n"))
}
@ -147,4 +152,3 @@ func ComposeNotification(box *Box, checks []CheckResult) Notification {
time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id),
}
}

Loading…
Cancel
Save