class Endoscope::Agent

Constants

ENDOSCOPE
EvalTimeout

Attributes

dyno_name[R]
redis_options[R]

Public Class Methods

new(dyno_name, redis_options) click to toggle source
# File lib/endoscope/agent.rb, line 16
def initialize(dyno_name, redis_options)
  @dyno_name = dyno_name
  @redis_options = redis_options || default_redis_options
end

Public Instance Methods

agent_listener() click to toggle source
# File lib/endoscope/agent.rb, line 32
def agent_listener
  Thread.current[:name] = ENDOSCOPE
  begin
    wait_for_commands
  rescue => e
    puts e.inspect
    puts e.backtrace.join("\n")
  end
end
capture_streams() { |out| ... } click to toggle source
# File lib/endoscope/agent.rb, line 77
def capture_streams
  $old_stdout = $stdout
  $old_stderr = $stderr

  out = StringIO.new
  $stdout = out
  $stderr = out
  yield(out)

  out.rewind
  out.read
ensure
  $stdout = $old_stdout
  $stderr = $old_stderr
end
command_received(command) click to toggle source
# File lib/endoscope/agent.rb, line 53
def command_received(command)
  puts "ns=endoscope at=command_received"
  to_eval = command.fetch('command')
  result = evaluate(to_eval)
  Transport.new(redis_options).publish_response(command, dyno_name, result)
end
default_redis_options() click to toggle source
# File lib/endoscope/agent.rb, line 21
def default_redis_options
  {
    url: ENV['ENDOSCOPE_REDIS_URL'] || 'redis://127.0.0.1:6379/',
    namespace: ENV['ENDOSCOPE_REDIS_NAMESPACE']
  }
end
evaluate(ruby) click to toggle source
# File lib/endoscope/agent.rb, line 62
def evaluate(ruby)
  capture_streams do |out|
    begin
      Timeout.timeout(10, EvalTimeout) do
        # rubocop:disable Eval
        res = eval(ruby, TOPLEVEL_BINDING, 'remote_command')
        # rubocop:enable Eval
        out.puts res.inspect
      end
    rescue Exception => e
      out.puts(e.inspect, *e.backtrace)
    end
  end
end
start() click to toggle source
# File lib/endoscope/agent.rb, line 28
def start
  Thread.new(&method(:agent_listener))
end
wait_for_commands() click to toggle source
# File lib/endoscope/agent.rb, line 42
def wait_for_commands
  transport = Transport.new(redis_options)
  transport.wait_for_commands(dyno_name) do |command|
    command_received(command)
  end
rescue Transport::ConnectionError => error
  puts "ns=endoscope at=wait_for_commands error=#{error} reconnect_in=1s"
  sleep 1
  retry
end