Disabled rabbitmq auto-ack

master
RuisPipe 2021-08-01 12:27:50 +02:00
parent 9c32e1b9ff
commit 67ba48a3db
3 changed files with 25 additions and 12 deletions

2
go.mod
View File

@ -6,5 +6,5 @@ require (
github.com/BurntSushi/toml v0.3.1 github.com/BurntSushi/toml v0.3.1
github.com/gofiber/fiber/v2 v2.12.0 github.com/gofiber/fiber/v2 v2.12.0
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1
github.com/streadway/amqp v1.0.0 // indirect github.com/streadway/amqp v1.0.0
) )

View File

@ -31,7 +31,7 @@ type Mail struct {
BodyData interface{} BodyData interface{}
} }
func NewMail(to []string, templateId int, languageId int, bodyData interface{}) { func NewMail(to []string, templateId int, languageId int, bodyData interface{}) error {
log.Infoln("new mail", templateId, bodyData) log.Infoln("new mail", templateId, bodyData)
mail := Mail{ mail := Mail{
@ -48,12 +48,17 @@ func NewMail(to []string, templateId int, languageId int, bodyData interface{})
if err != nil { if err != nil {
log.Fatalln("error parsing", err) log.Fatalln("error parsing", err)
return err
} }
mail.Send(body) if err = mail.Send(body); err != nil {
return err
}
return nil
} }
func (m *Mail) Send(body string) { func (m *Mail) Send(body string) error {
msg := "From: " + cfg.Mail.From + "\n" + msg := "From: " + cfg.Mail.From + "\n" +
"To: " + strings.Join(m.To, ",") + "\n" + "To: " + strings.Join(m.To, ",") + "\n" +
"Subject: " + m.Subject + "\n" + "Subject: " + m.Subject + "\n" +
@ -64,8 +69,10 @@ func (m *Mail) Send(body string) {
if err != nil { if err != nil {
log.Warnln("smtp error:", err) log.Warnln("smtp error:", err)
return err
} else { } else {
log.Debugln("send mail") log.Debugln("send mail")
return nil
} }
} }

View File

@ -49,15 +49,19 @@ func Init() {
log.Debug("RabbitMQ connected") log.Debug("RabbitMQ connected")
msgs, err := ch.Consume( msgs, err := ch.Consume(
"mails", "mails", // queue
"", "", // consumer
true, false, // auto-ack
false, false, // exclusive
false, false, // no-local
false, false, // no-wait
nil, nil, // args
) )
if err != nil {
log.Fatalln("Consume err", err)
}
forever := make(chan []byte) forever := make(chan []byte)
go func() { go func() {
@ -74,7 +78,9 @@ func Init() {
log.Infoln("input", mail) log.Infoln("input", mail)
mailer.NewMail([]string{mail.Mail}, mail.TemplateId, mail.LanguageId, mail.BodyData) if err = mailer.NewMail([]string{mail.Mail}, mail.TemplateId, mail.LanguageId, mail.BodyData); err == nil {
d.Ack(false)
}
} }
}() }()