class Crabfarm::Support::PhantomRunner

Constants

PHANTOM_START_TM

Public Class Methods

new(_config={}) click to toggle source
# File lib/crabfarm/support/phantom_runner.rb, line 11
def initialize(_config={})
  @config = _config;
  @process = nil
end

Public Instance Methods

port() click to toggle source
# File lib/crabfarm/support/phantom_runner.rb, line 16
def port
  @config[:port]
end
start() click to toggle source
# File lib/crabfarm/support/phantom_runner.rb, line 20
def start
  logger.info "Starting phantomjs in port #{port}"
  @process = spawn_phantomjs
  logger.info "Phantomjs started (PID: #{@process.pid})"
end
stop() click to toggle source
# File lib/crabfarm/support/phantom_runner.rb, line 26
def stop
  unless @process.nil?
    logger.info "Stopping phantomjs (PID: #{@process.pid})"
    @process.stop
    @process = nil
    logger.info "Phantomjs stopped"
  end
end

Private Instance Methods

logger() click to toggle source
# File lib/crabfarm/support/phantom_runner.rb, line 73
def logger
  Crabfarm.logger
end
phantomjs_cmd() click to toggle source
# File lib/crabfarm/support/phantom_runner.rb, line 51
def phantomjs_cmd
  cmd = [@config[:bin_path]]
  cmd << '--load-images=false' unless @config[:load_images]
  cmd << "--proxy=#{@config[:proxy]}" unless @config[:proxy].nil?
  cmd << "--webdriver=#{port}"
  cmd << "--ssl-protocol=#{@config[:ssl]}" unless @config[:ssl].nil?
  cmd << "--ignore-ssl-errors=true"
  cmd << "--web-security=false"
  cmd << "--webdriver-loglevel=#{@config[:log_level].to_s.upcase}"
  cmd
end
spawn_phantomjs() click to toggle source
# File lib/crabfarm/support/phantom_runner.rb, line 37
def spawn_phantomjs
  proc = nil
  begin
    proc = Utils::Processes.start_logged_process 'phantomjs', phantomjs_cmd, logger
    Timeout::timeout(PHANTOM_START_TM) { wait_for_server }
  rescue ChildProcess::LaunchError
    raise BinaryMissingError.new 'phantomjs', @config[:bin_path]
  rescue Timeout::Error
    proc.stop
    raise
  end
  proc
end
wait_for_server() click to toggle source
# File lib/crabfarm/support/phantom_runner.rb, line 63
def wait_for_server
  loop do
    begin
      Net::HTTP.get_response(URI.parse("http://127.0.0.1:#{port}/status"))
      break
    rescue
    end
  end
end