开发者

How to specify a default queue to use for all jobs with Resque in Rails?

I want all the enqueue calls to default to a certain queue unless specified otherwise so it's DRY and easier to maintain. In order to specify a queue, the documentation said to define a variable @queue = X within the class. So, I tried doing the following and it didn't work, any ideas?

class ResqueJob
  class << self; attr_accessor :queue end
  @qu开发者_C百科eue = :app
end

class ChildJob < ResqueJob
  def self.perform
  end
end

Resque.enqueue(ChildJob)

Resque::NoQueueError: Jobs must be placed onto a queue.
from /Library/Ruby/Gems/1.8/gems/resque-1.10.0/lib/resque/job.rb:44:in `create'
from /Library/Ruby/Gems/1.8/gems/resque-1.10.0/lib/resque.rb:206:in `enqueue'
from (irb):5


In ruby, class variables are not inherited. That is why Resque can't find your @queue variable.

You should instead define self.queue in your parent class. Resque first checks for the presence of @queue, but looks secondarily for a queue class method:

class ResqueJob
  def self.queue; :app; end
end

class ChildJob < ResqueJob
  def self.perform; ...; end
end


If you want to do this with a mixin, you can do it like this:

module ResqueJob
  extend ActiveSupport::Concern

  module ClassMethods
    def queue
      @queue || :interactor_operations
    end
  end
end

class ChildJob
  include ResqueJob

  def self.perfom
  end
end

(if you don't have activesupport, you can also do this the classical ruby way, but I find this way easier, so well worth the weight ;) )


Try a mixin. Something like this:

module ResqueJob
  def initQueue
    @queue = :app
  end 
end

class ChildJob 
  extend ResqueJob

  initQueue

  def self.perform
  end
end

Resque.enqueue(ChildJob)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜