diff --git a/lib/puma/cli.rb b/lib/puma/cli.rb index 67f4760e..231a8473 100644 --- a/lib/puma/cli.rb +++ b/lib/puma/cli.rb @@ -51,6 +51,10 @@ module Puma o.on "-b", "--bind URI", "URI to bind to (tcp:// and unix:// only)" do |arg| @binds << arg end + + o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg| + @options[:pidfile] = arg + end end @parser.banner = "puma " @@ -72,8 +76,20 @@ module Puma end end - def run + def write_pid + if path = @options[:pidfile] + File.open(path, "w") do |f| + f.puts Process.pid + end + end + end + + def parse_options @parser.parse! @argv + end + + def run + parse_options @rackup = ARGV.shift || "config.ru" @@ -82,6 +98,7 @@ module Puma end load_rackup + write_pid if @binds.empty? @options[:Host] ||= DefaultTCPHost diff --git a/test/test_cli.rb b/test/test_cli.rb new file mode 100644 index 00000000..a83ad29c --- /dev/null +++ b/test/test_cli.rb @@ -0,0 +1,19 @@ +require 'test/unit' +require 'puma/cli' +require 'tempfile' + +class TestCLI < Test::Unit::TestCase + def setup + @pid_file = Tempfile.new("puma-test") + @pid_path = @pid_file.path + @pid_file.close! + end + + def test_pid_file + cli = Puma::CLI.new ["--pidfile", @pid_path] + cli.parse_options + cli.write_pid + + assert_equal File.read(@pid_path).strip.to_i, Process.pid + end +end