github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/monitor/notifier.go (about) 1 package monitor 2 3 import ( 4 "github.com/evergreen-ci/evergreen" 5 "github.com/evergreen-ci/evergreen/notify" 6 "github.com/mongodb/grip" 7 "github.com/pkg/errors" 8 ) 9 10 type Notifier struct { 11 // functions which will be called to create any notifications that need 12 // to be sent 13 notificationBuilders []notificationBuilder 14 } 15 16 // create and send any notifications that need to be sent 17 func (self *Notifier) Notify(settings *evergreen.Settings) []error { 18 grip.Info("Building and sending necessary notifications...") 19 20 // used to store any errors that occur 21 var errs []error 22 23 for _, f := range self.notificationBuilders { 24 25 // get the necessary notifications 26 notifications, err := f(settings) 27 28 // continue on error so that one wonky function doesn't stop the others 29 // from running 30 if err != nil { 31 errs = append(errs, errors.Wrap(err, 32 "error building notifications to be sent")) 33 continue 34 } 35 36 // send the actual notifications. continue on error to allow further 37 // notifications to be sent 38 if newErrs := sendNotifications(notifications, settings); errs != nil { 39 for _, err := range newErrs { 40 errs = append(errs, errors.Wrap(err, 41 "error sending notifications")) 42 } 43 continue 44 } 45 46 } 47 48 grip.Info("Done building and sending notifications") 49 50 return errs 51 } 52 53 // send all of the specified notifications, and execute the callbacks for any 54 // that are successfully sent. returns an aggregate list of any errors 55 // that occur 56 func sendNotifications(notifications []notification, settings *evergreen.Settings) []error { 57 58 grip.Infof("Sending %d notifications...", len(notifications)) 59 60 // used to store any errors that occur 61 var errs []error 62 63 // ask for the mailer we'll use 64 mailer := notify.ConstructMailer(settings.Notify) 65 66 for _, n := range notifications { 67 68 // send the notification 69 err := notify.TrySendNotificationToUser( 70 n.recipient, 71 n.subject, 72 n.message, 73 mailer, 74 ) 75 76 // continue on error to allow further notifications to be sent 77 if err != nil { 78 errs = append(errs, errors.Wrapf(err, 79 "error sending notification to %s", n.recipient)) 80 continue 81 } 82 83 // run the notification's callback, since it has been successfully sent 84 if n.callback != nil { 85 if err := n.callback(n.host, n.threshold); err != nil { 86 errs = append(errs, errors.Wrap(err, 87 "error running notification callback")) 88 } 89 } 90 91 } 92 93 return errs 94 }