From 4ff24a7336d4de12185b009b862948de35dcf584 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 1 Mar 2025 09:48:48 -0600 Subject: [PATCH] Add Restart Puma Server By Touching Restart File as a Rails TIL --- README.md | 3 +- ...rt-puma-server-by-touching-restart-file.md | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 rails/restart-puma-server-by-touching-restart-file.md diff --git a/README.md b/README.md index d09e20c..39bf59c 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1603 TILs and counting..._ +_1604 TILs and counting..._ See some of the other learning resources I work on: - [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators) @@ -1074,6 +1074,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Rescue From](rails/rescue-from.md) - [Rescue From With A Separate Method](rails/rescue-from-with-a-separate-method.md) - [Respond With JSON Regardless of Content Type](rails/respond-with-json-regardless-of-content-type.md) +- [Restart Puma Server By Touching Restart File](rails/restart-puma-server-by-touching-restart-file.md) - [Retrieve An Object If It Exists](rails/retrieve-an-object-if-it-exists.md) - [Rollback A Couple Migrations](rails/rollback-a-couple-migrations.md) - [Rollback A Specific Migration Out Of Order](rails/rollback-a-specific-migration-out-of-order.md) diff --git a/rails/restart-puma-server-by-touching-restart-file.md b/rails/restart-puma-server-by-touching-restart-file.md new file mode 100644 index 0000000..21274ee --- /dev/null +++ b/rails/restart-puma-server-by-touching-restart-file.md @@ -0,0 +1,28 @@ +# Restart Puma Server By Touching Restart File + +Puma includes a plugin that allows us to restart the web server by touching the +`tmp/restart.txt` file. + +In one terminal pane I have my Rails server running. In another terminal pane +from the Rails directory, where there exists a `tmp` folder, I run the +following command. + +```bash +$ touch tmp/restart.txt +``` + +Then in the pane running the Rails server, I see the following after a second: + +``` +* Restarting... +=> Booting Puma +=> Rails 8.0.1 application starting in development +... +``` + +What is happening is that `touch` updates the modified time of that file, which +already exists in the `temp` directory. When the plugin notices (it checks +every 2 seconds) that the modified time is now fresher than the original +modified time when the plugin started, then it calls `launcher.restart`. + +[source](https://github.com/puma/puma/blob/ca201ef69757f8830b636251b0af7a51270eb68a/lib/puma/plugin/tmp_restart.rb)