Class: RunLoop::ProcessWaiter
- Inherits:
-
Object
- Object
- RunLoop::ProcessWaiter
- Defined in:
- lib/run_loop/process_waiter.rb
Overview
A class for waiting on processes.
Instance Attribute Summary collapse
-
#process_name ⇒ Object
readonly
Returns the value of attribute process_name.
Instance Method Summary collapse
-
#initialize(process_name, options = {}) ⇒ ProcessWaiter
constructor
A new instance of ProcessWaiter.
-
#pids ⇒ Array<Integer>
Collect a list of Integer pids.
-
#running_process? ⇒ Boolean
Is the
process_namea running?. -
#wait_for_any ⇒ Object
Wait for
process_nameto start. -
#wait_for_none ⇒ Object
Wait for all
process_nameto finish.
Constructor Details
#initialize(process_name, options = {}) ⇒ ProcessWaiter
Returns a new instance of ProcessWaiter.
8 9 10 11 |
# File 'lib/run_loop/process_waiter.rb', line 8 def initialize(process_name, ={}) @options = DEFAULT_OPTIONS.merge() @process_name = process_name end |
Instance Attribute Details
#process_name ⇒ Object (readonly)
Returns the value of attribute process_name.
6 7 8 |
# File 'lib/run_loop/process_waiter.rb', line 6 def process_name @process_name end |
Instance Method Details
#pids ⇒ Array<Integer>
Collect a list of Integer pids.
15 16 17 18 19 |
# File 'lib/run_loop/process_waiter.rb', line 15 def pids process_info = `ps x -o pid,comm | grep -v grep | grep #{process_name}` process_array = process_info.split("\n") process_array.map { |process| process.split(' ').first.strip.to_i } end |
#running_process? ⇒ Boolean
Is the process_name a running?
22 23 24 |
# File 'lib/run_loop/process_waiter.rb', line 22 def running_process? !pids.empty? end |
#wait_for_any ⇒ Object
Wait for process_name to start.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/run_loop/process_waiter.rb', line 27 def wait_for_any return true if running_process? now = Time.now poll_until = now + @options[:timeout] delay = @options[:interval] is_alive = false while Time.now < poll_until is_alive = running_process? break if is_alive sleep delay end if RunLoop::Environment.debug? puts "Waited for #{Time.now - now} seconds for '#{process_name}' to start." end if @options[:raise_on_timeout] and !is_alive raise "Waited #{@options[:timeout]} seconds for '#{process_name}' to start." end is_alive end |
#wait_for_none ⇒ Object
Wait for all process_name to finish.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/run_loop/process_waiter.rb', line 51 def wait_for_none return true if !running_process? now = Time.now poll_until = now + @options[:timeout] delay = @options[:interval] has_terminated = false while Time.now < poll_until has_terminated = !self.running_process? break if has_terminated sleep delay end if RunLoop::Environment.debug? puts "Waited for #{Time.now - now} seconds for '#{process_name}' to die." end if @options[:raise_on_timeout] and !has_terminated raise "Waited #{@options[:timeout]} seconds for '#{process_name}' to die." end has_terminated end |