Delayed job and Mandrill: uninitialized constant Mandrill::API

☆樱花仙子☆ 提交于 2019-12-22 10:34:22

问题


I have mailer service where users can upload an .xls file with emails and some other user related data to send an email campaign.

I was having some timeout issues since it takes some seconds to process (as I do some validation and configuration for each email to be sent, eg: save records to database, check if an email was sent in the last 30 days, create personalised html code for each email (to create links that contain the email address as a parameter, etc).

After some research, moving this to a delayed job seemed reasonable, as suggested in this rails cast. The only problem is that I am having an error that says uninitialized constant Mandrill::API, it seems that when I run the job, the call require 'mandrill' doesn't work.

I created the task in my model file. Something like this

class Mailer < ActiveRecord::Base
  attr_accessible :email, :lastname, :name

  def self.send_mail(emails)

    [...a lot of code here...]

    require 'mandrill'
    m = Mandrill::API.new ENV['MANDRILL_APIKEY']
    message = {
    :subject=> template.subject,
    :from_name=> template.from_name,
    :from_email=> from + "@" + email_domain,
    :to=>mails,
    :global_merge_vars=> [
      { :name => 'GREETING', :content => template.greeting},
      { :name => 'CONT1', :content => template.message},
      { :name => 'IMAGE', :content => image_url},
          ],
    :html=>email_template,
    :preserve_recipients => false,
    :merge_vars => email_contents,
    }
    sending = m.messages.send message

  end
end

from my controller I call Mailer.send_mails(emails) and it works just fine, but if I call Mailer.delay.send_mails(emails) I get the error. How can I fix this?

I have tried adding require 'mandrill' at the beginning of the class, before and after class Mailer < ActiveRecord::Base but nothing seems to work


回答1:


Make sure to restart the delayed_job daemon to pick up any code changes. It does not auto-reload like the Rails development environment does.



来源:https://stackoverflow.com/questions/24582435/delayed-job-and-mandrill-uninitialized-constant-mandrillapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!