class Grell::CrawlerManager::PhantomJSManager

Manages the PhantomJS process

Public Instance Methods

cleanup_all_processes() click to toggle source
# File lib/grell/crawler_manager.rb, line 52
def cleanup_all_processes
  pids = running_phantomjs_pids
  return if pids.empty?
  Grell.logger.warn "GRELL. Killing PhantomJS processes: #{pids.inspect}"
  pids.each do |pid|
    Grell.logger.warn "GRELL. Sending KILL to PhantomJS process #{pid}"
    kill_process(pid.to_i)
  end
end
force_kill(pid) click to toggle source
# File lib/grell/crawler_manager.rb, line 76
def force_kill(pid)
  Timeout.timeout(KILL_TIMEOUT) { Process.wait(pid) }
rescue Timeout::Error
  Process.kill('KILL', pid)
  Process.wait(pid)
end
kill_process(pid) click to toggle source
# File lib/grell/crawler_manager.rb, line 67
def kill_process(pid)
  Process.kill('TERM', pid)
  force_kill(pid)
rescue Errno::ESRCH, Errno::ECHILD
  # successfully terminated
rescue => e
  Grell.logger.error ["GRELL. PhantomJS process could not be killed", e.message, *e.backtrace].join($/)
end
running_phantomjs_pids() click to toggle source
# File lib/grell/crawler_manager.rb, line 62
def running_phantomjs_pids
  list_phantomjs_processes_cmd = "ps -ef | grep -E 'bin/phantomjs' | grep -v grep"
  `#{list_phantomjs_processes_cmd} | awk '{print $2;}'`.split("\n")
end