Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using env var to specify pidfile #36486

Merged
merged 1 commit into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion guides/source/initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def default_options
environment: (ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development").dup,
daemonize: false,
caching: nil,
pid: Options::DEFAULT_PID_PATH,
pid: ENV.fetch("PIDFILE", Options::DEFAULT_PIDFILE).dup,
restart_cmd: restart_command)
end
```
Expand Down
2 changes: 2 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Support using environment variable to set pidfile

*Ben Thorner*

Please check [6-0-stable](https://github.com/rails/rails/blob/6-0-stable/railties/CHANGELOG.md) for previous changes.
11 changes: 6 additions & 5 deletions railties/lib/rails/commands/server/server_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ServerCommand < Base # :nodoc:
RACK_SERVERS = %w(cgi fastcgi webrick lsws scgi thin puma unicorn)

DEFAULT_PORT = 3000
DEFAULT_PID_PATH = "tmp/pids/server.pid"
DEFAULT_PIDFILE = "tmp/pids/server.pid"

argument :using, optional: true

Expand All @@ -114,8 +114,8 @@ class ServerCommand < Base # :nodoc:
desc: "Runs server as a Daemon."
class_option :using, aliases: "-u", type: :string,
desc: "Specifies the Rack server used to run the application (thin/puma/webrick).", banner: :name
class_option :pid, aliases: "-P", type: :string, default: DEFAULT_PID_PATH,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change affected to an output of help. Please add a default value to desc like port and binding.

desc: "Specifies the PID file."
class_option :pid, aliases: "-P", type: :string,
desc: "Specifies the PID file - defaults to #{DEFAULT_PIDFILE}."
class_option :dev_caching, aliases: "-C", type: :boolean, default: nil,
desc: "Specifies whether to perform caching in development."
class_option :restart, type: :boolean, default: nil, hide: true
Expand Down Expand Up @@ -207,6 +207,7 @@ def user_supplied_options
end
user_supplied_options << :Host if ENV["HOST"] || ENV["BINDING"]
user_supplied_options << :Port if ENV["PORT"]
user_supplied_options << :pid if ENV["PIDFILE"]
user_supplied_options.uniq
end
end
Expand Down Expand Up @@ -253,15 +254,15 @@ def log_to_stdout?
end

def pid
File.expand_path(options[:pid])
File.expand_path(options[:pid] || ENV.fetch("PIDFILE", DEFAULT_PIDFILE))
end

def self.banner(*)
"rails server -u [thin/puma/webrick] [options]"
end

def prepare_restart
FileUtils.rm_f(options[:pid]) if options[:restart]
FileUtils.rm_f(pid) if options[:restart]
end

def deprecate_positional_rack_server_and_rewrite_to_option(original_options)
Expand Down
23 changes: 23 additions & 0 deletions railties/test/commands/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ def test_environment_with_binding
end
end

def test_environment_with_pidfile
switch_env "PIDFILE", "/tmp/rails.pid" do
options = parse_arguments
assert_equal "/tmp/rails.pid", options[:pid]
end
end

def test_caching_without_option
args = []
options = parse_arguments(args)
Expand Down Expand Up @@ -234,6 +241,12 @@ def test_argument_precedence_over_environment_variable
options = parse_arguments(args)
assert_equal "127.0.0.1", options[:Host]
end

switch_env "PIDFILE", "/tmp/rails.pid" do
args = ["-P", "/somewhere/else.pid"]
options = parse_arguments(args)
assert_equal "/somewhere/else.pid", options[:pid]
end
end

def test_records_user_supplied_options
Expand All @@ -253,6 +266,16 @@ def test_records_user_supplied_options
server_options = parse_arguments
assert_equal [:Host], server_options[:user_supplied_options]
end

switch_env "PORT", "3001" do
server_options = parse_arguments
assert_equal [:Port], server_options[:user_supplied_options]
end

switch_env "PIDFILE", "/tmp/server.pid" do
server_options = parse_arguments
assert_equal [:pid], server_options[:user_supplied_options]
end
end

def test_default_options
Expand Down