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)


81
82
83
# File 'lib/foreman/process.rb', line 81

def alive?
  kill(0)
end

#cwdObject

Returns the working directory for this Process



97
98
99
# File 'lib/foreman/process.rb', line 97

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

#dead?Boolean

Test whether or not this Process has terminated

Returns:

  • (Boolean)


89
90
91
# File 'lib/foreman/process.rb', line 89

def dead?
  !alive?
end

#kill(signal) ⇒ Object

Send a signal to this Process

Parameters:

  • signal (String)

    The signal to send



67
68
69
70
71
72
73
74
75
# File 'lib/foreman/process.rb', line 67

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/foreman/process.rb', line 33

def run(options={})
  env    = options[:env] ? @options[:env].merge(options[:env]) : @options[:env]
  output = options[:output] || $stdout

  if Foreman.windows?
    Dir.chdir(cwd) do
      expanded_command = command.dup
      env.each do |key, val|
        expanded_command.gsub!("$#{key}", val)
      end
      Process.spawn env, expanded_command, :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}"
      exec wrapped_command
    end
  else
    wrapped_command = "#{Foreman.runner} -d '#{cwd}' -p -- #{command}"
    Process.spawn env, wrapped_command, :out => output, :err => output
  end
end