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, secret_key: nil, timeout: 10.0) ⇒ Server

Returns a new instance of Server.



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

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

Instance Method Details

#execObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/redis_rpc/server.rb', line 28

def exec
  begin
    @redis.subscribe(@sub_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}")
        @logic.exec(args, @timeout)
      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



50
51
52
53
54
55
56
57
58
59
# File 'lib/redis_rpc/server.rb', line 50

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



22
23
24
25
26
# File 'lib/redis_rpc/server.rb', line 22

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