Class: Endoscope::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/endoscope/transport.rb

Constant Summary collapse

ConnectionError =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Transport

Returns a new instance of Transport.



11
12
13
14
# File 'lib/endoscope/transport.rb', line 11

def initialize(opts)
  @namespace = opts.delete(:namespace) || "endoscope"
  @redis_opts = opts
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'lib/endoscope/transport.rb', line 10

def namespace
  @namespace
end

#redis_optsObject (readonly)

Returns the value of attribute redis_opts.



10
11
12
# File 'lib/endoscope/transport.rb', line 10

def redis_opts
  @redis_opts
end

Instance Method Details

#listen_to_responsesObject



47
48
49
50
51
52
53
54
55
# File 'lib/endoscope/transport.rb', line 47

def listen_to_responses
  connection.subscribe(responses_channel) do |on|
    on.message do |_channel_name, message|
      response = JSON.parse(message)
      yield(response)
    end
  end

end

#publish_response(command, dyno_name, result) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/endoscope/transport.rb', line 38

def publish_response(command, dyno_name, result)
  connection.publish(responses_channel, JSON.generate(
    id: command.fetch('id'),
    command: command.fetch('command'),
    dyno_name: dyno_name,
    result: result
  ))
end

#send_command(command_id, command, dyno_selector) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/endoscope/transport.rb', line 29

def send_command(command_id, command, dyno_selector)
  channel = requests_channel(dyno_selector)
  connection.publish(channel, JSON.generate(
    id: command_id,
    command: command,
    channel: channel
  ))
end

#wait_for_commands(dyno_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/endoscope/transport.rb', line 16

def wait_for_commands(dyno_name)
  channels = command_channels(dyno_name)
  connection.subscribe(*channels) do |on|
    on.message do |_channel, message|
      # puts "##{channel}: #{message}"
      command = JSON.parse(message)
      yield(command)
    end
  end
rescue Redis::BaseConnectionError => error
  raise ConnectionError, error.message, error
end