forked from open-falcon-archive/sender
/
mail.go
54 lines (46 loc) · 958 Bytes
/
mail.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
package cron
import (
"github.com/Cepave/sender/g"
"github.com/Cepave/sender/model"
"github.com/Cepave/sender/proc"
"github.com/Cepave/sender/redis"
"github.com/toolkits/net/httplib"
"log"
"time"
)
func ConsumeMail() {
queue := g.Config().Queue.Mail
for {
L := redis.PopAllMail(queue)
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, 2*time.Minute)
r.Param("tos", mail.Tos)
r.Param("subject", mail.Subject)
r.Param("content", mail.Content)
resp, err := r.String()
if err != nil {
log.Println(err)
}
proc.IncreMailCount()
if g.Config().Debug {
log.Println("==mail==>>>>", mail)
log.Println("<<<<==mail==", resp)
}
}