Module: Plezi::Base::MessageDispatch::RedisDriver

Defined in:
lib/plezi/websockets/redis.rb

Class Method Summary collapse

Class Method Details

.away?(server) ⇒ Boolean

Returns:

  • (Boolean)

46
47
48
49
# File 'lib/plezi/websockets/redis.rb', line 46

def away?(server)
  return true unless connect
  @redis.pubsub('CHANNELS', server).empty?
end

.connectObject


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/plezi/websockets/redis.rb', line 11

def connect
  return false unless ENV['PL_REDIS_URL'] && defined?(::Redis)
  return @redis if (@redis_sub_thread && @redis_sub_thread.alive?) && @redis
  @redis_locker.synchronize do
    return @redis if (@redis_sub_thread && @redis_sub_thread.alive?) && @redis # repeat the test inside syncing, things change.
    @redis.quit if @redis
    @redis = ::Redis.new(url: ENV['PL_REDIS_URL'])
    raise "Redis connction failed for: #{ENV['PL_REDIS_URL']}" unless @redis
    @redis_sub_thread = Thread.new do
      begin
        ::Redis.new(url: ENV['PL_REDIS_URL']).subscribe(::Plezi.app_name, ::Plezi::Base::MessageDispatch.pid) do |on|
          on.message do |_channel, msg|
            ::Plezi::Base::MessageDispatch << msg
          end
        end
      rescue => e
        puts e.message, e.backtrace
        retry
      end
    end
    @redis
  end
end

.push(channel, message) ⇒ Object


40
41
42
43
44
# File 'lib/plezi/websockets/redis.rb', line 40

def push(channel, message)
  return unless connect
  return if away?(channel)
  redis.publish(channel, message)
end

.redisObject

Get the current redis connection.


36
37
38
# File 'lib/plezi/websockets/redis.rb', line 36

def redis
  @redis || connect
end