Created
April 2, 2015 01:28
-
-
Save austra/4a299cd9dd94794a36e0 to your computer and use it in GitHub Desktop.
redis expiration listener
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
def start_listener() | |
uri = URI.parse(ENV["REDISCLOUD_URL"]) | |
redis = Redis.new(host: uri.host, port: uri.port, password: uri.password) | |
Thread.new do | |
begin | |
redis.subscribe("__keyevent@0__:expired") do |on| | |
on.subscribe do |channel, subscriptions| | |
puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)" | |
end | |
on.message do |channel, message| | |
puts "got a message" | |
do_something() | |
redis.unsubscribe if message == "exit" | |
end | |
on.unsubscribe do |channel, subscriptions| | |
puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)" | |
end | |
end | |
rescue Redis::BaseConnectionError => error | |
puts "#{error}, retrying in 1s" | |
sleep 1 | |
retry | |
ensure | |
#no db connection anyway | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment