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(browser: :phantomjs, kill_signal: 9) ⇒ Manager

Returns a new instance of Manager.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/browsed/manager.rb', line 5

def initialize(browser: :phantomjs, kill_signal: 9)
  case browser
    when :phantomjs
      self.command    =   "ps -ef | grep /[p]hantomjs"
    when :firefox
      self.command    =   "ps -ef | grep /[g]eckodriver"
    when :chrome
      self.command    =   "ps -ef | grep /[c]hromedriver"
    else
      self.command    =   "ps -ef | grep /[p]hantomjs"
  end
  
  self.kill_signal    =   kill_signal
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

Instance Method Details

#can_start_more_processes?(max_count: 5) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/browsed/manager.rb', line 20

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

#get_current_processesObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/browsed/manager.rb', line 46

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



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

def kill_process(process)
  info "[Browsed::Manager] - #{Time.now.to_s(:db)}: 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(:db)}:  Failed to kill process with pid '#{process[:pid]}'. Error Class: #{e.class.name}. Error Message: #{e.message}"
  end
end

#reap_stale_processes(started_after: ::Browsed.configuration.processes_max_ttl.call) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/browsed/manager.rb', line 24

def reap_stale_processes(started_after: ::Browsed.configuration.processes_max_ttl.call)
  processes           =   get_current_processes
  
  processes.each do |process|
    if (process[:date] && process[:date] < (Time.now - started_after))
      info "[Browsed::Manager] - #{Time.now.to_s(:db)}: Process with PID #{process[:pid]} was started before #{started_after.to_s(:db)}. Process should be terminated."
      kill_process(process)
    end
  end if processes && processes.any?
end