Class: RedisBatch::Queue
- Inherits:
-
Object
- Object
- RedisBatch::Queue
- Defined in:
- lib/redis_batch/queue.rb
Class Method Summary collapse
Instance Method Summary collapse
- #abort_all ⇒ Object
- #add(*items) ⇒ Object
- #count ⇒ Object
-
#initialize(namespace = self.class.name) ⇒ Queue
constructor
A new instance of Queue.
- #take(count: 1, client: @client) ⇒ Object
Constructor Details
#initialize(namespace = self.class.name) ⇒ Queue
Returns a new instance of Queue.
3 4 5 6 7 |
# File 'lib/redis_batch/queue.rb', line 3 def initialize(namespace = self.class.name) @namespace = namespace @client = Client.instance @client.with { |redis| RedisBatch::Lua.function_load(redis) } end |
Class Method Details
.processing? ⇒ Boolean
17 18 19 |
# File 'lib/redis_batch/queue.rb', line 17 def self.processing? @client.with { |redis| redis.keys("#{@namespace}_takeout_*").any? } end |
Instance Method Details
#abort_all ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/redis_batch/queue.rb', line 21 def abort_all return unless processing? @client.with do |redis| redis.keys("#{@namespace}_takeout_*").each do |key| abort_processing(key, redis) end end end |
#add(*items) ⇒ Object
9 10 11 |
# File 'lib/redis_batch/queue.rb', line 9 def add(*items) @client.with { |redis| redis.rpush(queue_key, items) } end |
#count ⇒ Object
13 14 15 |
# File 'lib/redis_batch/queue.rb', line 13 def count @client.with { |redis| redis.llen(queue_key) } end |
#take(count: 1, client: @client) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/redis_batch/queue.rb', line 30 def take(count: 1, client: @client) values = client.with do |redis| redis.call("FCALL", "rb_lmove", 2, queue_key, take_key, count) end yield values client.with { |redis| redis.del(take_key) } rescue => error client.with { |redis| abort_processing(take_key, redis) } raise error end |