我有一个相当简单的Rails应用程序,允许用户管理他们的客户端并为每个客户端创建一个案例。创建案例时,会将详细信息发送到两个电子邮件地址(每封电子邮件中的不同内容),并使用gem将信息发送到 FreeagentCentral
。
我已经实现了 delayed_job
,它适用于 Freeagent API
调用,但我认为可能有更好的,更低的开销,发送电子邮件的方法。这似乎需要一段时间。
我目前在Rails 2.3应用程序中有以下代码。
kases_controller.rb
# POST /kases
# POST /kases.xml
def create
@company = Company.find(params[:kase][:company_id])
@kase = @company.kases.new(params[:kase])
if @kase.save
UserMailer.deliver_makeakase("dropbox@1223111.domain.highrisehq.com", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.deliver_makeakaseteam("surveymanager@domain.co.uk", "Highrise", @kase) if params[:notify_team_of_creation]
@kase.delay.create_freeagent_project(current_user) if params[:send_to_freeagent]
redirect_to(@kase)
#flash[:notice] = 'Case was successfully created.'
flash[:notice] = fading_flash_message("Case was successfully created.", 5)
else
render :new
end
end
user_mailer.rb
def makeakase(email, name, kase, bccemails = [])
recipients email
from "info@domain.co.uk"
subject "FW: Case creation from Survey Manager"
bcc bccemails
sent_on Time.now
body :name => name, :kase => kase
end
def makeakaseteam(email, name, kase = [])
recipients email
from "info@domain.co.uk"
subject "A new case has been created."
sent_on Time.now
body :name => name, :kase => kase
content_type "text/html"
end
我正在寻找减少发送电子邮件数量的建议,或者将这些电子邮件添加到延迟工作的方法。
我使用PostmarkApp发送电子邮件,这是另一个宝石。有关详细信息,请访问:postmark-gem
答案 0 :(得分:1)
不确定这是否是您想要的,但我会制作一个方法来发送电子邮件和创建freeagent,然后让它由delayed_job处理。
像
这样的东西class Kase
def send_emails(current_user, send_to_highrise, notify_team, send_to_freeagent)
UserMailer.deliver_makeakase("dropbox@1223111.domain.highrisehq.com", "Highrise", self) if send_to_highrise
UserMailer.deliver_makeakaseteam("surveymanager@domain.co.uk", "Highrise", self) if notify_team
self.create_freeagent_project(current_user) if send_to_freeagent
end
end
然后在你的控制器中你会写
if @kase.save
@kase.delay.send_emails(current_user, params[:sendtohighrise],
params[:notify_team_of_creation], params[:send_to_freeagent])
redirect_to @kase
flash[:notice] = fading_flash_message("Case was successfully created.", 5)
else
...
或只是传递params
可能更简单/更清洁。
答案 1 :(得分:0)
我已成功使用Starling and Workling来解决类似问题。我不确定这是否是一个较低的开销,因为我个人从未使用delayed_job
。您可能想要探索它。
答案 2 :(得分:0)
安装了delayed_job后,对于旧版本,您应该可以执行以下操作。
UserMailer.send_later(:deliver_makeakase, "dropbox@1223111.domain.highrisehq.com", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.send_later(:deliver_makeakaseteam, "surveymanager@domain.co.uk", "Highrise", @kase) if params[:notify_team_of_creation]
对于从2.0.4开始的版本,语法略有不同。
UserMailer.delay.deliver_makeakase("dropbox@1223111.domain.highrisehq.com", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.delay.deliver_makeakaseteam("surveymanager@domain.co.uk", "Highrise", @kase) if params[:notify_team_of_creation]