Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: resque/resque
...
head fork: mDialog/resque
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
23 lib/resque.rb
@@ -214,7 +214,7 @@ def watch_queue(queue)
#
# This method is considered part of the `stable` API.
def enqueue(klass, *args)
- Job.create(queue_from_class(klass), klass, *args)
+ Job.create(queue_from_class(klass, args), klass, *args)
Plugin.after_enqueue_hooks(klass).each do |hook|
klass.send(hook, *args)
@@ -249,14 +249,21 @@ def enqueue(klass, *args)
#
# This method is considered part of the `stable` API.
def dequeue(klass, *args)
- Job.destroy(queue_from_class(klass), klass, *args)
+ Job.destroy(queue_from_class(klass, args), klass, *args)
end
# Given a class, try to extrapolate an appropriate queue based on a
# class instance variable or `queue` method.
- def queue_from_class(klass)
- klass.instance_variable_get(:@queue) ||
- (klass.respond_to?(:queue) and klass.queue)
+ def queue_from_class(klass, args=nil)
+ if queue = klass.instance_variable_get(:@queue)
+ queue
+ elsif klass.respond_to?(:queue)
+ if klass.method(:queue).arity == 1
+ klass.queue(args)
+ else
+ klass.queue
+ end
+ end
end
# This method will return a `Resque::Job` object or a non-true value
@@ -273,9 +280,9 @@ def reserve(queue)
# If no queue can be inferred this method will raise a `Resque::NoQueueError`
#
# If given klass is nil this method will raise a `Resque::NoClassError`
- def validate(klass, queue = nil)
- queue ||= queue_from_class(klass)
-
+ def validate(klass, args=[], queue = nil)
+ queue ||= queue_from_class(klass, args)
+
if !queue
raise NoQueueError.new("Jobs must be placed onto a queue.")
end
View
2  lib/resque/job.rb
@@ -40,7 +40,7 @@ def initialize(queue, payload)
#
# Raises an exception if no queue or class is given.
def self.create(queue, klass, *args)
- Resque.validate(klass, queue)
+ Resque.validate(klass, args, queue)
if Resque.inline?
constantize(klass).perform(*decode(encode(args)))
View
42 test/resque_test.rb
@@ -21,7 +21,7 @@
new_namespace = Redis::Namespace.new("namespace", :redis => new_redis)
Resque.redis = new_namespace
assert_equal new_namespace, Resque.redis
-
+
Resque.redis = 'localhost:9736/namespace'
end
@@ -106,7 +106,7 @@
assert Resque::Job.create(:jobs, 'some-job', 20, '/tmp')
assert_equal Resque.reserve(:jobs), Resque.reserve(:jobs)
- assert Resque::Job.create(:jobs, 'SomeMethodJob', 20, '/tmp')
+ assert Resque::Job.create(:jobs, 'SomeMethodWithoutArgsJob', 20, '/tmp')
assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
assert_not_equal Resque.reserve(:jobs), Resque.reserve(:jobs)
@@ -115,21 +115,49 @@
assert_not_equal Resque.reserve(:jobs), Resque.reserve(:jobs)
end
- test "can put jobs on a queue by way of a method" do
+ test "can put jobs on a queue by way of a method with no args" do
assert_equal 0, Resque.size(:method)
- assert Resque.enqueue(SomeMethodJob, 20, '/tmp')
- assert Resque.enqueue(SomeMethodJob, 20, '/tmp')
+ assert Resque.enqueue(SomeMethodWithoutArgsJob, 20, '/tmp')
+ assert Resque.enqueue(SomeMethodWithoutArgsJob, 20, '/tmp')
job = Resque.reserve(:method)
assert_kind_of Resque::Job, job
- assert_equal SomeMethodJob, job.payload_class
+ assert_equal SomeMethodWithoutArgsJob, job.payload_class
assert_equal 20, job.args[0]
assert_equal '/tmp', job.args[1]
assert Resque.reserve(:method)
assert_equal nil, Resque.reserve(:method)
end
+
+ test "can put jobs on a queue by way of a method with args" do
+ assert_equal 0, Resque.size(:method_20_test)
+ assert Resque.enqueue(SomeMethodWithArgsJob, 20, 'test', 'arg3')
+ assert Resque.enqueue(SomeMethodWithArgsJob, 20, 'test', 'arg3')
+
+ job = Resque.reserve(:method_20_test)
+
+ assert_kind_of Resque::Job, job
+ assert_equal SomeMethodWithArgsJob, job.payload_class
+ assert_equal 20, job.args[0]
+ assert_equal 'test', job.args[1]
+
+ assert Resque.reserve(:method_20_test)
+ assert_equal nil, Resque.reserve(:method_20_test)
+ end
+
+ test "can remove jobs from a queue by way of a method with args" do
+ assert_equal 0, Resque.size(:method_20_test)
+ assert Resque.enqueue(SomeMethodWithArgsJob, 20, 'test', 'arg3')
+ assert Resque.enqueue(SomeMethodWithArgsJob, 20, 'test', 'arg4')
+ assert Resque.enqueue(SomeMethodWithArgsJob, 20, 'test', 'arg3')
+ assert Resque.enqueue(SomeMethodWithArgsJob, 20, 'test', 'arg4')
+ assert_equal 4, Resque.size(:method_20_test)
+
+ assert Resque.dequeue(SomeMethodWithArgsJob, 20, 'test', 'arg3')
+ assert_equal 2, Resque.size(:method_20_test)
+ end
test "needs to infer a queue with enqueue" do
assert_raises Resque::NoQueueError do
@@ -138,6 +166,8 @@
end
test "validates job for queue presence" do
+ assert_nil Resque.validate(SomeMethodWithArgsJob, [20, 'test', 'arg3'])
+
assert_raises Resque::NoQueueError do
Resque.validate(SomeJob)
end
View
8 test/test_helper.rb
@@ -93,12 +93,18 @@ class SomeIvarJob < SomeJob
@queue = :ivar
end
-class SomeMethodJob < SomeJob
+class SomeMethodWithoutArgsJob < SomeJob
def self.queue
:method
end
end
+class SomeMethodWithArgsJob < SomeJob
+ def self.queue(args)
+ "method_#{ args[0..-2].collect { |a| a.to_s }.join("_") }"
+ end
+end
+
class BadJob
def self.perform
raise "Bad job!"

No commit comments for this range

Something went wrong with that request. Please try again.