Skip to content
This repository has been archived by the owner on Sep 18, 2020. It is now read-only.

Likes not added to redis #10

Closed
cvrsor opened this issue Dec 13, 2011 · 5 comments
Closed

Likes not added to redis #10

cvrsor opened this issue Dec 13, 2011 · 5 comments

Comments

@cvrsor
Copy link

cvrsor commented Dec 13, 2011

Hi,

I'm developing a rails3 application using RedisToGo Nano as my development environment and a Mini instance in Heroku for production. I have closely followed the provided Likeable sample application and added the Likeable::UserMethods to my user model as well as the Likeable module to my posts model (and also added the likes_controller.rb, routes.rb, and like_helper.rb code as appropriate).

Problem is some likes are registered in redis and some are not at all! The bad ones do not create a key at all, meaning, after clicking the like link I run the REDIS.keys command and do not see anything relevant to the specific record.

I used the link tag provided in the posts index view in the sample application. Every time I click the link, the POST request is executed correctly, it includes "resource_name"=>"Post", "resource_id"=>"XX" in the parameters, and returns a 'Success' notice as it should in my view.

My only deviation from the sample application is in the setup of redis, I did:
in config/environments/development.rb:


and in config/initializers/redis.rb

REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

Any ideas?
Many thanks for your help,

@schneems
Copy link
Owner

This is an odd one. If you're saying it works sometimes, for some posts, but not other posts. Is there any pattern with the posts that cannot be liked? Perhaps certain id's that cannot be liked?

I would recommend tailing the log to make sure that the initial post works to the controller

heroku logs --tail

For some additional insight you can take a look at the result of target = Likeable.find_by_resource_id(params[:resource_name], params[:resource_id])

by calling puts target.inspect on the element to ensure it is being properly recovered from the database.

if that works every time, you can ensure the proper keys are being generated

puts target.like_key.inspect
puts current_user.like_key

If those look correct it is possible that your connection to redis is intermittent. You can try writing to redis directly

redis = Likeable.redis
puts redis.inspect # make sure this matches your redis-to-go information
puts redis.hset(target.like_key, current_user.id, Time.now.to_f)
puts redis.hkeys(target.like_key)

Let me know what the result of each of those tests are, and I can help trouble-shoot more. What version of Rails are you on?

@cvrsor
Copy link
Author

cvrsor commented Dec 15, 2011

Hi Richard,

Many thanks for your reply and help. Indeed, it is a very strange error: here's what I found after inspecting the elements you suggest:

  1. The post reaches the controller, I can see the 'Processing by LikesController#create as HTML' in the terminal window
  2. puts target.inspect correctly recovers the 'Post' element from database, all fields are shown correctly in the terminal window (#<Post id: 60, ...)
  3. puts target.like_key.inspect correctly returns "Posts:like:60"
  4. puts current_user.like_key returns Users:like:1

So I guess everything works fine until here.
Also, running redis = Likeable.redis in rails console spits out correctly my RedisToGo connection string, and I can set the hash.

Really strange error..

Only pattern I've noticed is that the first attempt to like/unlike always succeeds and every subsequent attempt always fails..And if I restart the server and try the same record it succeeds, it seems that the redis connection works only for the fist POST and then its gone!

Just in case you spot any stupid mistakes:
config/initializers/likeable.rb```
Likeable.setup do |likeable|
uri = URI.parse(ENV["REDISTOGO_URL"])
likeable.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end


Hey, many thanks for your help, really appreciated!

@cvrsor
Copy link
Author

cvrsor commented Dec 16, 2011

Don't know if it helps, I'm running Rails v.3.0.10. I really have no idea where to start for debugging..

@cvrsor
Copy link
Author

cvrsor commented Dec 16, 2011

Hi Richard,

I think my issue is the same as issue #11 opened by styx!

I had not spotted this before, my 'unsuccessful' posts were sending in the correct resource id, however the target returned by find_by_resource_id is always the first post that is made (from when server started).

Something funny could be going on in likeable / module_methods.rb and specifically in def find_one(klass, id)
Thanks!

@schneems
Copy link
Owner

Thanks again for reporting this, i updated notes in #11. Should be fixed with Gem Version 0.1.1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants