Class: GollyUtils::ChildProcess
- Defined in:
- lib/golly-utils/child_process.rb
Overview
Start, manage, and stop a child process.
Instance Attribute Summary collapse
-
#env ⇒ Hash
Environment variables to set in the child process.
-
#pid ⇒ Fixnum?
readonly
The PID of the child process if running.
-
#quiet ⇒ Boolean
(also: #quiet?)
Whether to print startup/shutdown info to stdout, and whether or not to the stdout and stderr streams of the child process (unless explictly redirected via :spawn_options).
-
#spawn_options ⇒ Hash
Options to pass to
Process#spawn. -
#start_command ⇒ String
The shell command to start the child process.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Checks if the process [previously started by this class] is still alive.
-
#initialize(options = {}) ⇒ ChildProcess
constructor
A new instance of ChildProcess.
-
#shutdown ⇒ Boolean
Stops the child process.
-
#startup ⇒ self
Starts the child process.
Constructor Details
#initialize(options = {}) ⇒ ChildProcess
Returns a new instance of ChildProcess.
32 33 34 35 36 |
# File 'lib/golly-utils/child_process.rb', line 32 def initialize(={}) = {env: {}, quiet: false, spawn_options: {}}.merge() [:spawn_options][:in] ||= '/dev/null' .each {|k,v| send "#{k}=", v} end |
Instance Attribute Details
#env ⇒ Hash
Environment variables to set in the child process.
22 23 24 |
# File 'lib/golly-utils/child_process.rb', line 22 def env @env end |
#pid ⇒ Fixnum? (readonly)
The PID of the child process if running.
26 27 28 |
# File 'lib/golly-utils/child_process.rb', line 26 def pid @pid end |
#quiet ⇒ Boolean Also known as: quiet?
Whether to print startup/shutdown info to stdout, and whether or not to the stdout and stderr streams of the child process (unless explictly redirected via :spawn_options)
13 14 15 |
# File 'lib/golly-utils/child_process.rb', line 13 def quiet @quiet end |
#spawn_options ⇒ Hash
Options to pass to Process#spawn.
18 19 20 |
# File 'lib/golly-utils/child_process.rb', line 18 def @spawn_options end |
#start_command ⇒ String
The shell command to start the child process.
8 9 10 |
# File 'lib/golly-utils/child_process.rb', line 8 def start_command @start_command end |
Instance Method Details
#alive? ⇒ Boolean
Checks if the process [previously started by this class] is still alive.
If it is determined that the process is no longer alive then the internal PID is cleared.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/golly-utils/child_process.rb', line 86 def alive? return false if @pid.nil? alive= begin Process.getpgid(@pid) != -1 rescue Errno::ESRCH false end @pid= nil unless alive alive end |
#shutdown ⇒ Boolean
Stops the child process.
If it is already running, then this will do nothing.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/golly-utils/child_process.rb', line 68 def shutdown if alive? t= Thread.new{ attempt_kill } if quiet? t.join else puts "Stopping process #@pid..." t.join end end !alive? end |
#startup ⇒ self
Starts the child process.
If it is already running, then this will do nothing.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/golly-utils/child_process.rb', line 43 def startup unless alive? opt= self. if quiet? opt= opt.dup mute= [:out,:err] - opt.keys.flatten mute.each {|fd| opt[fd] ||= '/dev/null'} end unless quiet? e= '' env.each{|k,v| e+="#{k}=#{v} "} if env puts "> #{e}#{start_command}" end @pid= spawn env, start_command, opt Process.detach @pid puts "< Spawned process #@pid" unless quiet? end self end |