Created
September 15, 2013 15:59
-
-
Save sirupsen/6571991 to your computer and use it in GitHub Desktop.
Wrap Redis queues in an enumerable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RedisQueue | |
include Enumerable | |
def initialize(queue) | |
@queue = queue | |
end | |
def each(&block) | |
while element = redis.lpop(@queue) | |
block.call(*JSON.parse(element)) | |
end | |
end | |
def push(task) | |
redis.rpush(@queue, task) | |
end | |
alias_method :<<, :push | |
private | |
def redis | |
BackgroundQueue.redis | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment