Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  @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
like image 695
Kang Chen Avatar asked Feb 24 '11 08:02

Kang Chen


People also ask

How do I start resque rails?

Open up http://localhost:3000/resque in a browser to check out the web running jobs and schedules. If you are using Resque-scheduler then it will add more options for you in the resque-web UI. Those options allows you to view queues and manually start queuing.

What is Activejob when should we use it?

Active Job is a framework for declaring jobs and making them run on a variety of queueing backends. These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really.

What is Resque-scheduler?

Resque-scheduler is an extension to Resque that adds support for queueing items in the future. Job scheduling is supported in two different ways: Recurring (scheduled) and Delayed. Scheduled jobs are like cron jobs, recurring on a regular basis.

How do I enqueue a job in rails?

Enqueue jobs The different enqueuing options are the following: wait : With this, the job is enqueued after the mentioned time period. wait_until : With this, the job is enqueued after that the specified time has elapsed. queue : With this, the job is added to the specified queue.


1 Answers

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
like image 162
adamlamar Avatar answered Oct 03 '22 00:10

adamlamar