Skip to content

Commit

Permalink
Resque.validate!(klass) method implementation
Browse files Browse the repository at this point in the history
Method used to determine is the given klass could be a valid Resque job
  • Loading branch information
bogdan authored and defunkt committed Feb 16, 2011
1 parent a8f8fe5 commit 3a48ade
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
9 changes: 9 additions & 0 deletions lib/resque.rb
Expand Up @@ -253,6 +253,15 @@ def reserve(queue)
Job.reserve(queue)
end

# Validates if the given klass could be a valid Resque job
#
# 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)
Job.validate!(klass)
end


#
# worker shortcuts
Expand Down
20 changes: 13 additions & 7 deletions lib/resque/job.rb
Expand Up @@ -40,13 +40,7 @@ def initialize(queue, payload)
#
# Raises an exception if no queue or class is given.
def self.create(queue, klass, *args)
if !queue
raise NoQueueError.new("Jobs must be placed onto a queue.")
end

if klass.to_s.empty?
raise NoClassError.new("Jobs must be given a class.")
end
validate!(klass, queue)

ret = Resque.push(queue, :class => klass.to_s, :args => args)
Plugin.after_enqueue_hooks(klass).each do |hook|
Expand Down Expand Up @@ -104,6 +98,18 @@ def self.reserve(queue)
new(queue, payload)
end


# Validates if the given klass could be a valid Resque job
def self.validate!(klass, queue = Resque.queue_from_class(klass))
if !queue
raise NoQueueError.new("Jobs must be placed onto a queue.")
end

if klass.to_s.empty?
raise NoClassError.new("Jobs must be given a class.")
end
end

# Attempts to perform the work represented by this job instance.
# Calls #perform on the class given in the payload with the
# arguments given in the payload.
Expand Down
8 changes: 7 additions & 1 deletion test/resque_test.rb
Expand Up @@ -128,6 +128,12 @@
end
end

test "validates job for queue presence" do
assert_raises Resque::NoQueueError do
Resque.validate!(SomeJob)
end
end

test "can put items on a queue" do
assert Resque.push(:people, { 'name' => 'jon' })
end
Expand Down Expand Up @@ -229,4 +235,4 @@
test "decode bad json" do
assert_nil Resque.decode("{\"error\":\"Module not found \\u002\"}")
end
end
end

0 comments on commit 3a48ade

Please sign in to comment.