Class: Travis::Lock::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/lock/redis.rb

Defined Under Namespace

Classes: LockError

Constant Summary collapse

DEFAULTS =
{
  ttl:      5 * 60 * 1000,
  retries:  5,
  interval: 0.1,
  timeout:  0.5
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config) ⇒ Redis

Returns a new instance of Redis.



32
33
34
35
36
37
# File 'lib/travis/lock/redis.rb', line 32

def initialize(name, config)
  @name    = name
  @config  = DEFAULTS.merge(config)
  @retried = 0
  @monitor = Monitor.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



30
31
32
# File 'lib/travis/lock/redis.rb', line 30

def config
  @config
end

#monitorObject (readonly)

Returns the value of attribute monitor.



30
31
32
# File 'lib/travis/lock/redis.rb', line 30

def monitor
  @monitor
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/travis/lock/redis.rb', line 30

def name
  @name
end

#retriedObject (readonly)

Returns the value of attribute retried.



30
31
32
# File 'lib/travis/lock/redis.rb', line 30

def retried
  @retried
end

Class Method Details

.clientsObject



19
20
21
# File 'lib/travis/lock/redis.rb', line 19

def self.clients
  @clients ||= {}
end

Instance Method Details

#exclusiveObject



39
40
41
42
43
44
45
# 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