Sha256: 28ad2acf13b25d9b8b22324ee984f2a3a62e9bff7af1c1173dce3c5e602332ce

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require "foreman"

class Foreman::Process

  attr_reader :entry
  attr_reader :num
  attr_reader :pid
  attr_reader :port

  def initialize(entry, num, port)
    @entry = entry
    @num = num
    @port = port
  end

  def run(pipe, basedir, environment)
    Dir.chdir(basedir) do
      with_environment(environment.merge("PORT" => port.to_s)) do
        run_process entry.command
      end
    end
  end

  def name
    "%s.%s" % [ entry.name, num ]
  end

private

  def run_process(command)
    io = IO.popen([Foreman.runner, replace_command_env(command)], "w+")
    @pid = io.pid
    trap("SIGTERM") { "got sigterm for %d" % @pid }
    output pipe, "started with pid %d" % @pid
    Thread.new do
      until io.eof?
        output pipe, io.gets
      end
    end
  end

  def output(pipe, message)
    pipe.puts "%s,%s" % [ name, message ]
  end

  def replace_command_env(command)
    command.gsub(/\$(\w+)/) { |e| ENV[e[1..-1]] }
  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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
foreman-0.28.0.pre1 lib/foreman/process.rb