Class: ROC::Lock
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #lock(expires_time) ⇒ Object
- #locked? ⇒ Boolean
- #locking_if_necessary(expires_time) ⇒ Object
- #unlock ⇒ Object
- #when_locked(expires_time, poll_ms = 100) ⇒ Object
Methods inherited from Time
#deserialize, #localtime, #serialize, #to_s, #to_time
Methods included from Types::ScalarType
#clobber, #deserialize, #inspect, #serialize, #setex
Methods included from Types::MethodGenerators
#deserializing_method, #nonserializing_method, #serializing_and_deserializing_method, #serializing_method, #zero_arg_method
Methods inherited from Base
#clobber, delegate_methods, #initialize, #method_missing, #respond_to?, #seed
Methods included from Types::AllTypes
Constructor Details
This class inherits a constructor from ROC::Base
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ROC::Base
Instance Method Details
#lock(expires_time) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/roc/objects/lock.rb', line 4 def lock(expires_time) aquired_lock = false if self.setnx(expires_time) aquired_lock = true else locked_until = self.value if locked_until.nil? || (locked_until < ::Time.now) ##ttl of 0 is not yet expired # only say we got the lock if we manage to update it first if self.getset(expires_time) == locked_until aquired_lock = true end end end aquired_lock end |
#locked? ⇒ Boolean
20 21 22 23 |
# File 'lib/roc/objects/lock.rb', line 20 def locked? locked_until = self.value !locked_until.nil? && (locked_until >= ::Time.now) ##ttl of 0 is not yet expired end |
#locking_if_necessary(expires_time) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/roc/objects/lock.rb', line 40 def locking_if_necessary(expires_time) obtained_lock = self.lock(expires_time) begin yield ensure if obtained_lock self.unlock end end end |
#unlock ⇒ Object
25 26 27 |
# File 'lib/roc/objects/lock.rb', line 25 def unlock self.forget end |
#when_locked(expires_time, poll_ms = 100) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/roc/objects/lock.rb', line 29 def when_locked(expires_time, poll_ms=100) until self.lock(expires_time) sleep(poll_ms.to_f / 1000) end begin yield ensure self.unlock end end |