forked from goffee/goffee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.go
80 lines (63 loc) · 1.57 KB
/
notifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package notifier
import (
"fmt"
"strconv"
"time"
"github.com/gophergala/goffee/Godeps/_workspace/src/github.com/keighl/mandrill"
"github.com/gophergala/goffee/data"
"github.com/gophergala/goffee/queue"
)
var (
exit = make(chan bool)
MandrillKey string
mandrillClient *mandrill.Client
)
func Run() {
go run()
}
func run() {
mandrillClient = mandrill.ClientWithKey(MandrillKey)
for {
notifications := queue.FetchNotifications()
for _, n := range notifications {
checkId, err := strconv.ParseInt(n, 10, 64)
check, err := data.FindCheck(checkId)
if err != nil {
continue
}
user, err := check.User()
if err != nil {
continue
}
sendMessage(check, user)
}
}
}
func sendMessage(c data.Check, u data.User) {
println("Notifying via email: " + u.Email)
var subject string
if c.Success {
subject = fmt.Sprintf("Up: %s (%d)", c.URL, c.Status)
} else {
subject = fmt.Sprintf("Down: %s (%d)", c.URL, c.Status)
}
html := `<strong>%s</strong>
<br>
<br>
<p>Checked at %s by <a href='http://goffee.io/'>Goffee.io</a></p>`
html = fmt.Sprintf(html, subject, c.UpdatedAt.Format(time.Kitchen))
text := `%s\n\nChecked at %s by Goffee.io`
text = fmt.Sprintf(text, subject, c.UpdatedAt.Format(time.Kitchen))
message := &mandrill.Message{}
message.AddRecipient(u.Email, u.Name, "to")
message.FromEmail = "no-reply@goffee.io"
message.FromName = "Goffee Notifier"
message.Subject = subject
message.HTML = html
message.Text = text
message.Subaccount = "goffee"
mandrillClient.MessagesSend(message)
}
func Wait() {
<-exit
}