Class: Alt::Foreman::Process

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cmd) ⇒ Process

Returns a new instance of Process.



7
8
9
# File 'lib/alt-foreman/process.rb', line 7

def initialize(name, cmd)
  @name, @cmd = name, cmd
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



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

def cmd
  @cmd
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



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

def pid
  @pid
end

Instance Method Details

#log(info, col = nil) ⇒ Object



10
11
12
# File 'lib/alt-foreman/process.rb', line 10

def log(info, col = nil)
  Alt::Foreman.log(@name, info, col)
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/alt-foreman/process.rb', line 13

def run
  args = Shellwords.split(@cmd)
  args.each_with_index do |arg, index|
    if arg =~ /-p(\d+)/
      Foreman.kill_tcp_server!($1, @name)
    elsif arg == '-p' && args[index+1] =~ /\d+/
      Foreman.kill_tcp_server!(args[index+1], @name)
    end
  end

  IO.popen(@cmd, 'rb') do |io|
    @pid = io.pid
    while line = io.gets
      log(line)
      Thread.pass
    end
  end
end