Module: Keepit::Locker
- Defined in:
- lib/keepit/locker.rb
Constant Summary collapse
- GLOBAL =
"global".freeze
- KEY =
"keepit:locks".freeze
- EXPIRES_IN =
60
Class Method Summary collapse
- .lock(resources, blocking = false) ⇒ Object
- .locked?(resource, check_global: true) ⇒ Boolean
- .locked_resources ⇒ Object
- .unlock(resources, blocking = false) ⇒ Object
Class Method Details
.lock(resources, blocking = false) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/keepit/locker.rb', line 7 def self.lock(resources, blocking = false) result = ::Keepit.redis.sadd(KEY, resources) sleep(EXPIRES_IN) if blocking result end |
.locked?(resource, check_global: true) ⇒ Boolean
21 22 23 24 25 26 27 |
# File 'lib/keepit/locker.rb', line 21 def self.locked?(resource, check_global: true) if check_global locked_resources.any? { |r| r == resource || r == GLOBAL } else locked_resources.any? { |r| r == resource } end end |
.locked_resources ⇒ Object
29 30 31 32 33 |
# File 'lib/keepit/locker.rb', line 29 def self.locked_resources ::Keepit::TransientStore.fetch(:locks, expires_in: EXPIRES_IN) do Array(::Keepit.redis.smembers(KEY)) end end |
.unlock(resources, blocking = false) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/keepit/locker.rb', line 14 def self.unlock(resources, blocking = false) result = ::Keepit.redis.srem(KEY, resources) sleep(EXPIRES_IN) if blocking result end |