Class: EasyRedisLock::GateKeeper

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_redis_lock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GateKeeper

Returns a new instance of GateKeeper.



9
10
11
12
13
# File 'lib/easy_redis_lock.rb', line 9

def initialize options={}
  @delay = options.fetch(:delay, 1500)
  @lock_time = options.fetch(:lock_time, 30) # seconds
  @redis = options.fetch(:redis) { Redis.new }
end

Instance Attribute Details

#delayObject (readonly)

Returns the value of attribute delay.



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

def delay
  @delay
end

#lock_timeObject (readonly)

Returns the value of attribute lock_time.



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

def lock_time
  @lock_time
end

#redisObject (readonly)

Returns the value of attribute redis.



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

def redis
  @redis
end

Instance Method Details

#cleanupObject



38
39
40
# File 'lib/easy_redis_lock.rb', line 38

def cleanup
  close_connection!
end

#mark_in_progress(delay_key) ⇒ Object



19
20
21
# File 'lib/easy_redis_lock.rb', line 19

def mark_in_progress delay_key
  set_progress delay_key
end

#should_delay?(delay_key) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/easy_redis_lock.rb', line 15

def should_delay? delay_key
  in_progress? delay_key
end

#with_lock(delay_key = Time.now.to_i, delay = seconds_delay, &block) ⇒ Object



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

def with_lock delay_key=Time.now.to_i, delay=seconds_delay, &block
  retries = 0
  begin
    while should_delay?(delay_key) do
      sleep(delay)
      retries += 1
      break if retries >= 30
    end
    mark_in_progress(delay_key)
    yield if block_given?
  ensure
    expire_lock(delay_key)
  end
end