Rails and Redis

redis, ruby, ruby-on-rails

Solution

One way to narrow down the problem is to use the redis-cli tool to connect to the Redis server (it appears to be connecting to the Redis instance on the syste,m the script runs on, and issue the command `MONITOR` in the resulting CLI shell.

Then run the script and watch for the commands to come in. You will, of course, also need to ensure the copy of Redis running on the system where you run the script has the data you need in it. Alternatively, you will need to change the Redis connect command to connect to the remote machine where the data resides.

Edit for updated info

So the code is setting an expire on the keys for 2-3 seconds. That is a very short timeout. It is quite possible the records are expired by the time you look for them.

For example, the `[EXPIRE][1]` on "busy" tells Redis to ignore the "busy" three seconds after it is modified. I'm not sure that is what you want. The expire on "Busy:..." also tells Redis to ignore that key after two seconds.

I'd recommend changing the value of the expire to something longer in order to verify it the way you want to. You'll know if 2-3 seconds is a "correct" expiration time for your use better than I, but I suspect it needs to be longer. If it is correct that you want to expire user sessions after 2 seconds and expire the set of all busy sessions so quickly, monitoring the commands will be how you verify they are being sent - rather than looking for keys that are likely expired by the time you can send the command to retrieve them.

You should also consider trying to replace the `keys` command if possible as that will likely kill performance if your key space gets large.

Problem

After start my redis server on my console using: ``` redis-server ``` I execute the following ruby script like this: ``` script/user/generate_roster_kids.rb start ``` But It does not work, I get: ``` generate_roster_kids] Pid not found, process seems doesn't exist! [generate_roster_kids] Process daemonized with pid 1110 with thread and Forever v.0.3.2 ``` if I execute the same command again I get the following: ``` generate_roster_kids] Found pid 1110... [generate_roster_kids] Waiting the daemon's death . DONE [generate_roster_kids] Process daemonized with pid 1385 with thread and Forever v.0.3.2 ``` But it does not work, What I'm doing wrong. This is the content of the generate_roster_kids.rb file: ``` #!/usr/bin/ruby require 'rubygems' unless defined?(Gem) require 'forever' require 'redis' Forever.run do REDIS = Redis.new() # # dir "foo" # Default: File.expand_path('../../', __FILE__) # file "bar" # Default: __FILE__ log "bar.log" # Default: File.expand_path(dir, '/log/[file_name].log') # pid "bar.pid" # Default: File.expand_path(dir, '/tmp/[file_name].pid') every 2.seconds do # REDIS.DEL :busy # begin # REDIS.SUNIONSTORE :busy, REDIS.keys('Busy:*') # rescue # REDIS.SUNIONSTORE :busy, nil # end REDIS.SUNIONSTORE :busy, REDIS.keys('Busy:*') rescue REDIS.SUNIONSTORE :busy, nil REDIS.EXPIRE :busy, 3 # online = REDIS.SUNION :online, REDIS.keys('Online:*') # diff = REDIS.SDIFF online, :busy # REDIS.SDIFFSTORE :online, (REDIS.SUNION :online, REDIS.keys('Online:*')), :busy # REDIS.DEL :online # begin # REDIS.SUNIONSTORE :online, REDIS.keys('Online:*') # rescue # REDIS.SUNIONSTORE :online, nil # end REDIS.SUNIONSTORE :online, REDIS.keys('Online:*') rescue REDIS.SUNIONSTORE :online, nil REDIS.EXPIRE :online, 3 end end ``` This code was made by the older developer, but he's not available anymore and I need to make this work on my localmachine. Thanks in advance UPDATE I check my redis-cli MONITOR and everything "works great", but I have the following problem, when a user change his status to ONLINE, I saw this on the MONITOR: ``` 1400661418.908947 [0 127.0.0.1:57453] "DEL" "Busy:537c63ea20db9040d2000332" 1400661418.909909 [0 127.0.0.1:57453] "SREM" "busy" "537c63ea20db9040d2000332" 1400661418.910687 [0 127.0.0.1:57453] "sadd" "Online:537c63ea20db9040d2000332" "537c63ea20db9040d2000332" 1400661418.911705 [0 127.0.0.1:57453] "expire" "Online:537c63ea20db9040d2000332" "2" 1400661419.436520 [0 127.0.0.1:62027] "keys" "Busy:*" 1400661419.437205 [0 127.0.0.1:62027] "SUNIONSTORE" "busy" "" 1400661419.437489 [0 127.0.0.1:62027] "EXPIRE" "busy" "3" 1400661419.437757 [0 127.0.0.1:62027] "keys" "Online:*" 1400661419.438070 [0 127.0.0.1:62027] "SUNIONSTORE" "online" "Online:537c63ea20db9040d2000332" ``` If the user change to BUSY, I saw this: ``` 400661508.795043 [0 127.0.0.1:57453] "DEL" "Online:537c63ea20db9040d2000332" 1400661508.796853 [0 127.0.0.1:57453] "SREM" "online" "537c63ea20db9040d2000332" 1400661508.798088 [0 127.0.0.1:57453] "sadd" "Busy:537c63ea20db9040d2000332" "537c63ea20db9040d2000332" 1400661508.799496 [0 127.0.0.1:57453] "expire" "Busy:537c63ea20db9040d2000332" "2" 1400661509.157067 [0 127.0.0.1:62027] "keys" "Busy:*" 1400661509.157751 [0 127.0.0.1:62027] "SUNIONSTORE" "busy" "Busy:537c63ea20db9040d2000332" ``` But when I check on my redis data base using the redisdesktopmanager, I don't see any new record, but If I try to create the records manually using the redis-cli the records are create successfully, what might be the problem, here????

Original source