批量邮寄时内存泄漏

时间:2018-05-15 08:41:30

标签: ruby-on-rails ruby heroku actionmailer amazon-ses

我正在后台进行电子邮件发送(超过1k的字母)。服务器在Heroku上。对于后台任务,我使用sucker_punch。 Rails是4.1.6,Web服务器是passenger。这是我的代码:

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
end

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
    end
end


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
end

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

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)
    end
end

我观察到内存泄漏。我还看到部分渲染时间随着每个字母而增加:

enter image description here

请告诉我可能出现的问题以及在哪里挖掘。

0 个答案:

没有答案