class Twirl::Server

Constants

ConfigTemplate

Attributes

admin_port[R]

Public: The admin_port kestrel will run on.

memcache_port[R]

Public: The memcache_port kestrel will run on.

text_port[R]

Public: The text_port kestrel will run on.

thrift_port[R]

Public: The thrift_port kestrel will run on.

version[R]

Public: The version kestrel will run.

Public Class Methods

new(dir, options = {}) click to toggle source
# File lib/twirl/server.rb, line 65
def initialize(dir, options = {})
  @dir           = dir

  @version       = options.fetch(:version)       { "2.4.1" }
  @memcache_port = options.fetch(:memcache_port) { 22133 }
  @thrift_port   = options.fetch(:thrift_port)   { 2229 }
  @text_port     = options.fetch(:text_port)     { 2222 }
  @admin_port    = options.fetch(:admin_port)    { 2223 }

  @download_dir  = options.fetch(:download_dir)  { "/tmp" }
  @stage         = options.fetch(:stage)         { "twirl" }

  @remote_zip    = "http://robey.github.io/kestrel/download/kestrel-#{@version}.zip"
  @zip_file      = File.join(@download_dir, "kestrel-#{@version}.zip")
  @unzipped_file = File.join(@download_dir, "kestrel-#{@version}")
  @jar_file      = File.join(@unzipped_file, "kestrel_2.9.2-#{@version}.jar")

  @queue_path  = File.join(@dir, "data")
  @log_path    = File.join(@dir, "logs")
  @config_path = File.join(@dir, "config")

  @log_file    = File.join(@log_path, "kestrel.log")
  @config_file = File.join(@config_path, "#{@stage}.scala")
end

Public Instance Methods

download() click to toggle source

Private: Downloads the file.

# File lib/twirl/server.rb, line 115
def download
  uri = URI(@remote_zip)

  Net::HTTP.start(uri.host, uri.port) do |http|
    request = Net::HTTP::Get.new uri.path

    http.request request do |response|
      if response.code.to_i == 200
        downloaded = 0
        last_percent = 0
        total = response["content-length"].to_i
        puts "Downloading #{total} bytes to #{@zip_file}"

        File.open @zip_file, "w" do |io|
          response.read_body do |chunk|
            io.write chunk
            downloaded += chunk.size
            percent_complete = ((downloaded.to_f / total) * 100).round
            show_status = percent_complete % 5 == 0 && last_percent != percent_complete

            if show_status
              last_percent = percent_complete
              puts "#{downloaded}/#{total}\t#{percent_complete}%"
            end
          end
        end
      else
        abort "Could not downloaded kestrel from #{@remote_zip} #{response.inspect}"
      end
    end
  end
end
downloaded?() click to toggle source

Private: Returns true or false depending on whether the file has been downloaded.

# File lib/twirl/server.rb, line 110
def downloaded?
  File.exists?(@zip_file)
end
ensure_configured() click to toggle source

Private: Ensure directories and configuration files are ready to go.

# File lib/twirl/server.rb, line 169
def ensure_configured
  [
    @queue_path,
    @log_path,
    @config_path,
  ].each do |path|
    FileUtils.mkdir_p path
  end

  config_contents = ERB.new(ConfigTemplate).result(binding)
  File.write @config_file, config_contents
end
ensure_downloaded() click to toggle source

Private: Downloads the file if it has not been downloaded.

# File lib/twirl/server.rb, line 104
def ensure_downloaded
  download unless downloaded?
end
ensure_unzipped() click to toggle source

Private: Ensures that file is unzipped if downloaded.

# File lib/twirl/server.rb, line 149
def ensure_unzipped
  if downloaded? && !unzipped?
    unzip
  end
end
get_response(path) click to toggle source

Private: Allows requesting things from the kestrel admin.

# File lib/twirl/server.rb, line 245
def get_response(path)
  uri = URI.parse("http://localhost:#{@admin_port}/#{path}")
  response = Net::HTTP.get_response(uri)
  JSON.parse(response.body)
rescue => exception
  {}
end
ping() click to toggle source

Private: Pings the kestrel server.

# File lib/twirl/server.rb, line 231
def ping
  get_response("ping")["response"]
end
running?() click to toggle source

Private: Returns true if server is running else false.

# File lib/twirl/server.rb, line 207
def running?
  return "pong" == ping
rescue => exception
  $stderr.puts exception.inspect
  false
end
shutdown() click to toggle source

Private: Shutsdown the kestrel server.

# File lib/twirl/server.rb, line 236
def shutdown
  h = get_response("shutdown")
  return h["response"] == "ok"
rescue => exception
  puts "Failed to shutdown: #{exception.inspect}"
  false
end
start() click to toggle source

Public: Downloads, unzips and starts the server.

# File lib/twirl/server.rb, line 91
def start
  ensure_downloaded
  ensure_unzipped
  ensure_configured
  start_server
end
start_server() click to toggle source

Private: Starts the server. Assumes downloaded and unzipped

# File lib/twirl/server.rb, line 183
def start_server
  puts "Starting server."
  Dir.chdir(@dir) do
    system "java -jar #{@jar_file} -f #{@config_file} &"

    loop do
      break if running?
    end
  end
  puts "Started server."
end
status() click to toggle source

Private: Prints out the status of the server.

# File lib/twirl/server.rb, line 220
def status
  if running?
    :running
  else
    :stopped
  end
rescue => exception
  :unknown
end
stop() click to toggle source

Public: Stops the server.

# File lib/twirl/server.rb, line 99
def stop
  stop_server
end
stop_server() click to toggle source

Private: Stops the server.

# File lib/twirl/server.rb, line 196
def stop_server
  puts "Stopping server."
  shutdown

  loop do
    break if stopped?
  end
  puts "Stopped server."
end
stopped?() click to toggle source

Private: Returns true if server is stopped else false.

# File lib/twirl/server.rb, line 215
def stopped?
  return !running?
end
unzip() click to toggle source

Private: Unzips the file.

# File lib/twirl/server.rb, line 156
def unzip
  Dir.chdir(File.dirname(@zip_file)) do
    system "unzip", "-o", @zip_file
  end
end
unzipped?() click to toggle source

Private: Returns true or false depending on whether the file has been unzipped.

# File lib/twirl/server.rb, line 164
def unzipped?
  File.exists?(@unzipped_file)
end