Skip to content

Commit

Permalink
Merge pull request #56 from kenpratt/before-enqueue-hooks-compatibility
Browse files Browse the repository at this point in the history
Statuses are added for jobs that fail before_enqueue hooks
  • Loading branch information
quirkey committed Mar 30, 2012
2 parents 1c37725 + d1afe64 commit dde6dca
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
14 changes: 10 additions & 4 deletions lib/resque/plugins/status.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -80,11 +80,17 @@ def create(options = {})
end end


# Adds a job of type <tt>klass<tt> to the queue with <tt>options<tt>. # Adds a job of type <tt>klass<tt> to the queue with <tt>options<tt>.
# Returns the UUID of the job #
# Returns the UUID of the job if the job was queued, or nil if the job was
# rejected by a before_enqueue hook.
def enqueue(klass, options = {}) def enqueue(klass, options = {})
uuid = Resque::Plugins::Status::Hash.create :options => options uuid = Resque::Plugins::Status::Hash.generate_uuid
Resque.enqueue(klass, uuid, options) if Resque.enqueue(klass, uuid, options)
uuid Resque::Plugins::Status::Hash.create uuid, :options => options
uuid
else
nil
end
end end


# This is the method called by Resque::Worker when processing jobs. It # This is the method called by Resque::Worker when processing jobs. It
Expand Down
3 changes: 1 addition & 2 deletions lib/resque/plugins/status/hash.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class Hash < ::Hash


# Create a status, generating a new UUID, passing the message to the status # Create a status, generating a new UUID, passing the message to the status
# Returns the UUID of the new status. # Returns the UUID of the new status.
def self.create(*messages) def self.create(uuid, *messages)
uuid = generate_uuid
set(uuid, *messages) set(uuid, *messages)
redis.zadd(set_key, Time.now.to_i, uuid) redis.zadd(set_key, Time.now.to_i, uuid)
redis.zremrangebyscore(set_key, 0, Time.now.to_i - @expire_in) if @expire_in redis.zremrangebyscore(set_key, 0, Time.now.to_i - @expire_in) if @expire_in
Expand Down
12 changes: 12 additions & 0 deletions test/test_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -98,3 +98,15 @@ def perform
failed("I'm such a failure") failed("I'm such a failure")
end end
end end

class NeverQueuedJob
include Resque::Plugins::Status

def self.before_enqueue(*args)
false
end

def perform
# will never get called
end
end
20 changes: 20 additions & 0 deletions test/test_resque_plugins_status.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ class TestResquePluginsStatus < Test::Unit::TestCase


end end


context ".create with a failing before_enqueue hook" do
setup do
@size = Resque.size(:statused)
@status_ids_size = Resque::Plugins::Status::Hash.status_ids.length
@res = NeverQueuedJob.create(:num => 100)
end

should "return nil" do
assert_equal nil, @res
end

should "not create a status" do
assert_equal @size, Resque.size(:statused)
end

should "not add the uuid to the statuses" do
assert_equal @status_ids_size, Resque::Plugins::Status::Hash.status_ids.length
end
end

context ".scheduled" do context ".scheduled" do
setup do setup do
@job_args = {'num' => 100} @job_args = {'num' => 100}
Expand Down
18 changes: 9 additions & 9 deletions test/test_resque_plugins_status_hash.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase
setup do setup do
Resque.redis.flushall Resque.redis.flushall
Resque::Plugins::Status::Hash.expire_in = nil Resque::Plugins::Status::Hash.expire_in = nil
@uuid = Resque::Plugins::Status::Hash.create @uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid)
Resque::Plugins::Status::Hash.set(@uuid, "my status") Resque::Plugins::Status::Hash.set(@uuid, "my status")
@uuid_with_json = Resque::Plugins::Status::Hash.create({"im" => "json"}) @uuid_with_json = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, {"im" => "json"})
end end


context ".get" do context ".get" do
Expand Down Expand Up @@ -43,35 +43,35 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase
context ".create" do context ".create" do
should "add an item to a key set" do should "add an item to a key set" do
before = Resque::Plugins::Status::Hash.status_ids.length before = Resque::Plugins::Status::Hash.status_ids.length
Resque::Plugins::Status::Hash.create Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid)
after = Resque::Plugins::Status::Hash.status_ids.length after = Resque::Plugins::Status::Hash.status_ids.length
assert_equal 1, after - before assert_equal 1, after - before
end end


should "return a uuid" do should "return a uuid" do
assert_match(/^\w{32}$/, Resque::Plugins::Status::Hash.create) assert_match(/^\w{32}$/, Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid))
end end


should "store any status passed" do should "store any status passed" do
uuid = Resque::Plugins::Status::Hash.create("initial status") uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, "initial status")
status = Resque::Plugins::Status::Hash.get(uuid) status = Resque::Plugins::Status::Hash.get(uuid)
assert status.is_a?(Resque::Plugins::Status::Hash) assert status.is_a?(Resque::Plugins::Status::Hash)
assert_equal "initial status", status.message assert_equal "initial status", status.message
end end


should "expire keys if expire_in is set" do should "expire keys if expire_in is set" do
Resque::Plugins::Status::Hash.expire_in = 1 Resque::Plugins::Status::Hash.expire_in = 1
uuid = Resque::Plugins::Status::Hash.create("new status") uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, "new status")
assert_contains Resque::Plugins::Status::Hash.status_ids, uuid assert_contains Resque::Plugins::Status::Hash.status_ids, uuid
assert_equal "new status", Resque::Plugins::Status::Hash.get(uuid).message assert_equal "new status", Resque::Plugins::Status::Hash.get(uuid).message
sleep 2 sleep 2
Resque::Plugins::Status::Hash.create Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid)
assert_does_not_contain Resque::Plugins::Status::Hash.status_ids, uuid assert_does_not_contain Resque::Plugins::Status::Hash.status_ids, uuid
assert_nil Resque::Plugins::Status::Hash.get(uuid) assert_nil Resque::Plugins::Status::Hash.get(uuid)
end end


should "store the options for the job created" do should "store the options for the job created" do
uuid = Resque::Plugins::Status::Hash.create("new", :options => {'test' => '123'}) uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, "new", :options => {'test' => '123'})
assert uuid assert uuid
status = Resque::Plugins::Status::Hash.get(uuid) status = Resque::Plugins::Status::Hash.get(uuid)
assert status.is_a?(Resque::Plugins::Status::Hash) assert status.is_a?(Resque::Plugins::Status::Hash)
Expand All @@ -98,7 +98,7 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase


setup do setup do
@uuids = [] @uuids = []
30.times{ Resque::Plugins::Status::Hash.create } 30.times{ Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid) }
end end


should "return an array of job ids" do should "return an array of job ids" do
Expand Down

1 comment on commit dde6dca

@zoras
Copy link

@zoras zoras commented on dde6dca Apr 23, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this pull request explains/fixes issue #42

Please sign in to comment.