public
Fork of tobi/delayed_job
Description: Database based asynchronously priority queue system -- Extracted from Shopify
Homepage: http://www.shopify.com
Clone URL: git://github.com/smn/delayed_job.git
adding look ahead filter to make sure jobs are not scheduled twice, this 
will be very expensive unless you have unique constraints setup in your db 
or have your indexes setup on the appropriate columns
smn (author)
Tue Jun 03 07:34:00 -0700 2008
commit  96eb4b26fb6531130aba890208be2fb39989d398
tree    f34d18487d9d27d8d2b4b731e9430677206edcda
parent  a79e8e2d70cbab2e99fceddf61dd1b1c58ded6ca
...
51
52
53
 
 
54
55
56
 
 
 
57
58
59
...
51
52
53
54
55
56
 
 
57
58
59
60
61
62
0
@@ -51,9 +51,12 @@ module Delayed
0
       unless object.respond_to?(:perform)
0
         raise ArgumentError, 'Cannot enqueue items which do not respond to perform'
0
       end
0
+ Job.create(:payload_object => object, :priority => priority)
0
+ end
0
     
0
- Job.create(:payload_object => object, :priority => priority)
0
- end
0
+ def self.enqueue_unique(object, priority = 0)
0
+ self.enqueue(object, priority) if Job.count( :conditions => ["handler = :h", { :h => object.to_yaml }] ).zero?
0
+ end
0
     
0
     def self.find_available(limit = 5)
0
       time_now = db_time_now
...
1
2
3
 
4
5
 
 
 
 
6
7
8
...
1
2
 
3
4
5
6
7
8
9
10
11
12
0
@@ -1,7 +1,11 @@
0
 module Delayed
0
   module MessageSending
0
- def send_later(method, *args)
0
+ def send_later(method, *args)
0
       Delayed::Job.enqueue Delayed::PerformableMethod.new(self, method.to_sym, args)
0
     end
0
+
0
+ def send_later_unique(method, *args)
0
+ Delayed::Job.enqueue_unique Delayed::PerformableMethod.new(self, method.to_sym, args)
0
+ end
0
   end
0
 end
0
\ No newline at end of file
...
53
54
55
56
 
 
 
 
 
57
58
59
...
67
68
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
71
72
...
53
54
55
 
56
57
58
59
60
61
62
63
...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
0
@@ -53,7 +53,11 @@ describe 'random ruby objects' do
0
                                            
0
     RandomRubyObject.new.respond_to?(:send_later)
0
     
0
- end
0
+ end
0
+
0
+ it "should respond_to :send_later_unique method" do
0
+ RandomRubyObject.new.respond_to?(:send_later_unique)
0
+ end
0
   
0
   it "should raise a ArgumentError if send_later is called but the target method doesn't exist" do
0
     lambda { RandomRubyObject.new.send_later(:method_that_deos_not_exist) }.should raise_error(NoMethodError)
0
@@ -67,6 +71,23 @@ describe 'random ruby objects' do
0
     Delayed::Job.count.should == 1
0
   end
0
   
0
+ it "should add a new entry to the job table when send_later_unique is called on it" do
0
+ Delayed::Job.count.should == 0
0
+
0
+ RandomRubyObject.new.send_later_unique(:to_s)
0
+
0
+ Delayed::Job.count.should == 1
0
+ end
0
+
0
+ it "should not add a new entry to the job table when send_later_unique is called for an already scheduled job" do
0
+ Delayed::Job.count.should == 0
0
+
0
+ RandomRubyObject.new.send_later_unique(:to_s)
0
+ RandomRubyObject.new.send_later_unique(:to_s)
0
+
0
+ Delayed::Job.count.should == 1
0
+ end
0
+
0
   it "should run get the original method executed when the job is performed" do
0
     
0
     RandomRubyObject.new.send_later(:say_hello)
...
29
30
31
 
 
 
 
 
 
32
33
34
...
29
30
31
32
33
34
35
36
37
38
39
40
0
@@ -29,6 +29,12 @@ describe Delayed::Job do
0
     Delayed::Job.count.should == 1
0
   end
0
   
0
+ it "should not increase count after enqueing items that already exist in the queue" do
0
+ Delayed::Job.enqueue_unique SimpleJob.new
0
+ Delayed::Job.enqueue_unique SimpleJob.new
0
+ Delayed::Job.count.should == 1
0
+ end
0
+
0
   it "should call perform on jobs when running work_off" do
0
     SimpleJob.runs.should == 0
0
         

Comments

    No one has commented yet.