Class: RedisRpc::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_rpc/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, sub_channel, pub_channel, front_object, level: Logger::WARN, standalone: true) ⇒ Server

Returns a new instance of Server.



11
12
13
14
15
16
17
18
# File 'lib/redis_rpc/server.rb', line 11

def initialize(url, sub_channel, pub_channel, front_object, level: Logger::WARN, standalone: true)
  @redis = Redis.new(url: url)
  @sub_channel = sub_channel
  @pub_channel = pub_channel
  @logic = Logic.new(url, front_object, pub_channel)
  init_log(level)
  standalone ? standalone_exec : exec
end

Instance Method Details

#execObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/redis_rpc/server.rb', line 26

def exec
  begin
    @redis.subscribe(@sub_channel, RedisRpc::EXCEPTION_CHANNEL) do |on|
      on.subscribe do |channel, subscriptions|
        @logger.info("Subscribed to ##{channel} (#{subscriptions} subscriptions)")
      end

      on.message do |channel, args|
        @logger.info("##{channel}: #{args}")
        case channel.to_sym
        when RedisRpc::EXCEPTION_CHANNEL
          @logger.error("exception: #{args}")
        else
          begin
            @logic.exec(args)
          rescue Exception => e
            @logger.error(e.message.to_s)
          end
        end
      end
      on.unsubscribe do |channel, subscriptions|
        @logger.info("Unsubscribed from ##{channel} (#{subscriptions} subscriptions)")
      end
    end
  rescue Redis::BaseConnectionError => error
    @logger.error("#{error}, retrying in 30s")
    sleep 30
    retry
  end
end

#init_log(level) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/redis_rpc/server.rb', line 57

def init_log(level)
  if defined?(Rails)
    @logger = Rails.logger
  else
    require 'logger'
    @logger = ::Logger.new(STDOUT)
    @logger.level = level
  end
  @logger
end

#standalone_execObject



20
21
22
23
24
# File 'lib/redis_rpc/server.rb', line 20

def standalone_exec
  @thread = Thread.new do
    exec
  end
end