Serializza gli invii SMTP su una coda unica per non far chiudere Aruba.
CI / scan_js (push) Has been cancelled
CI / scan_ruby (push) Has been cancelled
CI / lint (push) Has been cancelled

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-07 12:06:23 +02:00
co-authored by Cursor
parent a9470d7e77
commit 403fe80d37
12 changed files with 423 additions and 29 deletions
+3 -1
View File
@@ -30,7 +30,9 @@ class MailIdentity < ApplicationRecord
port: smtp_port,
enable_starttls_auto: encryption == "starttls",
ssl: encryption == "tls",
openssl_verify_mode: verify_ssl? ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
openssl_verify_mode: verify_ssl? ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE,
open_timeout: 15,
read_timeout: 60
}
if smtp_username.present?
settings[:user_name] = smtp_username
+44 -6
View File
@@ -71,32 +71,70 @@ class MailingRecipient < ApplicationRecord
MailMerge.render(mailing.subject_for(ab_variant), **merge_context)
end
def deliver!
def queued?
status == "queued"
end
def enqueue_for_delivery!
Mailings::OutboundQueue.enqueue(self)
end
def deliver_queued!
with_lock do
return if status.in?(%w[sent skipped])
return abort_delivery!("invio annullato") unless mailing.sending?
update!(status: "queued")
return unless queued?
end
return unless mailing.sending?
html = rendered_html
subject_line = rendered_subject_line
CampaignMailer.outreach(self, html: html, subject: subject_line).deliver_now
Mailings::SmtpGate.deliver do
CampaignMailer.outreach(self, html: html, subject: subject_line).deliver_now
end
record_success!(subject_line)
:sent
rescue *Mailings::SmtpGate::RETRYABLE => e
defer_or_fail!(e)
rescue StandardError => e
update!(status: "failed", error_message: e.message.to_s.truncate(500))
:failed
ensure
mailing.mark_finished_if_done!
end
def deliver!
enqueue_for_delivery!
deliver_queued! if queued?
end
private
def abort_delivery!(reason)
update!(status: "skipped", skip_reason: reason)
end
MAX_SMTP_DEFERS = 8
DEFER_PATTERN = /tentativo (\d+)\//
def defer_or_fail!(error)
attempt = smtp_defer_count + 1
message = error.message.to_s.truncate(400)
if attempt >= MAX_SMTP_DEFERS
update!(status: "failed", error_message: message)
:failed
else
update!(
status: "queued",
error_message: "Da ritentare (#{attempt}/#{MAX_SMTP_DEFERS}): #{message}"
)
:deferred
end
end
def smtp_defer_count
error_message.to_s[DEFER_PATTERN, 1].to_i
end
def record_success!(subject_line)
transaction do
update!(status: "sent", sent_at: Time.current, rendered_subject: subject_line, error_message: nil)