Class: Specbandit::RedisQueue
- Inherits:
-
Object
- Object
- Specbandit::RedisQueue
- Defined in:
- lib/specbandit/redis_queue.rb
Constant Summary collapse
- MAX_ATTEMPTS =
3
Instance Attribute Summary collapse
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(redis_url: Specbandit.configuration.redis_url) ⇒ RedisQueue
constructor
A new instance of RedisQueue.
-
#length(key) ⇒ Object
Returns the current length of the queue.
-
#push(key, files, ttl: nil) ⇒ Object
Push file paths onto the queue and set an expiry on the key.
-
#read_all(key) ⇒ Object
Read all file paths from the list non-destructively.
-
#steal(key, count) ⇒ Object
Atomically steal up to ‘count` file paths from the queue.
Constructor Details
#initialize(redis_url: Specbandit.configuration.redis_url) ⇒ RedisQueue
Returns a new instance of RedisQueue.
9 10 11 |
# File 'lib/specbandit/redis_queue.rb', line 9 def initialize(redis_url: Specbandit.configuration.redis_url) @redis = Redis.new(url: redis_url) end |
Instance Attribute Details
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
7 8 9 |
# File 'lib/specbandit/redis_queue.rb', line 7 def redis @redis end |
Instance Method Details
#close ⇒ Object
54 55 56 |
# File 'lib/specbandit/redis_queue.rb', line 54 def close redis.close end |
#length(key) ⇒ Object
Returns the current length of the queue.
44 45 46 |
# File 'lib/specbandit/redis_queue.rb', line 44 def length(key) with_retries { redis.llen(key) } end |
#push(key, files, ttl: nil) ⇒ Object
Push file paths onto the queue and set an expiry on the key. Returns the new length of the list.
17 18 19 20 21 22 23 24 25 |
# File 'lib/specbandit/redis_queue.rb', line 17 def push(key, files, ttl: nil) return 0 if files.empty? with_retries do count = redis.rpush(key, files) redis.expire(key, ttl) if ttl count end end |
#read_all(key) ⇒ Object
Read all file paths from the list non-destructively. Returns an array of file paths (empty array when key doesn’t exist).
50 51 52 |
# File 'lib/specbandit/redis_queue.rb', line 50 def read_all(key) with_retries { redis.lrange(key, 0, -1) } end |
#steal(key, count) ⇒ Object
Atomically steal up to ‘count` file paths from the queue. Returns an array of file paths (empty array when exhausted).
Uses LPOP with count argument (Redis 6.2+).
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/specbandit/redis_queue.rb', line 31 def steal(key, count) result = with_retries { redis.lpop(key, count) } # LPOP returns nil when the key doesn't exist or list is empty, # and returns a single string (not array) when count is 1 on some versions. case result when nil then [] when String then [result] else Array(result) end end |