Class: RedisRpc::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/redis-rpc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis_server, message_queue, local_object, timeout: nil, response_expiry: 1, verbose: false, logger: nil) ⇒ Server

Returns a new instance of Server.

Parameters:

  • redis_server (Redis)
  • message_queue (String)
  • local_object (Object)
  • timeout (nil, Numeric) (defaults to: nil)
  • response_expiry (Integer) (defaults to: 1)
  • verbose (Boolean) (defaults to: false)
  • logger (nil, Logger) (defaults to: nil)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/redis-rpc.rb', line 116

def initialize(redis_server, message_queue, local_object, timeout: nil, response_expiry: 1, verbose: false, logger: nil)
  @redis_server = redis_server
  @message_queue = message_queue
  @local_object = local_object.tap do |o|
    # Override #respond_to? so it only exposes methods which are directly defined by the receiver
    def o.respond_to?(method_name)
      self.public_methods(false).include?(method_name.to_sym)
    end
  end
  @timeout = timeout || 0
  @response_expiry = response_expiry
  @verbose = verbose
  @logger = logger
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



107
108
109
# File 'lib/redis-rpc.rb', line 107

def logger
  @logger
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



106
107
108
# File 'lib/redis-rpc.rb', line 106

def timeout
  @timeout
end

Instance Method Details

#flush_queue!Object



148
149
150
# File 'lib/redis-rpc.rb', line 148

def flush_queue!
  @redis_server.del @message_queue
end

#runObject



131
132
133
134
135
136
# File 'lib/redis-rpc.rb', line 131

def run
  catch(:stop!) do
    loop { run_one }
    logger&.info("[#{Time.now}] #{self.class.name} : action=run stopped")
  end
end

#run!Object



138
139
140
141
# File 'lib/redis-rpc.rb', line 138

def run!
  flush_queue!
  run
end

#stop!Object



143
144
145
146
# File 'lib/redis-rpc.rb', line 143

def stop!
  client = Client.new(@redis_server.dup, @message_queue)
  client.send(stop_message)
end