2

我在我的应用程序中使用 resque 来处理延迟工作,我无法异步向大量用户发送电子邮件和短信。并且数据存储在 mongodb 中,mongoid 是 ODM 连接 rails & mongo。

我的 mongoid 模型看起来像这样

class Item
  include Mongoid::Document
  include Geo::LocationHelper 

  field :name, :type => String
  field :desc, :type => String

  #resque queue name
  @queue = :item_notification

  #resque perform method
  def self.perform(item_id)
     @item = Item.find(item_id)

  end

end

我可以向 resque 添加作业,我已经使用 resque-web 进行了验证。每当我开始一个resque-worker

QUEUE=item_notification rake resque:work

我得到了未初始化的常量 Item,因为我使用 resque 作为 rails gem 并在 rails root 中启动 rake,我相信应该加载我的 mongoid 模型。

经过大量挖掘,我发现我们可以通过以下方式明确要求 rake 加载环境

  QUEUE=item_notification rake environment resque:work

但现在我也得到了同样的错误未初始化常量项

有人可以帮我吗?

和我的

4

1 回答 1

0

实际上,这是开发环境中的一个问题。将此行添加到 resque.rake 任务文件后

# load the Rails app all the time
namespace :resque do
  puts "Loading Rails environment for Resque"
  task :setup => :environment
  ActiveRecord::Base.send(:descendants).each { |klass|  klass.columns }
end

它工作正常

代码取自GitHub-Resque-Wiki

于 2011-06-22T14:24:56.073 回答