Skip to content

Instantly share code, notes, and snippets.

@mezis
Created January 21, 2014 13:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mezis/8539934 to your computer and use it in GitHub Desktop.
Save mezis/8539934 to your computer and use it in GitHub Desktop.
Restart servers in a staggered manner.
diff --git i/config/deploy.rb w/config/deploy.rb
index 73fc910..594322c 100644
--- i/config/deploy.rb
+++ w/config/deploy.rb
@@ -65,7 +65,7 @@ end
desc 'restart'
deploy.task :restart, :roles => :web do
- run "touch #{current_path}/tmp/restart.txt"
+ run "bash -c 'sleep $(( $RANDOM / 100 ))' ; date ; touch #{current_path}/tmp/restart.txt"
end
@mezis
Copy link
Author

mezis commented Jan 21, 2014

$RANDOM is a magic Bash variable that returns an positive signed short (0-32767), so dividing it by 100, each server will restart randomly over roughly 5 minutes.

Left as an exercise to the reader: add a comment and make it the default for production deploys (don't want to do that on staging)

@sdorunga
Copy link

The thing is now testing that everything is fine has to be done on targeted servers.

@mezis
Copy link
Author

mezis commented Jan 21, 2014

Not sure I get your point Stefan. All servers (with role web) are targeted by this.
Plus, the task doesn't return until each has restarted—which does add a 5min delay in your deploy, purposedly.

@sdorunga
Copy link

But then you can have broken servers for 5 minutes untill the last one is finished.

@mezis
Copy link
Author

mezis commented Jan 22, 2014

Why broken? They're just restarted with a new version of the code which is fine. The only issue is that for 5 minutes users may get 1 page from the old code and 1 page from the new

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