class Endoscope::Transport

Constants

ALL
ConnectionError

Attributes

namespace[R]
redis_opts[R]

Public Class Methods

new(opts) click to toggle source
# File lib/endoscope/transport.rb, line 11
def initialize(opts)
  @namespace = opts.delete(:namespace) || "endoscope"
  @redis_opts = opts
end

Public Instance Methods

listen_to_responses() { |response| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) { |command| ... } click to toggle source
# 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

Private Instance Methods

command_channels(dyno) click to toggle source
# File lib/endoscope/transport.rb, line 64
def command_channels(dyno)
  type = dyno.split('.', 2).first
  [requests_channel(type), requests_channel(dyno), requests_channel(ALL)]
end
connection() click to toggle source
# File lib/endoscope/transport.rb, line 59
def connection
  @connection ||= Redis.connect(redis_opts)
end
requests_channel(selector) click to toggle source
# File lib/endoscope/transport.rb, line 69
def requests_channel(selector)
  "#{namespace}:requests:#{selector}"
end
responses_channel() click to toggle source
# File lib/endoscope/transport.rb, line 73
def responses_channel
  "#{namespace}:responses"
end