This commit is contained in:
David Dollar
2011-12-08 16:19:19 -08:00
parent f668b87660
commit c5548a345e
4 changed files with 138 additions and 88 deletions
+40 -6
View File
@@ -2,13 +2,47 @@ require "foreman"
class Foreman::Process
attr_reader :name
attr_reader :command
attr_accessor :color
attr_reader :entry
attr_reader :num
attr_reader :pid
def initialize(name, command)
@name = name
@command = command
def initialize(entry, num)
@entry = entry
@num = num
end
def run(pipe, basedir, environment)
Dir.chdir(basedir) do
with_environment(environment) do
io = IO.popen("#{entry.command} 2>&1", "w+")
@pid = io.pid
output pipe, "started with pid %d"
Thread.new do
until io.eof?
output pipe, io.gets
end
end
end
end
end
def name
"%s.%s" % [ entry.name, num ]
end
private
def output(pipe, message)
pipe.puts "%s,%s" % [ name, message ]
end
def with_environment(environment)
old_env = ENV.each_pair.inject({}) { |h,(k,v)| h.update(k => v) }
environment.each { |k,v| ENV[k] = v }
ret = yield
ENV.clear
old_env.each { |k,v| ENV[k] = v}
ret
end
end