Class: RedisGetlock
- Inherits:
-
Object
- Object
- RedisGetlock
- Defined in:
- lib/redis_getlock.rb,
lib/redis_getlock/version.rb
Defined Under Namespace
Classes: LockError
Constant Summary collapse
- TIMEOUT =
-1
- EXPIRE =
2
- INTERVAL =
1
- VERSION =
"0.3.1"
Instance Attribute Summary collapse
-
#expire ⇒ Object
readonly
Returns the value of attribute expire.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Instance Method Summary collapse
-
#initialize(redis:, key:, logger: nil, timeout: TIMEOUT, expire: EXPIRE, interval: INTERVAL) ⇒ RedisGetlock
constructor
A new instance of RedisGetlock.
- #lock ⇒ Object
- #locked? ⇒ Boolean
- #self_locked? ⇒ Boolean
- #synchronize(&block) ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize(redis:, key:, logger: nil, timeout: TIMEOUT, expire: EXPIRE, interval: INTERVAL) ⇒ RedisGetlock
Returns a new instance of RedisGetlock.
15 16 17 18 19 20 21 22 23 |
# File 'lib/redis_getlock.rb', line 15 def initialize(redis:, key:, logger: nil, timeout: TIMEOUT, expire: EXPIRE, interval: INTERVAL) @redis = redis @key = key @logger = logger @timeout = timeout @expire = expire @interval = interval @uuid = SecureRandom.uuid end |
Instance Attribute Details
#expire ⇒ Object (readonly)
Returns the value of attribute expire.
7 8 9 |
# File 'lib/redis_getlock.rb', line 7 def expire @expire end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
7 8 9 |
# File 'lib/redis_getlock.rb', line 7 def interval @interval end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/redis_getlock.rb', line 7 def key @key end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/redis_getlock.rb', line 7 def logger @logger end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
7 8 9 |
# File 'lib/redis_getlock.rb', line 7 def redis @redis end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/redis_getlock.rb', line 7 def timeout @timeout end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
7 8 9 |
# File 'lib/redis_getlock.rb', line 7 def uuid @uuid end |
Instance Method Details
#lock ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/redis_getlock.rb', line 25 def lock logger.info { "#{log_head}Wait #{timeout < 0 ? '' : "#{timeout} sec "}to acquire a mysql lock '#{key}'" } if logger if locked = else locked = end @thr.terminate if @thr and @thr.alive? if locked @thr = Thread.new(&method(:keeplock)) logger.info { "#{log_head}Acquired a redis lock '#{key}'" } if logger true else logger.info { "#{log_head}Timeout to acquire a redis lock '#{key}'" } if logger false end end |
#locked? ⇒ Boolean
58 59 60 |
# File 'lib/redis_getlock.rb', line 58 def locked? redis.exists(key) end |
#self_locked? ⇒ Boolean
62 63 64 |
# File 'lib/redis_getlock.rb', line 62 def self_locked? locked? && uuid == JSON.parse(redis.get(key))['uuid'] end |
#synchronize(&block) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/redis_getlock.rb', line 66 def synchronize(&block) raise LockError unless lock begin return yield ensure unlock end end |
#unlock ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/redis_getlock.rb', line 43 def unlock @thr.terminate if @thr and @thr.alive? if self_locked? redis.del(key) logger.info { "#{log_head}Released a redis lock '#{key}'" } if logger true elsif locked? logger.info { "#{log_head}Failed to release a redis lock since somebody else locked '#{key}'" } if logger false else logger.info { "#{log_head}Redis lock did not exist '#{key}'" } if logger true end end |