forked from ixre/go2o
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail_queue.go
52 lines (48 loc) · 1.12 KB
/
mail_queue.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
/**
* Copyright 2015 @ z3q.net.
* name : mail_queue
* author : jarryliu
* date : 2015-07-27 17:06
* description :
* history :
*/
package daemon
import (
"github.com/jsix/gof"
"go2o/src/core/domain/interface/enum"
"go2o/src/core/domain/interface/partner/mss"
mssIns "go2o/src/core/infrastructure/mss"
"go2o/src/core/variable"
"time"
)
var (
mailChan chan int
)
func startMailQueue(app gof.App) {
if i, _ := appCtx.Storage().GetInt(variable.KvNewMailTask); i == enum.FALSE {
sendQueue()
appCtx.Storage().Set(variable.KvNewMailTask, enum.TRUE)
}
}
func sendQueue() {
var list = []*mss.MailTask{}
appCtx.Db().GetOrm().Select(&list, "is_send = 0 OR is_failed = 1")
mailChan = make(chan int, len(list))
for _, v := range list {
go func(ch chan int, t *mss.MailTask) {
err := mssIns.SendMailWithDefaultConfig(t.Subject, []string{t.SendTo}, []byte(t.Body))
if err != nil {
appCtx.Log().PrintErr(err)
t.IsFailed = 1
t.IsSend = 1
} else {
t.IsSend = 1
t.IsFailed = 0
}
t.SendTime = time.Now().Unix()
appCtx.Db().GetOrm().Save(t.Id, t)
mailChan <- 0
}(mailChan, v)
<-mailChan
}
}