Class: Foreman::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman/process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, options = {}) ⇒ Process

Create a Process

Parameters:

  • command (String)

    The command to run

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :cwd (String) — default: ./

    Change to this working directory before executing the process

  • :env (Hash) — default: {}

    Environment variables to set for this process



17
18
19
20
21
22
# File 'lib/foreman/process.rb', line 17

def initialize(command, options={})
  @command = command
  @options = options.dup

  @options[:env] ||= {}
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/foreman/process.rb', line 6

def command
  @command
end

#envObject (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

Returns:

  • (Boolean)


99
100
101
# File 'lib/foreman/process.rb', line 99

def alive?
  kill(0)
end

#cwdObject

Returns the working directory for this Process



115
116
117
# File 'lib/foreman/process.rb', line 115

def cwd
  File.expand_path(@options[:cwd] || ".")
end

#dead?Boolean

Test whether or not this Process has terminated

Returns:

  • (Boolean)


107
108
109
# File 'lib/foreman/process.rb', line 107

def dead?
  !alive?
end

#exec(options = {}) ⇒ Object

Exec a Process

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :env (Object) — default: {}

    Environment variables to set for this execution

Returns:

  • Does not return



74
75
76
77
78
79
# File 'lib/foreman/process.rb', line 74

def exec(options={})
  env = @options[:env].merge(options[:env] || {})
  env.each { |k, v| ENV[k] = v }
  Dir.chdir(cwd)
  Kernel.exec expanded_command(env)
end

#expanded_command(custom_env = {}) ⇒ String

Get environment-expanded command for a Process

Parameters:

  • custom_env (Hash) (defaults to: {})

    ({}) Environment variables to merge with defaults

Returns:

  • (String)

    The expanded command



30
31
32
33
34
35
36
37
# File 'lib/foreman/process.rb', line 30

def expanded_command(custom_env={})
  env = @options[:env].merge(custom_env)
  expanded_command = command.dup
  env.each do |key, val|
    expanded_command.gsub!("$#{key}", val)
  end
  expanded_command
end

#kill(signal) ⇒ Object

Send a signal to this Process

Parameters:

  • signal (String)

    The signal to send



85
86
87
88
89
90
91
92
93
# File 'lib/foreman/process.rb', line 85

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

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :env (Object) — default: {}

    Environment variables to set for this execution

  • :output (Object) — default: $stdout

    The output stream



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/foreman/process.rb', line 48

def run(options={})
  env    = @options[:env].merge(options[:env] || {})
  output = options[:output] || $stdout
  runner = "#{Foreman.runner}".shellescape
  
  if Foreman.windows?
    Dir.chdir(cwd) do
      Process.spawn env, expanded_command(env), :out => output, :err => output
    end
  elsif Foreman.jruby_18? || Foreman.ruby_18?
    require "posix/spawn"
    wrapped_command = "#{runner} -d '#{cwd.shellescape}' -p -- #{expanded_command(env)}"
    POSIX::Spawn.spawn(*spawn_args(env, wrapped_command.shellsplit, {:out => output, :err => output}))
  else
    wrapped_command = "exec #{runner} -d '#{cwd.shellescape}' -p -- #{command}"
    Process.spawn env, wrapped_command, :out => output, :err => output
  end
end