Skip to content

Commit

Permalink
fix(email): email queue initialization issue (#374)
Browse files Browse the repository at this point in the history
Signed-off-by: qwqcode <[email protected]>
  • Loading branch information
qwqcode authored Feb 4, 2023
1 parent 55ed0a6 commit 151b52f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ type EmailConf struct {
MailTpl string `koanf:"mail_tpl" json:"mail_tpl"` // 邮件模板
SMTP SMTPConf `koanf:"smtp" json:"smtp"` // SMTP 配置
AliDM AliDMConf `koanf:"ali_dm" json:"ali_dm"` // 阿里云邮件配置
Queue EmailQueueConf `koanf:"queue" json:"queue"` // 邮件发送队列配置
}

type SMTPConf struct {
Expand All @@ -169,6 +170,10 @@ type AliDMConf struct {
Region string `koanf:"region" json:"region"`
}

type EmailQueueConf struct {
BufferSize int `koanf:"buffer_size" json:"buffer_size"` // Channel buffer size (default is zero that not create buffer)
}

type DBType string

const (
Expand Down
53 changes: 39 additions & 14 deletions internal/email/queue.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,64 @@
package email

import (
"encoding/json"
"sync"

"github.com/ArtalkJS/Artalk/internal/config"
"github.com/ArtalkJS/Artalk/internal/query"
"github.com/sirupsen/logrus"
)

// Email Queue
var emailCh chan Email
var emailMutex sync.Mutex

// Initialize Email Queue
func InitQueue(emailConf *config.EmailConf) {
emailMutex.Lock()
defer emailMutex.Unlock()

func InitQueue() {
// email queue only need init once
if emailCh != nil {
emailCh = make(chan Email) // TODO: add size limit
return
}

// init email queue
emailCh = make(chan Email, emailConf.Queue.BufferSize)

if config.Instance.Debug {
logrus.Debug("[Email] Email Queue initialize complete")
}

go func() {
for {
select {
case email := <-emailCh:
sender := NewSender(config.Instance.Email.SendType)

if sender.Send(email) { // 发送成功
if email.LinkedNotify != nil {
// 标记关联评论邮件发送状态
if err := query.NotifySetEmailed(email.LinkedNotify); err != nil {
logrus.Errorf("[Email] Flag associated comment email delivery status failed: %s", err)
continue
}
}
email := <-emailCh
sender := NewSender(emailConf.SendType)

if config.Instance.Debug {
emailJson, _ := json.Marshal(email)
logrus.Debug("[Email] Sending an email: ", string(emailJson))
}

// send email
isOK := sender.Send(email)

if !isOK {
logrus.Errorf("[Email] Failed send email to addr: %s", email.ToAddr)
continue
}

// send success
if email.LinkedNotify != nil {
if err := query.NotifySetEmailed(email.LinkedNotify); err != nil { // flag associated comment as emailed
logrus.Errorf("[Email] Flag email delivery status for associated comment failed: %s", err)
}
}
}
}()
}

// Add an email to the sending queue
func AddToQueue(email Email) {
emailCh <- email
}
2 changes: 1 addition & 1 deletion internal/notify_launcher/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var NotifyCtx = context.Background()

func Init() {
// 初始化邮件队列
email.InitQueue()
email.InitQueue(&config.Instance.Email)

// 初始化 Notify
Notify = notify.New()
Expand Down

0 comments on commit 151b52f

Please sign in to comment.