Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63ef663

Browse files
committed
定时邮件改进:改成同步发送
1 parent 8fba57e commit 63ef663

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

websites/code/studygolang/src/server/studygolang/background.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func ServeBackGround() {
3737
c.AddFunc("@daily", service.GenSitemap)
3838

3939
// 给用户发邮件,如通知网站最近的动态,每周的晨读汇总等
40-
c.AddFunc("0 0 9 * * 1", service.EmailNotice)
40+
c.AddFunc("0 0 4 * * 1", service.EmailNotice)
4141

4242
c.Start()
4343
}

websites/code/studygolang/src/service/mail.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
package service
88

99
import (
10+
"bytes"
1011
"html/template"
1112
"net/smtp"
1213
"strings"
1314
"time"
1415

15-
"bytes"
1616
. "config"
1717
"logger"
1818
"model"
@@ -92,11 +92,12 @@ func EmailNotice() {
9292

9393
var (
9494
lastUid = 0
95+
limit = "500"
9596
users []*model.User
9697
)
9798

9899
for {
99-
users, err = userModel.Where("uid>?", lastUid).Order("uid ASC").FindAll()
100+
users, err = userModel.Where("uid>?", lastUid).Order("uid ASC").Limit(limit).FindAll()
100101
if err != nil {
101102
logger.Errorln("find user error:", err)
102103
continue
@@ -121,9 +122,11 @@ func EmailNotice() {
121122
continue
122123
}
123124

124-
go func(content, email string) {
125-
SendMail("每周精选", content, []string{email})
126-
}(content, user.Email)
125+
SendMail("每周精选", content, []string{user.Email})
126+
127+
if lastUid < user.Uid {
128+
lastUid = user.Uid
129+
}
127130
}
128131
}
129132

0 commit comments

Comments
 (0)