我有一份工作(由delayed_job
安排),当新用户注册到该应用程序时,该工作会发送电子邮件。这是用户模型:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :country, :phone_number, :opt_in
def email=(email)
super
Delayed::Job.enqueue self
end
def perform
begin
UserMailer.welcome_email self
rescue => e
STDERR.puts "Cannot perform the mailer: #{e}"
end
end
end
负责注册新用户的操作是:
def create
user = User.new({:email => params[:email],
:password => params[:password],
:password_confirmation => params[:password_confirmation],
:country => params[:country],
:opt_in => Boolean(params[:opt_in]),
:phone_number => params[:phone_number]})
if user.save(:validate => false)
redirect_to wrap_users_path
end
end
当用户注册时,我得到以下日志:
开始POST“/ wrap_users” ...于2011-05-30 12:58:35 +0200由WrapUsersController处理#create as HTML
参数: { “电子邮件”=&gt; “中XXXX”, “密码”=&gt; “中[FILTERED]”, “password_confirmation”=&gt; “中[FILTERED]”, “PHONE_NUMBER”=&gt; “中XXX”, “opt_in”=&gt;“true”,“country”=&gt;“France”}
AREL(0.3ms)INSERT INTO “delayed_jobs”(“优先级”, “尝试”,“处理程序”,“last_error”, “run_at”,“locked_at”,“failed_at”, “locked_by”,“created_at”, “updated_at”)VALUES(0,0,'--- !ruby / ActiveRecord:用户属性:
电子邮件:xxxx encrypted_password: $ 2A $ 10 $ DwHB / 5zSp38xdAAIkYriSuwIiLyKy2geU5kLmzjz2f1WsAuxpqyIW reset_password_token:
reset_password_sent_at:
remember_created_at:sign_in_count: 0 current_sign_in_at:
last_sign_in_at:current_sign_in_ip: last_sign_in_ip:created_at:
updated_at:country:France opt_in: true phone_number:xxx', NULL,'2011-05-30 10:58:35.250464', NULL,NULL,NULL,'2011-05-30 10:58:35.250560','2011-05-30 10:58:35.250560')AREL(2.1ms)INSERT INTO“users”(“email”, “encrypted_password” “reset_password_token” “reset_password_sent_at” “remember_created_at” “sign_in_count”,“current_sign_in_at”, “last_sign_in_at” “current_sign_in_ip” “last_sign_in_ip”,“created_at”, “updated_at”,“country”,“opt_in”, “phone_number”)价值观 ( 'XXX', '$ 2A $ 10 $ DwHB / 5zSp38xdAAIkYriSuwIiLyKy2geU5kLmzjz2f1WsAuxpqyIW', NULL,NULL,NULL,0,NULL,NULL,NULL, NULL,'2011-05-30 10:58:35.251788', '2011-05-30 10:58:35.251788', '法国','t','xxx') 重定向到 http://...:3000/wrap_users 完成302发现在277ms
正如您所看到的,根据日志,正在将条目记录到Delayed::Job
表中。但实际上,在计算此表中记录的数量时(在同一开发模式下的rails console
上),我得到0.
发生了什么事?该日志未提及有关insert into delayed_jobs
步骤的任何问题。
由于
答案 0 :(得分:3)
该表是空的,可能是因为一名工人将该工作从那里取出并删除了它。 作为旁注,我会使用after_create回调而不是email =来安排作业。至少有一个原因 - 即使验证失败,您也会发送电子邮件。