如何在 Rails 中使用 Resque 指定用于所有作业的默认队列? [英] How to specify a default queue to use for all jobs with Resque in Rails?
问题描述
除非另有说明,否则我希望所有入队调用都默认为某个队列,这样它既简单又易于维护.为了指定一个队列,文档说要在类中定义一个变量@queue = X.所以,我尝试执行以下操作,但没有奏效,有什么想法吗?
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
推荐答案
在 ruby 中,类变量不会被继承.这就是为什么 Resque 找不到您的 @queue 变量.
In ruby, class variables are not inherited. That is why Resque can't find your @queue variable.
你应该在你的父类中定义self.queue
.Resque 首先检查@queue 是否存在,然后再查找queue
类方法:
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
这篇关于如何在 Rails 中使用 Resque 指定用于所有作业的默认队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!