class Travis::Lock::Redis

Constants

DEFAULTS

Attributes

config[R]
monitor[R]
name[R]
retried[R]

Public Class Methods

clients() click to toggle source
# File lib/travis/lock/redis.rb, line 19
def self.clients
  @clients ||= {}
end
new(name, config) click to toggle source
# File lib/travis/lock/redis.rb, line 32
def initialize(name, config)
  @name    = name
  @config  = DEFAULTS.merge(config)
  @retried = 0
  @monitor = Monitor.new
end

Public Instance Methods

exclusive() { |lock| ... } click to toggle source
# File lib/travis/lock/redis.rb, line 39
def exclusive
  retrying do
    client.lock(name, config[:ttl]) do |lock|
      lock ? yield(lock) : raise(LockError.new(name))
    end
  end
end

Private Instance Methods

client() click to toggle source
# File lib/travis/lock/redis.rb, line 49
def client
  monitor.synchronize do
    self.class.clients[url] ||= Redlock::Client.new([url], redis_timeout: config[:timeout])
  end
end
retrying() { || ... } click to toggle source
# File lib/travis/lock/redis.rb, line 59
def retrying
  yield
rescue LockError
  raise if retried.to_i >= config[:retries]
  sleep config[:interval]
  @retries = retried + 1
  retry
end
url() click to toggle source
# File lib/travis/lock/redis.rb, line 55
def url
  config[:url] || fail("No Redis URL specified")
end