Class: Crabfarm::PhantomRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/crabfarm/phantom_runner.rb

Constant Summary collapse

PHANTOM_START_TM =

seconds

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_config = {}) ⇒ PhantomRunner

Returns a new instance of PhantomRunner.



11
12
13
14
# File 'lib/crabfarm/phantom_runner.rb', line 11

def initialize(_config={})
  @config = _config;
  @pid = nil
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



9
10
11
# File 'lib/crabfarm/phantom_runner.rb', line 9

def port
  @port
end

Instance Method Details

#startObject



16
17
18
19
20
21
# File 'lib/crabfarm/phantom_runner.rb', line 16

def start
  find_available_port
  Crabfarm.logger.info "Starting phantomjs in port #{@port}"
  @pid = spawn_phantomjs
  Crabfarm.logger.info "Phantomjs started (PID: #{@pid})"
end

#stopObject



23
24
25
26
27
28
29
30
31
# File 'lib/crabfarm/phantom_runner.rb', line 23

def stop
  unless @pid.nil?
    Crabfarm.logger.info "Stopping phantomjs (PID: #{@pid})"
    Process.kill "INT", @pid
    Process.wait @pid, Process::WNOHANG
    Crabfarm.logger.info "Phantomjs stopped (PID: #{@pid})"
    @pid = nil
  end
end