Class: RedisGetlock

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#expireObject (readonly)

Returns the value of attribute expire.



7
8
9
# File 'lib/redis_getlock.rb', line 7

def expire
  @expire
end

#intervalObject (readonly)

Returns the value of attribute interval.



7
8
9
# File 'lib/redis_getlock.rb', line 7

def interval
  @interval
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/redis_getlock.rb', line 7

def key
  @key
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/redis_getlock.rb', line 7

def logger
  @logger
end

#redisObject (readonly)

Returns the value of attribute redis.



7
8
9
# File 'lib/redis_getlock.rb', line 7

def redis
  @redis
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



7
8
9
# File 'lib/redis_getlock.rb', line 7

def timeout
  @timeout
end

#uuidObject (readonly)

Returns the value of attribute uuid.



7
8
9
# File 'lib/redis_getlock.rb', line 7

def uuid
  @uuid
end

Instance Method Details

#lockObject



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 set_options_available?
    locked = lock_with_set_options
  else
    locked = lock_without_set_options
  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

Returns:

  • (Boolean)


58
59
60
# File 'lib/redis_getlock.rb', line 58

def locked?
  redis.exists(key)
end

#self_locked?Boolean

Returns:

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

Raises:



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

#unlockObject



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