class Twirl::Server
Constants
- ConfigTemplate
Attributes
Public: The admin_port
kestrel will run on.
Public: The memcache_port
kestrel will run on.
Public: The text_port
kestrel will run on.
Public: The thrift_port
kestrel will run on.
Public: The version kestrel will run.
Public Class Methods
# 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
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
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
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
Private: Downloads the file if it has not been downloaded.
# File lib/twirl/server.rb, line 104 def ensure_downloaded download unless downloaded? end
Private: Ensures that file is unzipped if downloaded.
# File lib/twirl/server.rb, line 149 def ensure_unzipped if downloaded? && !unzipped? unzip end end
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
Private: Pings the kestrel server.
# File lib/twirl/server.rb, line 231 def ping get_response("ping")["response"] end
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
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
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
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
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
Public: Stops the server.
# File lib/twirl/server.rb, line 99 def stop stop_server end
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
Private: Returns true if server is stopped else false.
# File lib/twirl/server.rb, line 215 def stopped? return !running? end
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
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