Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

Commit

Permalink
Add the ability to start any command on any tcp port
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Apr 16, 2012
1 parent c99bc1e commit 426d881
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 31 deletions.
40 changes: 21 additions & 19 deletions lib/puma/express.rb
Expand Up @@ -14,7 +14,7 @@ class Puma::Express
}

def initialize
@servers = {}
@running = {}
@root = ENV['PUMA_EXPRESS_ROOT'] || File.expand_path("~/.puma_express")
@apps = {}

Expand All @@ -37,7 +37,7 @@ def monitor_apps
@apps.delete_if do |host, app|
if app.expired?
app.stop
@servers.delete host
@running.delete host
true
else
false
Expand All @@ -49,13 +49,13 @@ def monitor_apps
end
end

def find_host(env)
def find_app(env)
key = env['HTTP_HOST']
host, path = @servers[key]
if host
@apps[key].hit
[host, path]
if app = @running[key]
app.hit
end

app
end

def start(env)
Expand All @@ -65,17 +65,15 @@ def start(env)

path = File.join @root, base

socket = File.join @unix_socket_dir, host

puts "Starting #{base} on #{socket}"

if File.exists? path
app = App.new path, socket, 5.0
app = App.new host, path, @unix_socket_dir, 5.0

app.run

puts "Starting #{base} on #{app.connection}"

@apps[host] = app
@servers[host] = ["localhost", socket]
@running[host] = app
else
nil
end
Expand All @@ -86,7 +84,7 @@ def error(env, message="Unconfigured host")
[501, {}, ["#{host}: #{message}"]]
end

def proxy_unix(env, host, path)
def proxy_unix(env, path)
request = Rack::Request.new(env)

method = request.request_method.downcase
Expand Down Expand Up @@ -191,16 +189,20 @@ def proxy_tcp(env, host, port)
end

def call(env)
host, path = find_host(env)
app = find_app(env)

host, path = start(env) unless host
app = start(env) unless app

return error(env) unless host
return error(env) unless app

begin
proxy_unix env, host, path
if sock = app.unix_socket
proxy_unix env, sock
else
proxy_tcp env, "localhost", app.tcp_port
end
rescue SystemCallError => e
error env, "Error with #{host}: #{e.message} (#{e.class})"
error env, "Error: #{e.message} (#{e.class})"
rescue Exception => e
error env, "Unknown error: #{e.message} (#{e.class})"
end
Expand Down
67 changes: 55 additions & 12 deletions lib/puma/express/app.rb
Expand Up @@ -6,28 +6,54 @@ class Puma::Express
class App
Starter = File.expand_path "../starter.rb", __FILE__

def initialize(path, socket, idle_limit)
def initialize(host, path, tmp_dir, idle_limit)
@host = host
@path = path
@socket = socket
@tmp_dir = tmp_dir
@idle_limit = idle_limit
@last_hit = Time.now
@pid = nil

cf = "#{path}.yml"

@shell = false
@ruby = nil
@shell = false

@unix_socket = nil
@tcp_port = nil

load_config
end

attr_reader :unix_socket, :tcp_port

def load_config
cf = "#{@path}.yml"

if File.exists?(cf)
@config = YAML.load File.read(cf)
@ruby = @config['ruby']
else
@config = {}
end

@shell = @config['full_shell'] ||
File.exists?(File.join(path, ".rvmrc")) ||
File.exists?(File.join(path, ".rbenv-version"))
if cmd = @config['command']
@command = cmd
else
@ruby = @config['ruby']
@shell = @config['full_shell'] ||
File.exists?(File.join(@path, ".rvmrc")) ||
File.exists?(File.join(@path, ".rbenv-version"))
end

unless @tcp_port = @config['port']
@unix_socket = File.join @tmp_dir, @host
end
end

def connection
if @tcp_port
"tcp://0.0.0.0:#{@tcp_port}"
else
"unix://#{@unix_socket}"
end
end

def expired?
Expand All @@ -39,20 +65,37 @@ def hit
end

def run
if @command
@pid = fork do
ENV['PORT'] = @tcp_port.to_s if @tcp_port

Dir.chdir @path
exec "bash", "-c", @command
end

sleep 1
else
run_ruby
end
end

def run_ruby
r, w = IO.pipe

@pid = fork do
ENV['PORT'] = @tcp_port.to_s if @tcp_port

r.close

Dir.chdir @path

if @ruby
exec @ruby, Starter, @socket, w.to_i.to_s
exec @ruby, Starter, @unix_socket, w.to_i.to_s
elsif @shell
exec "bash", "-l", "-c", "ruby #{Starter} #{@socket} #{w.to_i}"
exec "bash", "-l", "-c", "ruby #{Starter} #{@unix_socket} #{w.to_i}"
else
ARGV.unshift w.to_i.to_s
ARGV.unshift @socket
ARGV.unshift @unix_socket

load Starter
end
Expand Down

0 comments on commit 426d881

Please sign in to comment.