forked from open-falcon/falcon-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail_sender.go
46 lines (40 loc) · 1 KB
/
mail_sender.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
package cron
import (
log "github.com/Sirupsen/logrus"
"github.com/open-falcon/falcon-plus/modules/alarm/g"
"github.com/open-falcon/falcon-plus/modules/alarm/model"
"github.com/open-falcon/falcon-plus/modules/alarm/redi"
"github.com/toolkits/net/httplib"
"time"
)
func ConsumeMail() {
for {
L := redi.PopAllMail()
if len(L) == 0 {
time.Sleep(time.Millisecond * 200)
continue
}
SendMailList(L)
}
}
func SendMailList(L []*model.Mail) {
for _, mail := range L {
MailWorkerChan <- 1
go SendMail(mail)
}
}
func SendMail(mail *model.Mail) {
defer func() {
<-MailWorkerChan
}()
url := g.Config().Api.Mail
r := httplib.Post(url).SetTimeout(5*time.Second, 30*time.Second)
r.Param("tos", mail.Tos)
r.Param("subject", mail.Subject)
r.Param("content", mail.Content)
resp, err := r.String()
if err != nil {
log.Errorf("send mail fail, receiver:%s, subject:%s, cotent:%s, error:%v", mail.Tos, mail.Subject, mail.Content, err)
}
log.Debugf("send mail:%v, resp:%v, url:%s", mail, resp, url)
}