class Rookout::Interface

Constants

TRUE_VALUE

Public Class Methods

new() click to toggle source
# File lib/rookout/interface.rb, line 10
def initialize
  @rook = nil
  @start_options = nil
end

Public Instance Methods

flush() click to toggle source
# File lib/rookout/interface.rb, line 52
def flush
  @rook.flush unless @rook.nil?
end
print_debug_messages() click to toggle source
start(options = {}) click to toggle source
# File lib/rookout/interface.rb, line 20
def start options = {}
  return unless @rook.nil?
  throw_errors = options[:throw_errors] == true
  Config.debug = evaluate_flag options[:debug], "ROOKOUT_DEBUG"

  print_debug_messages if Config.debug

  begin
    verify_env

    require_relative "rookout_singleton"

    configure options

    rook = RookoutSingleton.instance
    rook.connect(**@start_options)
  rescue LoadError
    raise if throw_errors
    STDERR.puts "[Rookout] Failed to load Rookout. Please make sure to force build native extensions by setting" \
      "'bundle config force_ruby_platform true'"
  rescue RookMissingToken, RookInvalidToken, RookInvalidOptions, RookVersionNotSupported, RookBadProtobuf => e
    raise if throw_errors
    STDERR.puts "[Rookout] Failed to start Rookout: #{e.message}"
  rescue RookCommunicationException => e
    raise if throw_errors
    STDERR.puts "[Rookout] " + e.message
  rescue Exception => e
    STDERR.puts e.full_message if Config.debug
    raise if throw_errors
  end
end
stop() click to toggle source
# File lib/rookout/interface.rb, line 56
def stop
  return if @rook.nil?

  @rook.stop
  @rook = nil
end

Private Instance Methods

configure(options) click to toggle source
# File lib/rookout/interface.rb, line 67
def configure options
  # If we are running post fork, use previous start_options
  if options[:post_fork]
    # Don't re-enable the fork handler
    @start_options[:fork] = false
  else
    configure_globals options

    @start_options = configure_start_options options
    print_config @start_options
  end
end
configure_globals(options) click to toggle source
# File lib/rookout/interface.rb, line 92
def configure_globals options
  if Config.debug
    log_to_stderr = true
    log_level = :DEBUG
  else
    log_to_stderr = evaluate_flag options[:log_to_stderr], "ROOKOUT_LOG_TO_STDERR"
    log_level = options[:log_level] || ENV["ROOKOUT_LOG_FILE"]
  end

  log_file = options[:log_file] || ENV["ROOKOUT_LOG_FILE"]

  Config.logger_log_to_stderr = log_to_stderr unless log_to_stderr.nil?
  Config.logger_filename = log_file unless log_file.nil?
  Config.logger_log_level = log_level unless log_level.nil?

  Config.user_git_origin = options[:git_origin] if options[:git_origin]
  Config.user_git_commit = options[:git_commit] if options[:git_commit]
end
configure_start_options(options) click to toggle source
# File lib/rookout/interface.rb, line 111
def configure_start_options options
  host = evaluate_config options[:host], "ROOKOUT_CONTROLLER_HOST", "wss://control.rookout.com"
  port = evaluate_config options[:port], "ROOKOUT_CONTROLLER_PORT", 443
  proxy = evaluate_config options[:proxy], "ROOKOUT_PROXY"
  token = evaluate_config options[:token], "ROOKOUT_TOKEN"

  raise RookMissingToken if token.nil? && !(host_specified options)
  verify_token token if token

  labels = stringify_labels(options[:labels]) || parse_labels(ENV["ROOKOUT_LABELS"])
  validate_labels labels

  async_start = true? ENV["ROOKOUT_ASYNC_START"]
  fork = evaluate_flag options[:fork], "ROOKOUT_ENABLE_FORK"

  { host: host, port: port, proxy: proxy, token: token, labels: labels, async_start: async_start, fork: fork }
end
evaluate_config(argument, env_var_name, default = nil) click to toggle source
# File lib/rookout/interface.rb, line 142
def evaluate_config argument, env_var_name, default = nil
  return argument unless argument.nil?
  return ENV[env_var_name] unless ENV[env_var_name].nil?
  default
end
evaluate_flag(argument, env_var_name) click to toggle source
# File lib/rookout/interface.rb, line 133
def evaluate_flag argument, env_var_name
  return true? argument unless argument.nil?
  true? ENV[env_var_name]
end
host_specified(options) click to toggle source
# File lib/rookout/interface.rb, line 129
def host_specified options
  !options[:host].nil? || !ENV["ROOKOUT_CONTROLLER_HOST"].nil? || !ENV["ROOKOUT_AGENT_HOST"].nil?
end
parse_labels(raw_labels) click to toggle source
# File lib/rookout/interface.rb, line 160
def parse_labels raw_labels
  labels = {}
  return labels if raw_labels.nil?

  raw_labels.split(",").each do |raw_label|
    keyvalue = raw_label.gsub(/^[( "')*]/, "").gsub(/[( "')*]$/, "").split(":")
    if keyvalue.length == 2
      labels[keyvalue[0]] = keyvalue[1]
    end
  end
  labels
end
print_config(options) click to toggle source
stringify_labels(labels) click to toggle source
# File lib/rookout/interface.rb, line 148
def stringify_labels labels
  return nil unless labels

  stringified_labels = {}

  labels.each do |label_name, label_value|
    stringified_labels[label_name.to_s] = label_value.to_s
  end

  stringified_labels
end
true?(value) click to toggle source
# File lib/rookout/interface.rb, line 138
def true? value
  TRUE_VALUE.include? value
end
validate_labels(labels) click to toggle source
# File lib/rookout/interface.rb, line 173
def validate_labels labels
  labels.each do |label_name, _|
    if label_name.start_with? "$"
      raise RookInvalidLabel, label_name
    end
  end
end
verify_env() click to toggle source
# File lib/rookout/interface.rb, line 80
def verify_env
  # Only test alpine
  return unless File.exist? "/etc/alpine-release"

  protobuf = Gem::Specification.find_by_path "google/protobuf"
  STDERR.puts RookBadProtobuf.new.message if protobuf.nil?
  return unless protobuf.platform != "ruby"

  error = RookBadProtobufPlatform.new protobuf.platform
  STDERR.puts error.message
end
verify_token(token) click to toggle source
# File lib/rookout/interface.rb, line 181
def verify_token token
  raise RookInvalidOptions, "Rookout token should be a String" unless token.is_a? String
  raise RookInvalidOptions, "Rookout token should be 64 characters" unless token.length == 64
  raise RookInvalidOptions, "Rookout token must consist of only hexadecimal characters" unless
      token.match(/^[0-9a-zA-Z]{0,64}$/)
end