そのため、たとえばメール配信には遅延ジョブを使用します。
def send_password_reset
begin
password_reset_token= SecureRandom.urlsafe_base64
end while User.exists?(:password_reset_token => password_reset_token)
self.password_reset_token= password_reset_token
self.password_link_sent_at=Time.now.utc
self.save!(:validate=>false)
UserMailer.delay.password_reset(self)
end
そしてDB:
SQL (1.0ms) INSERT INTO "delayed_jobs" ("attempts", "created_at", "failed_at", "handler", "last_error", "locked_at", "locked_by", "priority", "queue", "run_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id" [["attempts", 0], ["created_at", Wed, 17 Jul 2013 02:18:14 UTC +00:00], ["failed_at", nil], ["handler", "--- !ruby/object:Delayed::PerformableMailer\nobject: !ruby/class 'UserMailer'\nmethod_name: :password_reset\nargs:\n- !ruby/ActiveRecord:User\n attributes:\n id: '114'\n email: testmail@mail.ru\n password_digest: $2a$10$XLYdoO2vSyVuGvcUi05r3eF3of7OvtxWIP.WfJPgD3yju/SE8qiXW\n created_at: '2013-06-15 21:04:42.425284'\n updated_at: 2013-07-17 02:18:14.086686000 Z\n guest: f\n auth_token: oDZA3QN5aznIaK0MRiOqsA\n password_reset_token: 4bVLGOkzEww3bbvs_tAd7g\n password_link_sent_at: 2013-07-17 02:18:14.083686000 Z\n confirmed: t\n confirmation_token: CnSI6tUUYGyCCVTrVnPIow\n confirmation_link_sent_at: '2013-06-15 21:04:36.849965'\n"], ["last_error", nil], ["locked_at", nil], ["locked_by", nil], ["priority", 0], ["queue", nil], ["run_at", Wed, 17 Jul 2013 02:18:14 UTC +00:00], ["updated_at", Wed, 17 Jul 2013 02:18:14 UTC +00:00]]
コンソールから最後のジョブを呼び出すと:
<Delayed::Backend::ActiveRecord::Job id: 127, priority: 0, attempts: 0, handler: "--- !ruby/object:Delayed::PerformableMailer\nobject:...", last_error: nil, run_at: "2013-07-17 02:18:14", locked_at: nil, failed_at: nil, locked_by: nil, queue: nil, created_at: "2013-07-17 02:18:14", updated_at: "2013-07-17 02:18:14">
しかし、実際には何もしません:
[Worker(host:HP-PC pid:13476)] Starting job worker
実行rake jobs:work
した後は単に空で、ジョブの処理については何もありません。
たとえば、次のように呼び出します。
User.delay.test_method
つまり:
def self.test_method
nil.what?
end
エラーなし。遅延ジョブは処理を開始しません。