问题描述
我有邮件服务,用户可以在其中上传包含电子邮件和其他一些用户相关数据的.xls文件,以发送电子邮件广告系列.
I have mailer service where users can upload an .xls file with emails and some other user related data to send an email campaign.
我遇到了一些超时问题,因为要花几秒钟来处理(因为我对要发送的每封电子邮件进行了一些验证和配置,例如:将记录保存到数据库,检查最近30天是否发送了电子邮件,为每封电子邮件创建个性化的html代码(以创建包含电子邮件地址作为参数的链接,等等).
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).
经过研究,将其移至延迟的工作似乎是合理的,如这条路轨.唯一的问题是我遇到一个错误uninitialized constant Mandrill::API
,看来当我运行作业时,调用require 'mandrill'
无效.
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
从我的控制器中调用
,我调用Mailer.send_mails(emails)
,它工作正常,但是如果我调用Mailer.delay.send_mails(emails)
,我得到了错误.我该如何解决?
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?
我曾尝试在课程开始时,class Mailer < ActiveRecord::Base
之前和之后添加require 'mandrill'
,但似乎无济于事
I have tried adding require 'mandrill'
at the beginning of the class, before and after class Mailer < ActiveRecord::Base
but nothing seems to work
推荐答案
请确保重新启动delay_job守护程序以获取所有代码更改.它不会像Rails开发环境那样自动重新加载.
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.
这篇关于延迟的工作和Mandrill:未初始化的常量Mandrill :: API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!