【问题标题】:Weird delayed_job behaviour奇怪的延迟作业行为
【发布时间】:2011-09-04 18:05:04
【问题描述】:

我有一份工作(由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

参数: {"电子邮件"=>"xxxx", "密码"=>"[过滤]", "password_confirmation"=>"[过滤]", "电话号码"=>"xxx", "opt_in"=>"true", "country"=>"France"}

AREL (0.3ms) 插入 "delayed_jobs" ("优先级", “尝试”、“处理程序”、“last_error”、 “run_at”、“locked_at”、“failed_at”、 “locked_by”、“created_at”、 "updated_at") 值 (0, 0, '--- !ruby/ActiveRecord:用户属性:
电子邮件:xxx 加密密码: $2a$10$DwHB/5zSp38xdAAIkYriSuwIiLyKy2geU5kLmzjz2f1WsAuxpqyIW 重置密码令牌:
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: 国家: 法国 opt_in: 真正的电话号码:xxx', 空, '2011-05-30 10:58:35.250464', 空,空,空,'2011-05-30 10:58:35.250560', '2011-05-30 10:58:35.250560') AREL (2.1ms)

插入“用户”(“电子邮件”, “加密密码”, "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”、“国家”、“opt_in”、 “电话号码”)值 ('xxx', '$2a$10$DwHB/5zSp38xdAAIkYriSuwIiLyKy2geU5kLmzjz2f1WsAuxpqyIW', 空,空,空,0,空,空,空, 空, '2011-05-30 10:58:35.251788', '2011-05-30 10:58:35.251788', '法国','t','xxx') 重定向到 http://...:3000/wrap_users 完成 302 在 277 毫秒内找到

如您所见,根据日志,正在将条目记录到Delayed::Job 表中。但实际上,在计算这个表中的记录数时(在相同的开发模式下rails console)我得到0。

发生了什么事?日志没有提到关于insert into delayed_jobs 步骤的任何问题。

谢谢

【问题讨论】:

    标签: ruby-on-rails-3 actionmailer delayed-job


    【解决方案1】:

    该表是空的,可能是因为一个工人把工作从那里拿走了并删除了它。 作为旁注,我会使用 after_create 回调而不是 email= 来安排工作。至少有一个原因 - 即使验证失败,您仍然会发送电子邮件。

    【讨论】:

    • 这就是为什么它很奇怪。我正在运行的工人没有处理这项工作!所以没有理由可以删除作业。
    • 我仍然强烈建议使用回调或观察者方法,而不是覆盖 setter。
    • 确实如此。我改变了它,我现在正在使用 after_create ! :)
    猜你喜欢
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多