我有一个 Rails (web) 应用程序,我也需要添加一个 (redis) pub/sub 订阅者。
下面是我需要启动的 PubsubSubscriber 类,然后应用程序启动。
redis 连接是在 resque.rb 初始化文件中创建的。我在连接后尝试了 PubsubSubscriber.new,但是当我尝试启动 rails 服务器时,它挂在:
=> Booting Thin
=> Rails 3.2.13 application starting in development on http://0.0.0.0:5000
=> Call with -d to detach
=> Ctrl-C to shutdown server
与服务器成功启动时相反:
=> Booting Thin
=> Rails 3.2.13 application starting in development on http://0.0.0.0:5000
=> Call with -d to detach
=> Ctrl-C to shutdown server
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:5000, CTRL+C to stop
知道为什么当我尝试在初始化程序中实例化 PubsubSubscriber 类时服务器挂起吗?有更好的地方来启动它吗?
# example modified from https://github.com/redis/redis-rb/blob/master/examples/pubsub.rb
class PubsubSubscriber
def initialize
$redis.psubscribe( :channel_one ) do |on|
on.psubscribe do |event, total|
end
on.pmessage do |pattern, event, message|
# message received, kick off some workers
end
on.punsubscribe do |event, total|
end
end
end
end