BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr
API sender - Renewal lock
lotka22c3a846f2e
diff --git a/core/internal/service/batch_mail/api_mail_send.go b/core/internal/service/batch_mail/api_mail_send.goindex e4816934..248d604b 100644--- a/core/internal/service/batch_mail/api_mail_send.go+++ b/core/internal/service/batch_mail/api_mail_send.go@@ -25,7 +25,7 @@ const (QueryTimeout = 15 // Query timeout duration (seconds)SendTimeout = 15 // Send timeout duration (seconds)LockKey = "api_mail_queue_lock"- LockTimeout = 60 // Lock timeout duration (seconds)+ LockTimeout = 60 * 2 // Lock timeout duration (seconds))type ApiMailLog struct {@@ -160,10 +160,31 @@ func ProcessApiMailQueueWithLock(ctx context.Context) {return}- // Set expiration time- g.Redis().Expire(ctx, LockKey, int64(LockTimeout))+ // // Set expiration time+ // g.Redis().Expire(ctx, LockKey, int64(LockTimeout))++ // defer func() {+ // g.Redis().Del(ctx, LockKey)+ // }()++ // Start the goroutine for lease renewal+ stopRenew := make(chan struct{})+ go func() {+ ticker := time.NewTicker(60 * time.Second)+ defer ticker.Stop()+ for {+ select {+ case <-ticker.C:+ // Renewal lock+ g.Redis().Expire(ctx, LockKey, int64(LockTimeout))+ case <-stopRenew:+ return+ }+ }+ }()defer func() {+ close(stopRenew)g.Redis().Del(ctx, LockKey)}()@@ -177,14 +198,14 @@ func ProcessApiMailQueue(ctx context.Context) {defer cancel()// Process all pending emails in batches- offset := 0+ lastMaxId := int64(0)for {// Get a batch of pending emailsvar mailLogs []ApiMailLogerr := g.DB().Model("api_mail_logs").Where("status = ?", StatusPending).+ Where("id > ?", lastMaxId).Order("id ASC").- Offset(offset).Limit(BatchSize).Ctx(ctx).Scan(&mailLogs)@@ -218,7 +239,7 @@ func ProcessApiMailQueue(ctx context.Context) {for i := 0; i < maxSenderGoroutinesPerAddresser; i++ {sender, err := mail_service.NewEmailSenderWithLocal(addresser)if err != nil {-+ g.Log().Errorf(ctx, "Failed to create sender for %s: %v", addresser, err)for _, log := range logs {updateLogStatus(ctx, log.Id, StatusFailed, "Failed to create sender: "+err.Error())}@@ -294,7 +315,11 @@ func ProcessApiMailQueue(ctx context.Context) {}wg.Wait()- offset += len(mailLogs)+ // lastMaxId = mailLogs[len(mailLogs)-1].Id+ if len(mailLogs) > 0 {+ lastMaxId = mailLogs[len(mailLogs)-1].Id+ }+ // offset += len(mailLogs)if len(mailLogs) < BatchSize {break}diff --git a/core/internal/service/timers/timers.go b/core/internal/service/timers/timers.goindex 12b8d264..dad2138f 100644--- a/core/internal/service/timers/timers.go+++ b/core/internal/service/timers/timers.go@@ -130,7 +130,6 @@ func Start(ctx context.Context) (err error) {})gtimer.Add(1*time.Minute, func() {- //gtimer.Add(5*time.Second, func() {batch_mail.ProcessApiMailQueueWithLock(ctx)})