Skip to content

Instantly share code, notes, and snippets.

@austra
Created April 2, 2015 01:28
Show Gist options
  • Save austra/4a299cd9dd94794a36e0 to your computer and use it in GitHub Desktop.
Save austra/4a299cd9dd94794a36e0 to your computer and use it in GitHub Desktop.
redis expiration listener
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