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

Returns:

  • (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



33
34
35
36
37
38
39
40
41
# File 'lib/periodically/locks.rb', line 33

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_allObject



43
44
45
46
47
48
# File 'lib/periodically/locks.rb', line 43

def self.reset_all
  Periodically.redis do |conn|
    keys = conn.keys("locks:*")
    conn.del(keys)
  end
end

.unlock(key) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/periodically/locks.rb', line 25

def self.unlock(key)
  lkey = lock_key(key)

  Periodically.redis do |conn|
    conn.del(lkey)
  end
end