-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(email): email queue initialization issue (#374)
Signed-off-by: qwqcode <[email protected]>
- Loading branch information
Showing
3 changed files
with
45 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters