Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for running the app under any ruby

  • Loading branch information...
commit f75d29c0801bcc748abd410404fbc264c5c29750 1 parent 4ddd03a
@evanphx evanphx authored
View
83 lib/puma/express.rb
@@ -3,6 +3,8 @@
require 'rubygems'
require 'tmpdir'
+require 'puma/express/app'
+
class Puma::Express
VERSION = '1.0.0'
@@ -11,66 +13,8 @@ class Puma::Express
"content-length" => true
}
- class App
- def initialize(path, socket, idle_limit)
- @path = path
- @socket = socket
- @idle_limit = idle_limit
- @last_hit = Time.now
- @pid = nil
- end
-
- def expired?
- @last_hit && Time.now - @last_hit > @idle_limit
- end
-
- def hit
- @last_hit = Time.now
- end
-
- def in_child(ready)
- Dir.chdir @path
-
- events = Puma::Events.new STDOUT, STDERR
-
- app, options = Rack::Builder.parse_file "config.ru"
-
- s = Puma::Server.new app, events
- s.min_threads = 0
- s.max_threads = 10
-
- s.add_unix_listener @socket
-
- ready << "!"
-
- s.run.join
- end
-
- def run
- r, w = IO.pipe
-
- @pid = fork do
- begin
- in_child w
- rescue Interrupt
- end
- end
-
- r.read 1
- end
-
- def stop
- if @pid
- Process.kill 'INT', @pid
- Process.wait @pid
- @pid = nil
- end
- end
- end
-
def initialize
@servers = {}
- @starter = File.expand_path "../starter.rb", __FILE__
@root = ENV['PUMA_EXPRESS_ROOT'] || File.expand_path("~/.puma_express")
@apps = {}
@@ -80,6 +24,10 @@ def initialize
end
def cleanup
+ @apps.each do |h,a|
+ a.stop
+ end
+
FileUtils.remove_entry_secure @unix_socket_dir
end
@@ -110,23 +58,6 @@ def find_host(env)
end
end
- class KeepAliveTimer
- def initialize(idle_limit, app)
- @last_hit = nil
- @idle_limit = idle_limit
- @app = app
- end
-
- def call(env)
- @last_hit = Time.now
- @app.call env
- end
-
- def expired?
- @last_hit && Time.now - @last_hit > @idle_limit
- end
- end
-
def start(env)
host = env['HTTP_HOST']
@@ -269,7 +200,7 @@ def call(env)
begin
proxy_unix env, host, path
rescue SystemCallError => e
- error env, "Error with #{host}:#{port}: #{e.message} (#{e.class})"
+ error env, "Error with #{host}: #{e.message} (#{e.class})"
rescue Exception => e
error env, "Unknown error: #{e.message} (#{e.class})"
end
View
78 lib/puma/express/app.rb
@@ -0,0 +1,78 @@
+require 'puma/events'
+require 'puma/server'
+require 'rack/builder'
+
+class Puma::Express
+ class App
+ Starter = File.expand_path "../starter.rb", __FILE__
+
+ def initialize(path, socket, idle_limit)
+ @path = path
+ @socket = socket
+ @idle_limit = idle_limit
+ @last_hit = Time.now
+ @pid = nil
+
+ cf = "#{path}.yml"
+
+ @ruby = nil
+ @exec = false
+
+ if File.exists?(cf)
+ @config = YAML.load File.read(cf)
+ @ruby = @config['ruby']
+ else
+ @config = nil
+ end
+
+ unless @ruby
+ @exec = File.exists?(File.join(path, ".rvmrc")) ||
+ File.exists?(File.join(path, ".rbenv-version"))
+ end
+ end
+
+ def expired?
+ @last_hit && Time.now - @last_hit > @idle_limit
+ end
+
+ def hit
+ @last_hit = Time.now
+ end
+
+ def run
+ r, w = IO.pipe
+
+ @pid = fork do
+ r.close
+
+ Dir.chdir @path
+
+ if @ruby
+ exec @ruby, Starter, @socket, w.to_i.to_s
+ elsif @exec
+ exec "bash", "-l", "-c", "ruby #{Starter} #{@socket} #{w.to_i}"
+ else
+ ARGV.unshift w.to_i.to_s
+ ARGV.unshift @socket
+
+ load Starter
+ end
+ end
+
+ w.close
+
+ IO.select [r]
+
+ r.read 1
+ r.close
+ end
+
+ def stop
+ if @pid
+ Process.kill 'TERM', @pid
+ Process.wait @pid
+ @pid = nil
+ end
+ end
+ end
+end
View
34 lib/puma/express/starter.rb
@@ -0,0 +1,34 @@
+path = ARGV.shift
+ready = IO.for_fd ARGV.shift.to_i
+ready.sync = true
+
+s = nil
+
+begin
+ require 'rubygems'
+ require 'puma'
+
+ events = Puma::Events.new STDOUT, STDERR
+
+ app, options = Rack::Builder.parse_file "config.ru"
+
+ s = Puma::Server.new app, events
+ s.min_threads = 0
+ s.max_threads = 10
+
+ s.add_unix_listener path
+
+ Signal.trap "INT" do
+ s.stop
+ end
+
+ Signal.trap "TERM" do
+ s.stop
+ end
+ensure
+ ready << "!"
+ ready.close
+end
+
+s.run.join if s
+
View
20 lib/puma/starter.rb
@@ -1,20 +0,0 @@
-
-require 'puma'
-
-base = ARGV.shift
-port = ARGV.shift
-
-Dir.chdir base
-
-events = Puma::Events.new STDOUT, STDERR
-
-app, options = Rack::Builder.parse_file "config.ru"
-
-s = Puma::Server.new app, events
-s.min_threads = 0
-s.max_threads = 10
-
-s.add_tcp_listener "127.0.0.1", port.to_i
-
-s.run.join
-
View
2  tmp/hello/config.ru
@@ -2,5 +2,5 @@ require 'pp'
run lambda { |env|
pp env
- [200, {"Content-Type" => "text/plain", "X-Puma" => "youbetcha"}, ["Hello World"]]
+ [200, {"Content-Type" => "text/plain", "X-Puma" => "youbetcha"}, ["Hello World - #{RUBY_VERSION}"]]
}
Please sign in to comment.
Something went wrong with that request. Please try again.