Class: Legion::Gaia::Router::RouterBridge

Inherits:
Object
  • Object
show all
Includes:
Logging::Helper
Defined in:
lib/legion/gaia/router/router_bridge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel_registry:, worker_routing: nil, allowed_worker_ids: []) ⇒ RouterBridge

Returns a new instance of RouterBridge.



13
14
15
16
17
18
19
# File 'lib/legion/gaia/router/router_bridge.rb', line 13

def initialize(channel_registry:, worker_routing: nil, allowed_worker_ids: [])
  log.debug "initialize(channel_registry: #{channel_registry}, " \
            "worker_routing: #{worker_routing}, allowed_worker_ids: #{allowed_worker_ids}"
  @channel_registry = channel_registry
  @worker_routing = worker_routing || WorkerRouting.new(allowed_worker_ids: allowed_worker_ids)
  @started = false
end

Instance Attribute Details

#channel_registryObject (readonly)

Returns the value of attribute channel_registry.



11
12
13
# File 'lib/legion/gaia/router/router_bridge.rb', line 11

def channel_registry
  @channel_registry
end

#startedObject (readonly)

Returns the value of attribute started.



11
12
13
# File 'lib/legion/gaia/router/router_bridge.rb', line 11

def started
  @started
end

#worker_routingObject (readonly)

Returns the value of attribute worker_routing.



11
12
13
# File 'lib/legion/gaia/router/router_bridge.rb', line 11

def worker_routing
  @worker_routing
end

Instance Method Details

#route_inbound(input_frame) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/gaia/router/router_bridge.rb', line 37

def route_inbound(input_frame)
  log.debug "route_inbound(input_frame: #{input_frame})"
  return { routed: false, reason: :not_started } unless started?

  identity = extract_identity(input_frame)
  worker_id = @worker_routing.resolve_worker_id(identity)
  worker_id ||= @worker_routing.resolve_from_db(identity) if identity

  return offline_response(input_frame) unless worker_id

  log.info("RouterBridge routing inbound frame_id=#{input_frame.id} worker_id=#{worker_id}")
  publish_input_frame(input_frame, worker_id: worker_id)
end

#route_outbound(payload) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/legion/gaia/router/router_bridge.rb', line 51

def route_outbound(payload)
  log.debug "route_outbound(payload: #{payload})"
  return { delivered: false, reason: :not_started } unless started?

  frame = reconstruct_output_frame(payload)
  return { delivered: false, reason: :invalid_frame } unless frame

  adapter = @channel_registry.adapter_for(frame.channel_id)
  unless adapter
    log.error(
      'RouterBridge route_outbound failed ' \
      "frame_id=#{frame.id} channel_id=#{frame.channel_id} error=no_adapter"
    )
    return { delivered: false, reason: :no_adapter, channel_id: frame.channel_id }
  end

  rendered = adapter.translate_outbound(frame)
  deliver_result = normalize_delivery_result(
    deliver_output(adapter, rendered, frame),
    channel_id: frame.channel_id,
    frame_id: frame.id
  )
  if deliver_result[:delivered] == false || deliver_result[:error]
    log.error(
      'RouterBridge route_outbound failed ' \
      "frame_id=#{frame.id} channel_id=#{frame.channel_id} " \
      "error=#{deliver_result[:error] || deliver_result[:reason]}"
    )
  else
    log.info("RouterBridge routed outbound frame_id=#{frame.id} channel_id=#{frame.channel_id}")
  end
  deliver_result
end

#startObject



21
22
23
24
25
# File 'lib/legion/gaia/router/router_bridge.rb', line 21

def start
  @started = true
  subscribe_outbound if transport_available?
  log.info("RouterBridge started workers=#{@worker_routing.size}")
end

#started?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/legion/gaia/router/router_bridge.rb', line 33

def started?
  @started == true
end

#stopObject



27
28
29
30
31
# File 'lib/legion/gaia/router/router_bridge.rb', line 27

def stop
  @outbound_consumer&.cancel if @outbound_consumer.respond_to?(:cancel)
  @started = false
  log.info('RouterBridge stopped')
end