Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

changed locking strategy

  • Loading branch information...
commit fc5825295b6c95932e467bdfcba008d25348728a 1 parent 18f8caf
Alexander Gräfenstein authored
Showing with 9 additions and 7 deletions.
  1. +5 −3 app/models/cronjobber/task.rb
  2. +4 −4 spec/cronjobber_spec.rb
View
8 app/models/cronjobber/task.rb
@@ -22,7 +22,9 @@ def self.cronjob_enqueue(key=nil)
end
def self.cronjob_perform
- unless cronjob.lock!
+ key = DateTime.now.to_i
+
+ unless cronjob.lock!(key)
cronjob.status = "locked"
return cronjob
end
@@ -34,7 +36,7 @@ def self.cronjob_perform
end
if self.cronjob_delayed
- self.cronjob_enqueue(cronjob.locking_key)
+ self.cronjob_enqueue(key)
cronjob.status = "enqueued"
cronjob.last_error = nil
cronjob.save!
@@ -74,7 +76,7 @@ def locked?(key=nil)
def lock!(key=nil)
return false if self.locked?
- return self.update_attributes!(:locked_at => DateTime.now, :locking_key => Time.now.to_i)
+ return self.update_attributes!(:locked_at => DateTime.now, :locking_key => key)
end
def unlock! exception=nil
View
8 spec/cronjobber_spec.rb
@@ -100,13 +100,13 @@ def self.cronjob_enqueue(key=nil)
end
it "should be locked for invalid key" do
- @job.lock!.should be true
- @job.locked?("123456").should be true
+ @job.lock!("123456").should be true
+ @job.locked?("654321").should be true
end
it "should not be locked for valid key" do
- @job.lock!.should be true
- @job.locked?(@job.locking_key).should be false
+ @job.lock!("123456").should be true
+ @job.locked?("123456").should be false
end
it "should perform the job" do
Please sign in to comment.
Something went wrong with that request. Please try again.