Class: Rdsck::Watch

Inherits:
Object
  • Object
show all
Defined in:
lib/nchan_tools/rdsck.rb

Instance Method Summary collapse

Constructor Details

#initialize(rdsck, node, filters, set_notify_config = nil) ⇒ Watch

Returns a new instance of Watch.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/nchan_tools/rdsck.rb', line 111

def initialize(rdsck, node, filters, set_notify_config = nil)
  @rdsck = rdsck
  @sync = node
  @filters = filters
  @set_notify_config = set_notify_config
  @host, @port, @location = @sync.connection[:host], @sync.connection[:port]
  @url = @sync.connection[:id]
  @async = Async::Redis::Client.new(Async::IO::Endpoint.tcp(@host, @port))
  if set_notify_config
    @rdsck.dbg "set #{@url} notify-keyspace-events to \"Kh\""
    @prev_notify_keyspace_event_config = @sync.config("get", "notify-keyspace-events")
    @prev_notify_keyspace_event_config = @prev_notify_keyspace_event_config[1] if @prev_notify_keyspace_event_config
    
    @sync.config :set, "notify-keyspace-events", "Kh"
  end
end

Instance Method Details

#stopObject



162
163
164
165
166
167
168
# File 'lib/nchan_tools/rdsck.rb', line 162

def stop
  @async.close
  if @set_notify_config
    @rdsck.dbg "set #{@url} notify-keyspace-events back to #{@prev_notify_keyspace_event_config}"
    @sync.config(:set, "notify-keyspace-events", @prev_notify_keyspace_event_config)
  end
end

#watch(task) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/nchan_tools/rdsck.rb', line 128

def watch(task)
  task.async do
    #puts "subscribeme"
    while true do
      @async.psubscribe "__keyspace*__:{channel:*}" do |ctx|
        type, pattern, name, msg = ctx.listen
        #puts "TYPE: #{type}, PAT:#{pattern}, NAME:#{name}, MSG:#{msg}"
        m=name.match(/^__.*__:(\{.*\})/)
        if m && m[1]
          key = m[1]
          
          filtered = false
          subs = nil
          
          if @filters[:min_subscribers]
            subs = @sync.hget key, "fake_subscribers"
            subs = subs.to_i
            filtered = true if subs < @filters[:min_subscribers]
          end
          
          if !filtered
            if subs
              puts "#{key} subscribers: #{subs}"
            else
              puts "#{key}"
            end
          end
          
        end
      end
    end
  end
end