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

Configuration: Get environment from RAILS_ENV, too #2022

Merged
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
3 changes: 3 additions & 0 deletions History.md
Expand Up @@ -2,6 +2,7 @@

* Features
* Add pumactl `thread-backtraces` command to print thread backtraces (#2053)
* Configuration: `environment` is read from `RAILS_ENV`, if `RACK_ENV` can't be found (#2022)

* Bugfixes
* Your bugfix goes here (#Github Number)
Expand All @@ -12,8 +13,10 @@
* Features
* Strip whitespace at end of HTTP headers (#2010)
* Optimize HTTP parser for JRuby (#2012)
* Add SSL support for the control app (#2046)
* Add SSL support for the control app and cli (#2046, #2052)


* Bugfixes
* Fix Errno::EINVAL when SSL is enabled and browser rejects cert (#1564)
* Fix pumactl defaulting puma to development if an environment was not specified (#2035)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -220,7 +220,7 @@ You can also provide a configuration file with the `-C` (or `--config`) flag:
$ puma -C /path/to/config
```

If no configuration file is specified, Puma will look for a configuration file at `config/puma.rb`. If an environment is specified, either via the `-e` and `--environment` flags, or through the `RACK_ENV` environment variable, Puma looks for configuration at `config/puma/<environment_name>.rb`.
If no configuration file is specified, Puma will look for a configuration file at `config/puma.rb`. If an environment is specified, either via the `-e` and `--environment` flags, or through the `RACK_ENV` or the `RAILS_ENV` environment variables, Puma looks for configuration at `config/puma/<environment_name>.rb`.

If you want to prevent Puma from looking for a configuration file in those locations, provide a dash as the argument to the `-C` (or `--config`) flag:

Expand Down Expand Up @@ -275,7 +275,7 @@ reliability in production environments:

## Community Plugins

* [puma-heroku](https://github.com/evanphx/puma-heroku) — default Puma configuration for running on Heroku
* [puma-heroku](https://github.com/puma/puma-heroku) — default Puma configuration for running on Heroku
* [puma-metrics](https://github.com/harmjanblok/puma-metrics) — export Puma metrics to Prometheus
* [puma-plugin-statsd](https://github.com/yob/puma-plugin-statsd) — send Puma metrics to statsd
* [puma-plugin-systemd](https://github.com/sj26/puma-plugin-systemd) — deeper integration with systemd for notify, status and watchdog
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/configuration.rb
Expand Up @@ -182,7 +182,7 @@ def puma_default_options
:worker_shutdown_timeout => DefaultWorkerShutdownTimeout,
:remote_address => :socket,
:tag => method(:infer_tag),
:environment => -> { ENV['RACK_ENV'] || "development" },
:environment => -> { ENV['RACK_ENV'] || ENV['RAILS_ENV'] || "development" },
:rackup => DefaultRackup,
:logger => STDOUT,
:persistent_timeout => Const::PERSISTENT_TIMEOUT,
Expand Down
2 changes: 1 addition & 1 deletion lib/puma/control_cli.rb
Expand Up @@ -23,7 +23,7 @@ def initialize(argv, stdout=STDOUT, stderr=STDERR)
@control_auth_token = nil
@config_file = nil
@command = nil
@environment = ENV['RACK_ENV']
@environment = ENV['RACK_ENV'] || ENV['RAILS_ENV']

@argv = argv.dup
@stdout = stdout
Expand Down
15 changes: 13 additions & 2 deletions test/test_cli.rb
Expand Up @@ -384,11 +384,22 @@ def test_extra_runtime_dependencies
assert_equal %w[a b], extra_dependencies
end

def test_environment
def test_environment_rack_env
ENV.delete 'RACK_ENV'

Puma::CLI.new ["--environment", @environment]

assert_equal ENV['RACK_ENV'], @environment
assert_equal @environment, ENV['RACK_ENV']
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This improves the output on failing - the order of arguments is: assert_equal exp, actual

end

def test_environment_rails_env
ENV.delete 'RACK_ENV'
ENV['RAILS_ENV'] = @environment

Puma::CLI.new []

assert_equal @environment, ENV['RACK_ENV']

ENV.delete 'RAILS_ENV'
end
end
9 changes: 9 additions & 0 deletions test/test_config.rb
Expand Up @@ -230,6 +230,15 @@ def test_config_files_with_rack_env
end
end

def test_config_files_with_rails_env
with_env('RAILS_ENV' => 'fake-env', 'RACK_ENV' => nil) do
conf = Puma::Configuration.new do
end

assert_equal ['config/puma/fake-env.rb'], conf.config_files
end
end

def test_config_files_with_specified_environment
conf = Puma::Configuration.new do
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_pumactl.rb
Expand Up @@ -53,7 +53,7 @@ def test_rack_env_without_environment
end

def test_environment_without_rack_env
with_env("RACK_ENV" => nil) do
with_env("RACK_ENV" => nil, 'RAILS_ENV' => nil) do
control_cli = Puma::ControlCLI.new ["halt"]
assert_nil control_cli.instance_variable_get("@environment")

Expand Down Expand Up @@ -95,7 +95,7 @@ def test_default_config_file_exist
puma_config_file = "config/puma.rb"
development_config_file = "config/puma/development.rb"

with_env("RACK_ENV" => nil) do
with_env("RACK_ENV" => nil, 'RAILS_ENV' => nil) do
with_config_file(puma_config_file, port) do
control_cli = Puma::ControlCLI.new ["halt"]
assert_equal puma_config_file, control_cli.instance_variable_get("@config_file")
Expand Down