Module: Daemon::Utils::Process
- Defined in:
- lib/onapp/engine/utils/process.rb
Class Method Summary (collapse)
-
+ (Object) block_and_fork(prepare_block, timeout = 3, &block)
Spawn new fork, execute prepare_block within fork while blocking parent process.
- + (Object) find(cmd, name = nil, exclude_self: true)
Class Method Details
+ (Object) block_and_fork(prepare_block, timeout = 3, &block)
Spawn new fork, execute prepare_block within fork while blocking parent process
Purpose of this method:
-
Parent process should wait while prepare_block executed
-
Execute prepare_block only within fork
-
Return pid and error. Error should be not-nil if prepare_block failed or timeout exceeded
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/onapp/engine/utils/process.rb', line 29 def block_and_fork(prepare_block, timeout = 3, &block) unless prepare_block.is_a?(Proc) raise TypeError, "Need a Proc for prepare_block for #{self}.#{__method__}" end r, w = IO.pipe pid = ::Process.fork do begin prepare_block.call w.puts('ok') block.call ensure exit! 1 end end error = nil Timeout.timeout(timeout) do = r.readline.strip error = if != 'ok' end [pid, error] rescue Timeout::Error [pid, "#{timeout} seconds timeout exceeded!"] ensure r.close if r w.close if w end |
+ (Object) find(cmd, name = nil, exclude_self: true)
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/onapp/engine/utils/process.rb', line 9 def find(cmd, name = nil, exclude_self: true) ::Sys::ProcTable.ps .select do |p| res = p.comm == cmd res &&= p.cmdline.to_s.strip.match(/#{name}/) unless name.nil? res &&= ![::Process.ppid, ::Process.pid].include?(p.pid) if exclude_self res end .map(&:pid) end |