Class: Sqeduler::RedisLock

Inherits:
Object
  • Object
show all
Includes:
RedisScripts
Defined in:
lib/sqeduler/redis_lock.rb

Overview

Uses eval_sha to execute server-side scripts on redis. Avoids some of the potentially racey and brittle dependencies on Time-based redis locks in other locking libraries.

Direct Known Subclasses

TriggerLock

Defined Under Namespace

Classes: LockTimeoutError

Constant Summary collapse

SLEEP_TIME =
0.1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RedisScripts

#refresh_lock, #release_lock

Constructor Details

#initialize(key, options = {}) ⇒ RedisLock

Returns a new instance of RedisLock.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
# File 'lib/sqeduler/redis_lock.rb', line 14

def initialize(key, options = {})
  @key = key
  @expiration = options[:expiration]
  raise ArgumentError, "Expiration must be provided!" unless @expiration

  @timeout = options[:timeout] || 5
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



12
13
14
# File 'lib/sqeduler/redis_lock.rb', line 12

def key
  @key
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



12
13
14
# File 'lib/sqeduler/redis_lock.rb', line 12

def timeout
  @timeout
end

Class Method Details

.with_lock(key, options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sqeduler/redis_lock.rb', line 69

def self.with_lock(key, options)
  raise "Block is required" unless block_given?

  mutex = new(key, options)
  unless mutex.lock
    raise LockTimeoutError, "Timed out trying to get #{key} lock. Exceeded #{mutex.timeout} sec"
  end

  begin
    yield
  ensure
    mutex.unlock
  end
end

Instance Method Details

#expiration_millisecondsObject



84
85
86
87
# File 'lib/sqeduler/redis_lock.rb', line 84

def expiration_milliseconds
  # expiration needs to be an integer
  @expiration ? (@expiration * 1000).to_i : 0
end

#lockObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sqeduler/redis_lock.rb', line 22

def lock
  Service.logger.info(
    "Try to acquire lock with #{key}, expiration: #{@expiration} sec, timeout: #{timeout} sec"
  )
  return true if locked?

  if poll_for_lock
    Service.logger.info "Acquired lock #{key} with value #{lock_value}"
    true
  else
    Service.logger.info "Failed to acquire lock #{key} with value #{lock_value}"
    false
  end
end

#locked?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sqeduler/redis_lock.rb', line 57

def locked?
  redis_pool.with do |redis|
    if redis.get(key) == lock_value
      Service.logger.info "Lock #{key} with value #{lock_value} is valid"
      true
    else
      Service.logger.info "Lock #{key} with value #{lock_value} has expired or is not present"
      false
    end
  end
end

#refreshObject



47
48
49
50
51
52
53
54
55
# File 'lib/sqeduler/redis_lock.rb', line 47

def refresh
  if refresh_lock
    Service.logger.info "Refreshed lock #{key} with value #{lock_value}"
    true
  else
    Service.logger.info "Cannot refresh lock #{key} with value #{lock_value}"
    false
  end
end

#unlockObject



37
38
39
40
41
42
43
44
45
# File 'lib/sqeduler/redis_lock.rb', line 37

def unlock
  if release_lock
    Service.logger.info "Released lock #{key} with value #{lock_value}"
    true
  else
    Service.logger.info "Cannot release lock #{key} with value #{lock_value}"
    false
  end
end