How can I gracefully restart thin + nginx?
Asked Answered
H

1

5

I have my Thin servers configured with nginx and my ROR app is running on them.

Running thin restart when I release an update to my code introduces some downtime to my application. I was trying to figure how to gracefully restart the running Thin instances but I could not find a good solution.

Has anyone been able to achieve this?

Human answered 25/7, 2012 at 20:4 Comment(3)
What does "gracefully" mean? If you need to restart, you need to restart.Huambo
"gracefully" means it doesn't drop current connections until they are handled.Elane
This is how Unicorn does it: 1- Fork master and worker processes. 2- Send all new requests to new master. 3- Send a kill signal to all old processes to die when done. This is graceful.Concavoconcave
H
8
# Restart just the thin server described by that config
sudo thin -C /etc/thin/mysite.yml restart 

Nginx will continue running and proxying requests. If you have your Nginx set to use multiple upstream servers, e.g.

server {
  listen        80;
  server_name  myapp.mysite.com;
  # ...
  location / {
    try_files $uri $uri/index.html /cache$uri.html $uri.html @proxy;
  }
  location @proxy {
    proxy_pass http://myapp.rails;
  }
}

upstream myapp.rails {
   server 127.0.0.1:9001 max_fails=1 fail_timeout=10s;
   server 127.0.0.1:9002 max_fails=1 fail_timeout=10s;
   server 127.0.0.1:9003 max_fails=1 fail_timeout=10s;
}

…then each instance will be restarted in turn and Nginx will automatically route requests around one of the proxies if it's down.

Huambo answered 25/7, 2012 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.