How do I specify a default queue for all jobs using Resque in Rails?

I want all calls to the queue to be in a specific queue by default, unless otherwise specified, therefore it is DRY and easier to maintain. To indicate the queue, the documentation indicated that the variable @queue = X was set in the class. So, I tried to do the following and it did not work, any ideas?

class ResqueJob
  class << self; attr_accessor :queue end
  @queue = :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
+5
source share
3 answers

In ruby, class variables are not inherited. This is why Resque cannot find your @queue variable.

Instead, you should define self.queuein your parent class. Resque first checks for @queue, but a second time searches for a class method queue:

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

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

mixin, :

module ResqueJob
  extend ActiveSupport::Concern

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

class ChildJob
  include ResqueJob

  def self.perfom
  end
end

( activesupport, , , ;))

+4

Try mixing. Something like that:

module ResqueJob
  def initQueue
    @queue = :app
  end 
end

class ChildJob 
  extend ResqueJob

  initQueue

  def self.perform
  end
end

Resque.enqueue(ChildJob)
0
source

All Articles