Module: Periodically::Locks
- Defined in:
- lib/periodically/locks.rb
Class Method Summary collapse
Class Method Details
.lock(key, seconds) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/periodically/locks.rb', line 14 def self.lock(key, seconds) lkey = lock_key(key) Periodically.redis do |conn| conn.multi do |multi| multi.set(lkey, "1") multi.expire(lkey, seconds) end end end |
.locked?(*keys) ⇒ Boolean
5 6 7 8 9 10 11 12 |
# File 'lib/periodically/locks.rb', line 5 def self.locked?(*keys) return false if keys.empty? lkeys = keys.map { |key| lock_key(key) } Periodically.redis do |conn| lkeys.any? { |lkey| conn.exists(lkey) } end end |
.remaining(key) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/periodically/locks.rb', line 25 def self.remaining(key) lkey = lock_key(key) Periodically.redis do |conn| remaining = conn.ttl(lkey) return nil if remaining == -1 remaining end end |
.reset_all ⇒ Object
35 36 37 38 39 40 |
# File 'lib/periodically/locks.rb', line 35 def self.reset_all Periodically.redis do |conn| keys = conn.keys("locks:*") conn.del(keys) end end |