Class: Specbandit::RedisQueue

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#redisObject (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

#closeObject



50
51
52
# File 'lib/specbandit/redis_queue.rb', line 50

def close
  redis.close
end

#length(key) ⇒ Object

Returns the current length of the queue.



40
41
42
# File 'lib/specbandit/redis_queue.rb', line 40

def length(key)
  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.



15
16
17
18
19
20
21
# File 'lib/specbandit/redis_queue.rb', line 15

def push(key, files, ttl: nil)
  return 0 if files.empty?

  count = redis.rpush(key, files)
  redis.expire(key, ttl) if ttl
  count
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).



46
47
48
# File 'lib/specbandit/redis_queue.rb', line 46

def read_all(key)
  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+).



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/specbandit/redis_queue.rb', line 27

def steal(key, count)
  result = 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