Class: RedisLocker
- Inherits:
-
Object
- Object
- RedisLocker
- Defined in:
- lib/redis-locker.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#running ⇒ Object
readonly
Returns the value of attribute running.
-
#time_limit ⇒ Object
readonly
Returns the value of attribute time_limit.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#timestamp_key ⇒ Object
readonly
Returns the value of attribute timestamp_key.
Class Method Summary collapse
Instance Method Summary collapse
- #current? ⇒ true, false
-
#enter_queue ⇒ Object
Puts running block information in Redis This information will be used to place running block in a specific position of its queue.
-
#exit_queue ⇒ Object
Clears all data from queue related to this block.
-
#get_ready ⇒ true, false
Returns true if block is ready to run.
-
#initialize(key, time_limit = 5) ⇒ RedisLocker
constructor
A new instance of RedisLocker.
- #ready? ⇒ Boolean
- #redis ⇒ Object
-
#run(&block) ⇒ Object
Waits for the queue and evaluates the block.
- #run!(time_limit = @time_limit, clear_queue_on_timeout = false, &block) ⇒ Object
- #running? ⇒ Boolean
Constructor Details
#initialize(key, time_limit = 5) ⇒ RedisLocker
Returns a new instance of RedisLocker.
11 12 13 14 15 |
# File 'lib/redis-locker.rb', line 11 def initialize(key, time_limit = 5) @key = key @time_limit = time_limit @running = false end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/redis-locker.rb', line 7 def key @key end |
#running ⇒ Object (readonly)
Returns the value of attribute running.
7 8 9 |
# File 'lib/redis-locker.rb', line 7 def running @running end |
#time_limit ⇒ Object (readonly)
Returns the value of attribute time_limit.
7 8 9 |
# File 'lib/redis-locker.rb', line 7 def time_limit @time_limit end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
7 8 9 |
# File 'lib/redis-locker.rb', line 7 def @timestamp end |
#timestamp_key ⇒ Object (readonly)
Returns the value of attribute timestamp_key.
7 8 9 |
# File 'lib/redis-locker.rb', line 7 def @timestamp_key end |
Class Method Details
.logger ⇒ Object
98 99 100 |
# File 'lib/redis-locker.rb', line 98 def self.logger @logger ||= Logger.new(STDOUT) end |
.logger=(logger) ⇒ Object
102 103 104 |
# File 'lib/redis-locker.rb', line 102 def self.logger=(logger) @logger = logger end |
.redis ⇒ Object
106 107 108 |
# File 'lib/redis-locker.rb', line 106 def self.redis @redis end |
.redis=(adapter) ⇒ Object
110 111 112 |
# File 'lib/redis-locker.rb', line 110 def self.redis=(adapter) @redis = adapter end |
Instance Method Details
#current? ⇒ true, false
18 19 20 |
# File 'lib/redis-locker.rb', line 18 def current? == end |
#enter_queue ⇒ Object
Puts running block information in Redis This information will be used to place running block in a specific position of its queue
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/redis-locker.rb', line 24 def enter_queue logger.info("Entering #@key") raise 'This block is already in the queue' if running? @running = true self. = .to_s redis.set , '1' redis.expire , time_limit redis.rpush key, end |
#exit_queue ⇒ Object
Clears all data from queue related to this block
37 38 39 40 41 42 |
# File 'lib/redis-locker.rb', line 37 def exit_queue logger.info("Leaving #@key") redis.del redis.lrem key, 1, @running = false end |
#get_ready ⇒ true, false
Returns true if block is ready to run
46 47 48 49 50 51 52 53 |
# File 'lib/redis-locker.rb', line 46 def get_ready if ready? .nil? ? start_queue : make_current true else current? end end |
#ready? ⇒ Boolean
55 56 57 58 59 |
# File 'lib/redis-locker.rb', line 55 def ready? .nil? || current? || ( - .to_f >= time_limit) || redis.get(()).nil? end |
#redis ⇒ Object
61 62 63 |
# File 'lib/redis-locker.rb', line 61 def redis self.class.redis end |
#run(&block) ⇒ Object
Waits for the queue and evaluates the block
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/redis-locker.rb', line 66 def run(&block) logger.info("Running queue #@key") enter_queue wait begin block.call ensure exit_queue end end |
#run!(time_limit = @time_limit, clear_queue_on_timeout = false, &block) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/redis-locker.rb', line 81 def run!(time_limit = @time_limit, clear_queue_on_timeout = false, &block) Timeout::timeout(time_limit) { run(&block) } rescue Timeout::Error => error logger.error("Failed by timeout #{time_limit}s on #@key") if clear_queue_on_timeout logger.info("Clearing queue #@key") clear_queue end raise error end |
#running? ⇒ Boolean
94 95 96 |
# File 'lib/redis-locker.rb', line 94 def running? @running end |