1

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

我知道如果我在任何方法声明之后放置handle_asynchronously,该方法将始终异步运行。但我得到这样的未定义方法错误:

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 行

它会工作的!

有任何想法吗?谢谢!

4

2 回答 2

2

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

[control + c]
rake jobs:work

让 mongoid 延迟作业能够找到方法。我不知道为什么。因此,只要您添加了另一行 handle_asynchronously :method,请记住重新启动工作程序

快乐编码!

于 2012-06-02T23:02:58.627 回答
1

你的语法应该是

class Item
   # mongoid include stuff

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

您可以从这里获取更多详细信息https://github.com/collectiveidea/delayed_job

于 2012-05-27T10:22:33.490 回答