【问题标题】:Gracefully unsubscribe from redis at exit在退出时优雅地取消订阅 redis
【发布时间】:2017-09-07 14:25:11
【问题描述】:

我有一个监听 redis 频道的 ruby​​ 程序:

module Listener
  class << self
    def listen
      redis.subscribe "messaging" do |on|
        on.message do |_, msg|
          Notify.about(msg)
        end
      end
    end

    def redis
      @redis ||= Redis.new(driver: :hiredis)
    end
  end
end

每次我部署应用程序时,我都会重新启动该过程

kill -15 listener-pid

但是Airbrake 用以下回溯通知我SignalException: SIGTERM

/gems/hiredis-0.6.1/lib/hiredis/ext/connection.rb:19 in read
/gems/hiredis-0.6.1/lib/hiredis/ext/connection.rb:19 in read
/gems/redis-3.3.3/lib/redis/connection/hiredis.rb:54 in read
/gems/redis-3.3.3/lib/redis/client.rb:262 in block in read
/gems/redis-3.3.3/lib/redis/client.rb:250 in io
/gems/redis-3.3.3/lib/redis/client.rb:261 in read
/gems/redis-3.3.3/lib/redis/client.rb:136 in block (3 levels) in call_loop
/gems/redis-3.3.3/lib/redis/client.rb:135 in loop
/gems/redis-3.3.3/lib/redis/client.rb:135 in block (2 levels) in call_loop
/gems/redis-3.3.3/lib/redis/client.rb:231 in block (2 levels) in process
/gems/redis-3.3.3/lib/redis/client.rb:367 in ensure_connected
/gems/redis-3.3.3/lib/redis/client.rb:221 in block in process
/gems/redis-3.3.3/lib/redis/client.rb:306 in logging
/gems/redis-3.3.3/lib/redis/client.rb:220 in process
/gems/redis-3.3.3/lib/redis/client.rb:134 in block in call_loop
/gems/redis-3.3.3/lib/redis/client.rb:280 in with_socket_timeout
/gems/redis-3.3.3/lib/redis/client.rb:133 in call_loop
/gems/redis-3.3.3/lib/redis/subscribe.rb:43 in subscription
/gems/redis-3.3.3/lib/redis/subscribe.rb:12 in subscribe
/gems/redis-3.3.3/lib/redis.rb:2765 in _subscription
/gems/redis-3.3.3/lib/redis.rb:2143 in block in subscribe
/gems/redis-3.3.3/lib/redis.rb:58 in block in synchronize
/usr/lib/ruby/2.4.0/monitor.rb:214 in mon_synchronize
/gems/redis-3.3.3/lib/redis.rb:58 in synchronize
/gems/redis-3.3.3/lib/redis.rb:2142 in subscribe

是否可以优雅地重新启动侦听器进程,这样我就不会收到 SIGTERM 错误?

【问题讨论】:

    标签: ruby redis application-restart hiredis


    【解决方案1】:

    我在redis-rb 中找到了一个pubsub 示例

    在我添加trap('SIGTERM') { exit } 后,问题就解决了

    现在我的监听器类看起来像这样:

    module Listener
      class << self
        def listen
          trap('SIGTERM') { exit }
    
          redis.subscribe "messaging" do |on|
            on.message do |_, msg|
              Notify.about(msg)
            end
          end
        end
    
        def redis
          @redis ||= Redis.new(driver: :hiredis)
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-20
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      • 2010-12-16
      • 1970-01-01
      相关资源
      最近更新 更多