Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Rework UniqueJob as module

In order to have flexiblity implement module Resque::Plugins::UniqueJob
instead of old class Resque::Plugins::Loner::UniqueJob remained with deprecation
warning.
  • Loading branch information...
commit 2ada35a8a080cc2f2540ecf83a8461e4b0b2df0b 1 parent 376fb3c
Bogdan Gusiev authored March 19, 2011
2  README.markdown
Source Rendered
@@ -95,7 +95,7 @@ The last part of this key is the job's ID, which is pretty much your queue item'
95 95
 
96 96
     { 'class': 'CacheSweeper', 'args': [1] }`
97 97
 
98  
-The default method to create a job ID from these parameters  is to do some normalization on the payload and then md5'ing it (defined in `Resque::Plugins::Loner::UniqueJob#redis_key`).
  98
+The default method to create a job ID from these parameters  is to do some normalization on the payload and then md5'ing it (defined in `Resque::Plugins::UniqueJob#redis_key`).
99 99
 
100 100
 You could also use the whole payload or anything else as a redis key, as long as you make sure these requirements are met:
101 101
 
4  lib/resque-loner/helpers.rb
@@ -28,7 +28,7 @@ def self.unique_job_queue_key(queue, item)
28 28
         def self.item_is_a_unique_job?(item)
29 29
           begin
30 30
             klass = constantize(item[:class] || item["class"])
31  
-            klass.ancestors.include?(::Resque::Plugins::Loner::UniqueJob)
  31
+            klass.included_modules.include?(::Resque::Plugins::UniqueJob)
32 32
           rescue
33 33
             false # Resque testsuite also submits strings as job classes while Resque.enqueue'ing,
34 34
           end     # so resque-loner should not start throwing up when that happens.
@@ -57,4 +57,4 @@ def self.cleanup_loners(queue)
57 57
       end
58 58
     end
59 59
   end
60  
-end
  60
+end
39  lib/resque-loner/unique_job.rb
... ...
@@ -1,27 +1,54 @@
1 1
 require 'digest/md5'
2 2
 
3 3
 #
4  
-#  If you want your job to be unique, subclass it from this class. If you wish,
  4
+#  If you want your job to be unique, include this module in it. If you wish,
5 5
 #  you can overwrite this implementation of redis_key to fit your needs
6 6
 #
7 7
 module Resque
8 8
   module Plugins
9  
-    module Loner
10  
-      class UniqueJob
11  
-        extend Resque::Helpers
  9
+    module UniqueJob
  10
+    
  11
+      def self.included(base)
  12
+        base.extend         ClassMethods
  13
+        base.class_eval do
  14
+          base.send(:extend, Resque::Helpers)
  15
+        end
  16
+      end # self.included
  17
+    
  18
+      module ClassMethods
  19
+    
12 20
 
13 21
         #
14 22
         #  Payload is what Resque stored for this job along with the job's class name.
15 23
         #  On a Resque with no plugins installed, this is a hash containing :class and :args
16 24
         #
17  
-        def self.redis_key(payload)
  25
+        def redis_key(payload)
18 26
           payload = decode(encode(payload)) # This is the cycle the data goes when being enqueued/dequeued
19 27
           job  = payload[:class] || payload["class"]
20 28
           args = payload[:args]  || payload["args"]
21 29
           digest = Digest::MD5.hexdigest encode(:class => job, :args => args)
22 30
           digest
23 31
         end
  32
+      end # ClassMethods
  33
+    
  34
+    
  35
+    end
  36
+  end
  37
+end
  38
+module Resque
  39
+  module Plugins
  40
+    module Loner
  41
+      class UniqueJob
  42
+        
  43
+        include Resque::Plugins::UniqueJob
  44
+
  45
+        def self.inherited(host)
  46
+          super(host)
  47
+          return  if @__unique_job_warned
  48
+          warn "Inherit Resque::Plugins::Loner::UniqueJob is deprecated. Include Resque::Plugins::UniqueJob module instead."
  49
+          @__unique_job_warned = true
  50
+        end
24 51
       end
25 52
     end
26 53
   end
27  
-end
  54
+end
5  resque-loner.gemspec
@@ -25,7 +25,10 @@ Gem::Specification.new do |s|
25 25
 Makes sure that for special jobs, there can be only one job with the same workload in one queue.
26 26
 
27 27
 Example:
28  
-    class CacheSweeper < Resque::Plugins::Loner::UniqueJob
  28
+    class CacheSweeper 
  29
+    
  30
+       include Resque::Plugins::UniqueJob
  31
+
29 32
        @queue = :cache_sweeps
30 33
     
31 34
        def self.perform(article_id)
20  spec/loner_spec.rb
@@ -11,18 +11,28 @@ class SomeJob
11 11
   @queue = :some_queue
12 12
 end
13 13
 
14  
-class SomeUniqueJob < Resque::Plugins::Loner::UniqueJob
  14
+class SomeUniqueJob 
  15
+  
  16
+  include Resque::Plugins::UniqueJob
  17
+
15 18
   @queue = :other_queue
16 19
   def self.perform(foo); end
17 20
 end
18 21
 
19  
-class FailingUniqueJob < Resque::Plugins::Loner::UniqueJob
  22
+class FailingUniqueJob
  23
+  include Resque::Plugins::UniqueJob
20 24
   @queue = :other_queue
21 25
   def self.perform(foo)
22 26
     raise "I beg to differ"
23 27
   end
24 28
 end
25 29
 
  30
+class DeprecatedUniqueJob < Resque::Plugins::Loner::UniqueJob
  31
+
  32
+  @queue = :other_queue
  33
+  def self.perform(foo); end
  34
+end
  35
+
26 36
 describe "Resque" do
27 37
 
28 38
   before(:each) do
@@ -43,6 +53,12 @@ def self.perform(foo)
43 53
       Resque.enqueue SomeUniqueJob, "foo"
44 54
       Resque.size(:other_queue).should == 1
45 55
     end
  56
+
  57
+    it "should support deprecated Resque::Plugins::Loner::UniqueJob class" do
  58
+      Resque.enqueue DeprecatedUniqueJob, "foo"
  59
+      Resque.enqueue DeprecatedUniqueJob, "foo"
  60
+      Resque.size(:other_queue).should == 1
  61
+    end
46 62
   
47 63
     it "should allow the same jobs to be executed one after the other" do
48 64
       Resque.enqueue SomeUniqueJob, "foo"

0 notes on commit 2ada35a

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