Skip to content

Commit

Permalink
Modified padrino start to take an extra --options (-O) parameter
Browse files Browse the repository at this point in the history
to pass options to the rack handler.

For instance, with `puma` you can now do:
`bundle exec padrino s -O Threads=3:16` and the server will start with 3
to 16 threads.

This implementation follows the same syntax that
[rackup](https://github.com/rack/rack/blob/master/lib/rack/server.rb\#L100) uses for
compatibility.

Also implemented --server_options to print out the supported options for
the selected handler (application server).

This has been requested a couple of times, the latest in #1451.

I'd still recommend people to run their applications through the
application server directly -e.g., puma, unicorn, thin...- instead of
using this but it might come in handy for development.
  • Loading branch information
Dario Cravero committed Oct 14, 2013
1 parent 8a8cb19 commit 5fe35cc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
33 changes: 32 additions & 1 deletion padrino-core/lib/padrino-core/cli/base.rb
Expand Up @@ -18,11 +18,19 @@ class Base < Thor
method_option :daemonize, :type => :boolean, :aliases => "-d", :desc => "Run daemonized in the background."
method_option :pid, :type => :string, :aliases => "-i", :desc => "File to store pid."
method_option :debug, :type => :boolean, :desc => "Set debugging flags."
method_option :options, :type => :array, :aliases => "-O", :desc => "--options NAME=VALUE NAME2=VALUE2'. pass VALUE to the server as option NAME. If no VALUE, sets it to true. Run '#{$0} --server_options"
method_option :server_options, :type => :boolean, :desc => "Tells the current server handler's options that can be used with --options"

def start
prepare :start
require File.expand_path("../adapter", __FILE__)
require File.expand_path('config/boot.rb')
Padrino::Cli::Adapter.start(options)

if options[:server_options]
puts server_options(options)
else
Padrino::Cli::Adapter.start(options)
end
end

desc "stop", "Stops the Padrino application (alternatively use 'st')."
Expand Down Expand Up @@ -124,6 +132,29 @@ def prepare(task)
end
end

# https://github.com/rack/rack/blob/master/lib/rack/server.rb\#L100
def server_options(options)
begin
info = []
server = Rack::Handler.get(options[:server]) || Rack::Handler.default(options)
if server && server.respond_to?(:valid_options)
info << ""
info << "Server-specific options for #{server.name}:"

has_options = false
server.valid_options.each do |name, description|
next if name.to_s.match(/^(Host|Port)[^a-zA-Z]/) # ignore handler's host and port options, we do our own.
info << " -O %-21s %s" % [name, description]
has_options = true
end
return "" if !has_options
end
info.join("\n")
rescue NameError
return "Warning: Could not find handler specified (#{options[:server] || 'default'}) to determine handler-specific options"
end
end

protected

def self.banner(task=nil, *args)
Expand Down
1 change: 1 addition & 0 deletions padrino-core/lib/padrino-core/server.rb
Expand Up @@ -31,6 +31,7 @@ def self.start(app, opts={})
FileUtils.mkdir_p(File.dirname(options[:pid]))
end
options[:server] = detect_rack_handler if options[:server].blank?
options.merge!(Hash[*options.delete(:options).map {|opt| opt.split('=',2)}.flatten].symbolize_keys!) if options[:options].is_a?(Array)
new(options, app).start
end

Expand Down

0 comments on commit 5fe35cc

Please sign in to comment.