Created
June 19, 2012 15:59
-
-
Save tomekr/2954967 to your computer and use it in GitHub Desktop.
Evented redis with evented http request
This file contains 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
EM.run do | |
# Create both an evented and non evented redis handle | |
@evented_redis = EM::Protocols::Redis.connect | |
@non_evented_redis = Redis.new | |
# Setup lambda in order to properly rerun code using EventMachine's next_tick method | |
(popper = lambda do | |
@evented_redis.blpop("blpop:queue", 0) do |msg| | |
url = msg.last | |
# Make the http request using the url sent through the message | |
http = EventMachine::HttpRequest.new( url ).get | |
# CALLBACKS | |
http.errback { p 'Uh oh' } | |
http.callback do | |
# Send the response back to redis, I need to use non evented redis here | |
# because I am unable to do the lpush using the evented redis handle | |
# while inside the blpop block | |
@non_evented_redis.lpush("responses", http.response) | |
end # end callback | |
# Call next tick | |
EventMachine::next_tick(&popper) | |
end # end blpop | |
end).call | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment