简体   繁体   English

Rails和Redis

[英]Rails and Redis

After start my redis server on my console using: 使用以下命令在我的控制台上启动redis服务器后:

redis-server

I execute the following ruby script like this: 我像这样执行以下ruby脚本:

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: 这是generate_roster_kids.rb文件的内容:

#!/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 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: 我检查了我的redis-cli MONITOR,一切都“很棒”,但我有以下问题,当用户将状态更改为ONLINE时,我在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: 如果用户改为BUSY,我看到了这个:

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???? 但是当我使用redisdesktopmanager查看我的redis数据库时,我没有看到任何新记录,但是如果我尝试使用redis-cli手动创建记录,则记录成功创建,这可能是什么问题,这里? ???

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. 缩小问题范围的一种方法是使用redis-cli工具连接到Redis服务器(它似乎连接到系统上的Redis实例,脚本运行,并在生成的CLI中发出命令MONITOR贝壳。

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. 然后运行脚本并观察要进入的命令。当然,您还需要确保在运行脚本的系统上运行的Redis副本具有您需要的数据。 Alternatively, you will need to change the Redis connect command to connect to the remote machine where the data resides. 或者,您需要更改Redis connect命令以连接到数据所在的远程计算机。

Edit for updated info 编辑更新的信息

So the code is setting an expire on the keys for 2-3 seconds. 所以代码在键上设置过期2-3秒。 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. 例如,“忙”上的[EXPIRE][1]告诉Redis在修改后三秒钟忽略“忙”。 I'm not sure that is what you want. 我不确定那是你想要的。 The expire on "Busy:..." also tells Redis to ignore that key after two seconds. “Busy:...”到期也告诉Redis在两秒钟后忽略该键。

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. 你会知道2-3秒是否是一个“正确”的使用时间比你更好,但我怀疑它需要更长。 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. 如果您希望在2秒后使用户会话到期并且如此快速地使所有繁忙会话的设置到期,则监视命令将是您验证它们的发送方式 - 而不是查找可能在时间到期的密钥您可以发送命令来检索它们。

You should also consider trying to replace the keys command if possible as that will likely kill performance if your key space gets large. 如果可能的话,您还应该考虑尝试替换keys命令,因为如果您的密钥空间变大,这可能会破坏性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM