Class: Pakyow::Realtime::Server::Adapters::Redis::Subscriber Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/realtime/server/adapters/redis.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Constructor Details

#initialize(redis, channel, &callback) ⇒ Subscriber

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Subscriber.



241
242
243
244
245
246
247
# File 'lib/pakyow/realtime/server/adapters/redis.rb', line 241

def initialize(redis, channel, &callback)
  @redis, @channel, @callback = redis, channel, callback

  @thread = Thread.new do
    subscribe
  end
end

Instance Method Details

#disconnectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



249
250
251
252
# File 'lib/pakyow/realtime/server/adapters/redis.rb', line 249

def disconnect
  @thread.exit
  @redis.disconnect!
end

#resubscribeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



272
273
274
275
# File 'lib/pakyow/realtime/server/adapters/redis.rb', line 272

def resubscribe
  sleep 0.25
  subscribe
end

#subscribeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/pakyow/realtime/server/adapters/redis.rb', line 254

def subscribe
  @redis.subscribe(@channel) do |on|
    on.message do |_, payload|
      begin
        @callback.call(payload)
      rescue => error
        Pakyow.logger.error "[Pakyow::Realtime::Server::Adapters::Redis] Subscriber callback failed: #{error}"
      end
    end
  end
rescue ::Redis::CannotConnectError
  Pakyow.logger.error "[Pakyow::Realtime::Server::Adapters::Redis] Subscriber disconnected"
  resubscribe
rescue => error
  Pakyow.logger.error "[Pakyow::Realtime::Server::Adapters::Redis] Subscriber crashed: #{error}"
  resubscribe
end