Reputation: 29556
I need to have an infinite loop on top of eventmachine that constantly reads a redis queue. below is my code. is recursion the right way to do it? I tried loop do
loop, but could not make it work that way.
require 'em-hiredis'
def read
d = @redis.blpop 'queue', 0
d.callback do |_, value|
p value
read
end.errback do |e|
p e
EM.next_tick { read }
end
end
EM.run do
@redis = EM::Hiredis.connect
read
end
Upvotes: 4
Views: 536
Reputation: 1451
Better to subscribe to redis pub/sub queue. https://gist.github.com/957367 If you really need a loop then EM itself is an infinite loop, you just need to schedule your job again and again with next_tick:
def read
d = @redis.blpop 'queue', 0
d.callback do |_, value|
EM.next_tick { read }
end.errback do |e|
EM.next_tick { read }
end
end
Upvotes: 3