Class: Grell::CrawlerManager::PhantomJSManager

Inherits:
Object
  • Object
show all
Defined in:
lib/grell/crawler_manager.rb

Overview

Manages the PhantomJS process

Instance Method Summary collapse

Instance Method Details

#cleanup_all_processesObject



52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



76
77
78
79
80
81
# 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) ⇒ Object



67
68
69
70
71
72
73
74
# 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.exception e, "GRELL. PhantomJS process could not be killed"
end

#running_phantomjs_pidsObject



62
63
64
65
# 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