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

Do not generate pidfile in production environments #50644

Merged
merged 4 commits into from Jan 8, 2024

Conversation

hschne
Copy link
Contributor

@hschne hschne commented Jan 8, 2024

Motivation / Background

This is a follow-up to #50628.

When running Rails applications with Docker containers may fail to restart if they crashed (e.g. because of OOM). This is because the /rails/tmp/pids/server.pid file is already present.

Such failed containers will require manual intervention (e.g. remove the faulty container) to get up and running again. To allow containers to restart the server.pid file must be deleted on container start. As suggested in #50628, rather than removing the file in the docker-entrypoint, the default for new Rails applications should be not to create PIDs in the first place in production.

This Pull Request has been created to not create pidfiles in production.

Detail

This makes the pidfile instruction from the default puma.tt template conditional. The puma files in the respective dummy / test apps have been updated for consistency. The default path for the pidfile in the server command has also been made conditional.

Additional information

I opted for making pidfile generation conditional rather than removing it outright as it has its uses in development (e.g. when not running with Docker per default). Might be though that this conditional behaviour ends up causing some confusion. Thoughts?

@@ -243,11 +243,13 @@ def log_to_stdout?
end

def pid
File.expand_path(options[:pid] || ENV.fetch("PIDFILE", DEFAULT_PIDFILE))
default_pidfile = environment == "development" ? DEFAULT_PIDFILE : nil
pid = options[:pid] || ENV["PIDFILE"] || default_pidfile
Copy link
Member

Choose a reason for hiding this comment

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

If someone specify they PIDFILE env or the pid option we should generate it regardless of the environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If someone specify they PIDFILE env or the pid option we should generate it regardless of the environment.

@rafaelfranca I'm sorry if I got it somehow wrong, but isn't that what happens? 😅

The assignment to pid is evaluated regardless of the environment. So if the user sets either the option or ENV["PIDFILE"], their respective values will be assigned to pid

pid = options[:pid] || ENV["PIDFILE"] || default_pidfile # < always non-nil if e.g. PIDFILE is assigned

Only the 'fallback' default_pidfile is unassigned in production, everything else stays the same as before. Open to suggestions on how to make the code clearer 🙂

Copy link
Member

Choose a reason for hiding this comment

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

lol, right. Sorry. I got it wrong

activestorage/test/dummy/config/puma.rb Outdated Show resolved Hide resolved
hschne and others added 2 commits January 8, 2024 18:57
Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
@rafaelfranca rafaelfranca merged commit 482330d into rails:main Jan 8, 2024
3 of 4 checks passed
if ENV["PIDFILE"]
pidfile ENV["PIDFILE"]
else
pidfile tmp/pids/server.pid" if ENV.fetch("RAILS_ENV", "development") == "development"
Copy link
Contributor

Choose a reason for hiding this comment

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

@hschne There is missing "

- pidfile tmp/pids/server.pid" if ENV.fetch("RAILS_ENV", "development") == "development"
+ pidfile "tmp/pids/server.pid" if ENV.fetch("RAILS_ENV", "development") == "development"

Copy link
Contributor

Choose a reason for hiding this comment

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

Also what do you think about simplifying the if-condition to reflect to what we say in the changelog: " Disable pidfile generation in production environment."

unless ENV["RAILS_ENV"] == "production"

Would that work?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like pidfile generation in railties/lib/rails/generators/rails/app/templates/config/puma.rb.tt was updated further by #50669.

@rafaelfranca Wondering whether we should sync

  • actionmailbox/test/dummy/config/puma.rb
  • actiontext/test/dummy/config/puma.rb
  • activestorage/test/dummy/config/puma.rb

files with railties/lib/rails/generators/rails/app/templates/config/puma.rb.tt? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants