Home Memory leak when bulk mailing
Reply: 0

Memory leak when bulk mailing

user2172 Published in June 20, 2018, 10:42 pm

I'm doing an email delivery in the background (more than 1k letters). The server is on Heroku. For background tasks, I use sucker_punch. Rails is 4.1.6, web-server is passenger. This is my code:

def email_blast message, child_message_id, resend
    @download_url = message.doc_file_size ? message.doc.url : ''
    @subscriber = message.subscriber
    @message = message
    send_message child_message_id, resend

def send_message child_message_id, resend
    messages = child_message_id ? Message.where(id: child_message_id) : Message.where(parent_message_id: @message.id)
    messages = messages.joins(:pending_messages).uniq if resend && child_message_id.nil?
    subject = 'New Message'
    type = @message.type.split('::').second || @message.type.split('::').first
    messages.each do |message|
      send_mail message.all_email_addresses, action_name, subject, message, type

def send_mail emails, template, subject, model, type
    response = mail(to: emails, subject: subject,
                    template_name: template).deliver!
    create_track_emails response.string.split(' ').third.strip, get_status(response), 'delivered', emails, model, type
  rescue => e
    create_track_emails '00000', 'failed', e.message, emails, model, type

def get_status response
    response.status == '250' ? 'success' : 'failed'

def create_track_emails smtp_message_id, status, description, emails, model, type
    id = type == 'Logon' ? model.id : model.to_id
    emails.each do |email|
      model.track_emails.create(email_address: email,
                                status: status,
                                status_description: description,
                                smtp_message_id: smtp_message_id,
                                subscriber_id: model.subscriber_id,
                                user_id: id,
                                message_type: type)

I observe memory leak. I also see that the partial render time increases with each letter:

Please tell me what can be the problem, and where to dig.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.325087 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO