delayed_job_mongoid不能使用handle_asynchronously,但可以使用.delay.method

时间:2012-05-27 09:07:17

标签: ruby-on-rails mongoid delayed-job

我正在使用Mongoid和delayed_job_mongoid宝石。一切都在最新版本,包括Rails(3.2.3)。

我理解如果在任何方法声明之后我把handle_asynchronously,这个方法将始终运行async。但是我得到了这样的未定义方法错误:

class Item
  # mongoid include stuff

  def shout
    puts 'a simple hi'
  end
  handle_asynchronously :shout
end

item = Item.create
item.shout # returns a delayed job object

j = _
# wait a moment
j.last_error 

{undefined method `shout_without_delay' for #<Item:0x007f8365e62978>
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/mongoid-2.4.9/lib/mongoid/attributes.rb:166:in `method_missing'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/performable_method.rb:26:in `perform'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/backend/base.rb:94:in `block in invoke_job'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/lifecycle.rb:60:in `call'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/lifecycle.rb:60:in `block in initialize'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/lifecycle.rb:65:in `call'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/lifecycle.rb:65:in `execute'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/lifecycle.rb:38:in `run_callbacks'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/backend/base.rb:91:in `invoke_job'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/worker.rb:178:in `block (2 levels) in run'
/Users/nik/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
/Users/nik/.rvm/gems/ruby-1.9.3-p194/gems/delayed_job-3.0.2/lib/delayed/worker.rb:178:in `block in run'
/Users/nik/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/benchmark.rb:295:in `realtime'

但如果我打电话

item.delay.shout

没有handle_asynchronously行

它会起作用!

有什么想法吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

这是该帖子的作者。我发现了发生了什么。简短回答:您必须重新启动终端中的工作人员

[control + c]
rake jobs:work

对于mongoid延迟作业能够找到该方法。我不知道为什么。所以只要记住每当你添加另一行handle_asynchronously时重启worker:方法

快乐的编码!

答案 1 :(得分:1)

你的语法应该是

class Item
   # mongoid include stuff

   def shout
      puts 'a simple hi'
   end
   handle_asynchronously :shout
end

您可以从https://github.com/collectiveidea/delayed_job

获取更多详细信息