Class: Browsed::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/browsed/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command: nil, browser_id: nil, browser: :phantomjs, kill_signal: 9, logging: false) ⇒ Manager

Returns a new instance of Manager.



5
6
7
8
9
10
# File 'lib/browsed/manager.rb', line 5

def initialize(command: nil, browser_id: nil, browser: :phantomjs, kill_signal: 9, logging: false)
  set_command(command: command, browser_id: browser_id, browser: browser)
  
  self.kill_signal    =   kill_signal
  self.logging        =   logging
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



3
4
5
# File 'lib/browsed/manager.rb', line 3

def command
  @command
end

#kill_signalObject

Returns the value of attribute kill_signal.



3
4
5
# File 'lib/browsed/manager.rb', line 3

def kill_signal
  @kill_signal
end

#loggingObject

Returns the value of attribute logging.



3
4
5
# File 'lib/browsed/manager.rb', line 3

def logging
  @logging
end

Instance Method Details

#can_start_more_processes?(max_count: nil) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/browsed/manager.rb', line 31

def can_start_more_processes?(max_count: nil)
  return max_count.nil? || get_current_processes.size < max_count
end

#get_current_processesObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/browsed/manager.rb', line 35

def get_current_processes
  processes           =   []
  procs               =   `#{self.command}`.split("\n")
  
  procs.each do |process_data|
    process           =   parse_process(process_data)
    processes        <<   process
  end if procs && procs.any?
  
  return processes
end

#kill_process!(process) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/browsed/manager.rb', line 60

def kill_process!(process)
  info "[Browsed::Manager] - #{Time.now.to_s}: Killing process with PID #{process[:pid]} matching command #{self.command}."
  
  begin
    ::Process.kill(self.kill_signal, process[:pid])
  
  rescue StandardError => e
    info "[Browsed::Manager] - #{Time.now.to_s}:  Failed to kill process with pid '#{process[:pid]}'. Error Class: #{e.class.name}. Error Message: #{e.message}"
  end
end

#kill_processes!(started_after: nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/browsed/manager.rb', line 51

def kill_processes!(started_after: nil)
  processes           =   get_current_processes
  
  processes.each do |process|
    killable          =   started_after.nil? || (process[:date] && process[:date] < (Time.now - started_after))
    kill_process!(process) if killable
  end if processes && processes.any?
end

#kill_stale_processes!Object



47
48
49
# File 'lib/browsed/manager.rb', line 47

def kill_stale_processes!
  kill_processes!(started_after: ::Browsed.configuration.processes_max_ttl)
end

#set_command(command: nil, browser_id: nil, browser: :phantomjs) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/browsed/manager.rb', line 12

def set_command(command: nil, browser_id: nil, browser: :phantomjs)
  if !command.to_s.empty?
    self.command        =   command
  elsif !browser_id.to_s.empty?
    self.command        =   "ps aux | awk '/--browser_id=#{browser_id}/'"
  else
    case browser
      when :chrome
        self.command    =   "ps -ef | grep /[c]hromedriver"
      when :firefox
        self.command    =   "ps -ef | grep /[f]irefox-bin"
      when :phantomjs
        self.command    =   "ps -ef | grep /[p]hantomjs"
      else
        self.command    =   "ps -ef | grep /[c]hromedriver"
    end
  end
end