Skip to content
This repository
Browse code

Merge pull request #56 from kenpratt/before-enqueue-hooks-compatibility

Statuses are added for jobs that fail before_enqueue hooks
  • Loading branch information...
commit dde6dcadcc248561aa223b6085b40d1c6e6a621b 2 parents 1c37725 + d1afe64
Aaron Quint authored March 30, 2012
14  lib/resque/plugins/status.rb
@@ -80,11 +80,17 @@ def create(options = {})
80 80
         end
81 81
 
82 82
         # Adds a job of type <tt>klass<tt> to the queue with <tt>options<tt>.
83  
-        # Returns the UUID of the job
  83
+        #
  84
+        # Returns the UUID of the job if the job was queued, or nil if the job was
  85
+        # rejected by a before_enqueue hook.
84 86
         def enqueue(klass, options = {})
85  
-          uuid = Resque::Plugins::Status::Hash.create :options => options
86  
-          Resque.enqueue(klass, uuid, options)
87  
-          uuid
  87
+          uuid = Resque::Plugins::Status::Hash.generate_uuid
  88
+          if Resque.enqueue(klass, uuid, options)
  89
+            Resque::Plugins::Status::Hash.create uuid, :options => options
  90
+            uuid
  91
+          else
  92
+            nil
  93
+          end
88 94
         end
89 95
 
90 96
         # This is the method called by Resque::Worker when processing jobs. It
3  lib/resque/plugins/status/hash.rb
@@ -11,8 +11,7 @@ class Hash < ::Hash
11 11
 
12 12
         # Create a status, generating a new UUID, passing the message to the status
13 13
         # Returns the UUID of the new status.
14  
-        def self.create(*messages)
15  
-          uuid = generate_uuid
  14
+        def self.create(uuid, *messages)
16 15
           set(uuid, *messages)
17 16
           redis.zadd(set_key, Time.now.to_i, uuid)
18 17
           redis.zremrangebyscore(set_key, 0, Time.now.to_i - @expire_in) if @expire_in
12  test/test_helper.rb
@@ -98,3 +98,15 @@ def perform
98 98
     failed("I'm such a failure")
99 99
   end
100 100
 end
  101
+
  102
+class NeverQueuedJob
  103
+  include Resque::Plugins::Status
  104
+
  105
+  def self.before_enqueue(*args)
  106
+    false
  107
+  end
  108
+
  109
+  def perform
  110
+    # will never get called
  111
+  end
  112
+end
20  test/test_resque_plugins_status.rb
@@ -32,6 +32,26 @@ class TestResquePluginsStatus < Test::Unit::TestCase
32 32
 
33 33
     end
34 34
 
  35
+    context ".create with a failing before_enqueue hook" do
  36
+      setup do
  37
+        @size = Resque.size(:statused)
  38
+        @status_ids_size = Resque::Plugins::Status::Hash.status_ids.length
  39
+        @res = NeverQueuedJob.create(:num => 100)
  40
+      end
  41
+
  42
+      should "return nil" do
  43
+        assert_equal nil, @res
  44
+      end
  45
+
  46
+      should "not create a status" do
  47
+        assert_equal @size, Resque.size(:statused)
  48
+      end
  49
+
  50
+      should "not add the uuid to the statuses" do
  51
+        assert_equal @status_ids_size, Resque::Plugins::Status::Hash.status_ids.length
  52
+      end
  53
+    end
  54
+
35 55
     context ".scheduled" do
36 56
       setup do
37 57
         @job_args = {'num' => 100}
18  test/test_resque_plugins_status_hash.rb
@@ -6,9 +6,9 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase
6 6
     setup do
7 7
       Resque.redis.flushall
8 8
       Resque::Plugins::Status::Hash.expire_in = nil
9  
-      @uuid = Resque::Plugins::Status::Hash.create
  9
+      @uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid)
10 10
       Resque::Plugins::Status::Hash.set(@uuid, "my status")
11  
-      @uuid_with_json = Resque::Plugins::Status::Hash.create({"im" => "json"})
  11
+      @uuid_with_json = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, {"im" => "json"})
12 12
     end
13 13
 
14 14
     context ".get" do
@@ -43,17 +43,17 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase
43 43
     context ".create" do
44 44
       should "add an item to a key set" do
45 45
         before = Resque::Plugins::Status::Hash.status_ids.length
46  
-        Resque::Plugins::Status::Hash.create
  46
+        Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid)
47 47
         after = Resque::Plugins::Status::Hash.status_ids.length
48 48
         assert_equal 1, after - before
49 49
       end
50 50
 
51 51
       should "return a uuid" do
52  
-       assert_match(/^\w{32}$/, Resque::Plugins::Status::Hash.create)
  52
+        assert_match(/^\w{32}$/, Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid))
53 53
       end
54 54
 
55 55
       should "store any status passed" do
56  
-        uuid = Resque::Plugins::Status::Hash.create("initial status")
  56
+        uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, "initial status")
57 57
         status = Resque::Plugins::Status::Hash.get(uuid)
58 58
         assert status.is_a?(Resque::Plugins::Status::Hash)
59 59
         assert_equal "initial status", status.message
@@ -61,17 +61,17 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase
61 61
 
62 62
       should "expire keys if expire_in is set" do
63 63
         Resque::Plugins::Status::Hash.expire_in = 1
64  
-        uuid = Resque::Plugins::Status::Hash.create("new status")
  64
+        uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, "new status")
65 65
         assert_contains Resque::Plugins::Status::Hash.status_ids, uuid
66 66
         assert_equal "new status", Resque::Plugins::Status::Hash.get(uuid).message
67 67
         sleep 2
68  
-        Resque::Plugins::Status::Hash.create
  68
+        Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid)
69 69
         assert_does_not_contain Resque::Plugins::Status::Hash.status_ids, uuid
70 70
         assert_nil Resque::Plugins::Status::Hash.get(uuid)
71 71
       end
72 72
 
73 73
       should "store the options for the job created" do
74  
-        uuid = Resque::Plugins::Status::Hash.create("new", :options => {'test' => '123'})
  74
+        uuid = Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid, "new", :options => {'test' => '123'})
75 75
         assert uuid
76 76
         status = Resque::Plugins::Status::Hash.get(uuid)
77 77
         assert status.is_a?(Resque::Plugins::Status::Hash)
@@ -98,7 +98,7 @@ class TestResquePluginsStatusHash < Test::Unit::TestCase
98 98
 
99 99
       setup do
100 100
         @uuids = []
101  
-        30.times{ Resque::Plugins::Status::Hash.create }
  101
+        30.times{ Resque::Plugins::Status::Hash.create(Resque::Plugins::Status::Hash.generate_uuid) }
102 102
       end
103 103
 
104 104
       should "return an array of job ids" do

1 note on commit dde6dca

I guess this pull request explains/fixes issue #42

Please sign in to comment.
Something went wrong with that request. Please try again.