agoragames / leaderboard

Leaderboards backed by Redis in Ruby
https://rubygems.org/gems/leaderboard
MIT License
478 stars 65 forks source link

Redis connection not being set #57

Closed camillavk closed 9 years ago

camillavk commented 9 years ago

I'm trying to create a leaderboard in my app and it is all working locally but when I try to push it the production environment (in Heroku) it's not accepting the redis_connection option. I know the redis connection is working as on the heroku console it is connecting fine. However when this code runs:

def self.default_leaderboard TieRankingLeaderboard.new( DEFAULT_BOARD, default_options, :redis_connnection => Redis.current ) end

(in my initializers/redis.rb is: uri = ENV[ENV["REDIS_PROVIDER"]] Redis.current = Redis.new(:url => uri) )

This gives me an error saying it can't connect to the Redis server on the localhost. Is there a reason why it is trying to connect to the localhost (even though there is no reference to the localhost anywhere?) and it is not using the :redis_connection => Redis.current?

Thanks

czarneckid commented 9 years ago

It looks like you have ENV[ENV["REDIS_PROVIDER"]]? Could that be the error?

camillavk commented 9 years ago

I didn't initially have it set to that but after contacting the Redis support team they suggested changing it to that (it didn't actually fix the problem though). I eventually managed to get the code working in the production environment by changing the self.default_leaderboard to this:

def self.students_leaderboard TieRankingLeaderboard.new('students_lb', Leaderboard::DEFAULT_OPTIONS.merge(page_size: 50), redis_connection: Redis.current) end

I'm still not sure why I had to do it like this but it seemed to be breaking at the default_options section...

Either way, it's working now, thanks!

czarneckid commented 9 years ago

Odd, I'm not seeing that behavior locally:

@orth ➜  leaderboard rvm:(ruby-2.2.1@leaderboard_gem) git:(master) be irb
2.2.1 :001 > require 'redis'
 => true
2.2.1 :002 > require 'leaderboard'
 => true
2.2.1 :003 >
2.2.1 :004 >   Redis.current = Redis.new(url: 'redis://localhost:6379/15')
 => #<Redis client v3.1.0 for redis://localhost:6379/15>
2.2.1 :005 >
2.2.1 :006 >   leaderboard = Leaderboard.new('highscores', Leaderboard::DEFAULT_OPTIONS, redis_connection: Redis.current)
 => #<Leaderboard:0x007ff16d1d44b0 @leaderboard_name="highscores", @reverse=false, @page_size=25, @member_key=:member, @rank_key=:rank, @score_key=:score, @member_data_key=:member_data, @member_data_namespace="member_data", @global_member_data=false, @redis_connection=#<Redis client v3.1.0 for redis://localhost:6379/15>>
2.2.1 :007 > p leaderboard.instance_variable_get(:@redis_connection)
#<Redis client v3.1.0 for redis://localhost:6379/15>
 => #<Redis client v3.1.0 for redis://localhost:6379/15>
2.2.1 :008 > leaderboard = Leaderboard.new('highscores', Leaderboard::DEFAULT_OPTIONS, :redis_connection => Redis.current)
 => #<Leaderboard:0x007ff16d2de1d0 @leaderboard_name="highscores", @reverse=false, @page_size=25, @member_key=:member, @rank_key=:rank, @score_key=:score, @member_data_key=:member_data, @member_data_namespace="member_data", @global_member_data=false, @redis_connection=#<Redis client v3.1.0 for redis://localhost:6379/15>>
2.2.1 :009 > p leaderboard.instance_variable_get(:@redis_connection)
#<Redis client v3.1.0 for redis://localhost:6379/15>
 => #<Redis client v3.1.0 for redis://localhost:6379/15>
2.2.1 :010 >

Glad you were able to move past the issue however.

camillavk commented 9 years ago

Sorry, I guess that wasn't very clear. The issue wasn't with :redis_connection => Redis.current vs redis_connection: Redis.current.

In the first instance where I used default_options in the TieRankingLeaderboard.new method, I had to change that to specify the default options with Leaderboard::DEFAULT_OPTIONS.merge(page_size: 50).

I really struggled to replicate the issue in the development branch and only managed to fix it on the production branch after hours and hours of trial and error and going back and forth with the code.