Module: Urabbit::RPC::Server

Defined in:
lib/urabbit/rpc/server.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

SleepInterval =

How often to check if server should stop.

1

Instance Method Summary collapse

Instance Method Details

#startObject

TODO: Currently this is called directly, but it should be called using server_engine.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/urabbit/rpc/server.rb', line 6

def start
  @channel = Urabbit.create_channel
  logger.info("Starting RPC server for #{self.class.name}")

  @queue = @channel.queue(self.class.queue_name)
  @exchange = @channel.default_exchange

  @queue.subscribe do |delivery_info, properties, payload|
    begin
      result = work(payload)

      @exchange.publish(result,
        routing_key: properties.reply_to,
        correlation_id: properties.correlation_id
      )
    rescue => exception
      @exchange.publish("",
        routing_key: properties.reply_to,
        correlation_id: properties.correlation_id,
        headers: {
          error: {
            code: 500,
            message: exception.message
          }
        }
      )

      logger.warn(
        "RPC Server for #{self.class.name} responded with an error "\
        "due to an exception: #{exception.inspect} caused by payload: "\
        "#{payload.inspect}"
      )
    end
  end

  # Subscribing in blocking mode above disables auto-reconnection feature.
  # It's better to just sleep.
  until(@should_stop) do
    sleep(SleepInterval)
  end

  logger.info("Stopped responding to RPC calls for #{self.class.name}")
end

#stopObject

TODO: Use this method when server_engine is implemented.



51
52
53
54
# File 'lib/urabbit/rpc/server.rb', line 51

def stop
  @should_stop = true
  logger.info("Stopped RPC server for #{self.class.name}")
end