Class: Foreman::Process
- Inherits:
-
Object
- Object
- Foreman::Process
- Defined in:
- lib/foreman/process.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Test whether or not this
Process
is still running. -
#cwd ⇒ Object
Returns the working directory for this
Process
. -
#dead? ⇒ Boolean
Test whether or not this
Process
has terminated. -
#exec(options = {}) ⇒ Object
Exec a
Process
. -
#expanded_command(custom_env = {}) ⇒ String
Get environment-expanded command for a
Process
. -
#initialize(command, options = {}) ⇒ Process
constructor
Create a Process.
-
#kill(signal) ⇒ Object
Send a signal to this
Process
. -
#run(options = {}) ⇒ Object
Run a
Process
.
Constructor Details
#initialize(command, options = {}) ⇒ Process
Create a Process
17 18 19 20 21 22 |
# File 'lib/foreman/process.rb', line 17 def initialize(command, ={}) @command = command @options = .dup @options[:env] ||= {} end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
6 7 8 |
# File 'lib/foreman/process.rb', line 6 def command @command end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
7 8 9 |
# File 'lib/foreman/process.rb', line 7 def env @env end |
Instance Method Details
#alive? ⇒ Boolean
Test whether or not this Process
is still running
106 107 108 |
# File 'lib/foreman/process.rb', line 106 def alive? kill(0) end |
#cwd ⇒ Object
Returns the working directory for this Process
122 123 124 |
# File 'lib/foreman/process.rb', line 122 def cwd File.(@options[:cwd] || ".") end |
#dead? ⇒ Boolean
Test whether or not this Process
has terminated
114 115 116 |
# File 'lib/foreman/process.rb', line 114 def dead? !alive? end |
#exec(options = {}) ⇒ Object
Exec a Process
81 82 83 84 85 86 |
# File 'lib/foreman/process.rb', line 81 def exec(={}) env = @options[:env].merge([:env] || {}) env.each { |k, v| ENV[k] = v } Dir.chdir(cwd) Kernel.exec (env) end |
#expanded_command(custom_env = {}) ⇒ String
Get environment-expanded command for a Process
30 31 32 33 34 35 36 37 |
# File 'lib/foreman/process.rb', line 30 def (custom_env={}) env = @options[:env].merge(custom_env) = command.dup env.each do |key, val| .gsub!("$#{key}", val) end end |
#kill(signal) ⇒ Object
Send a signal to this Process
92 93 94 95 96 97 98 99 100 |
# File 'lib/foreman/process.rb', line 92 def kill(signal) if Foreman.windows? pid && Process.kill(signal, pid) else pid && Process.kill("-#{signal}", pid) end rescue Errno::ESRCH false end |
#run(options = {}) ⇒ Object
Run a Process
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/foreman/process.rb', line 48 def run(={}) env = @options[:env].merge([:env] || {}) output = [:output] || $stdout if Foreman.windows? Dir.chdir(cwd) do Process.spawn env, (env), :out => output, :err => output end elsif Foreman.jruby? require "posix/spawn" wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{command}" POSIX::Spawn.spawn env, wrapped_command, :out => output, :err => output elsif Foreman.ruby_18? fork do $stdout.reopen output $stderr.reopen output env.each { |k,v| ENV[k] = v } wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{command}" Kernel.exec wrapped_command end else wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{command}" Process.spawn env, wrapped_command, :out => output, :err => output end end |