My question is at the top level: how can I make Puma stop failing. But it really consists of many small questions. I will celebrate and boldly each of them in order to try to answer this question.
I host a Rails application on an EC2 instance which is t2.nano. This is admittedly a very small box, but I do not expect my site to receive any kind of traffic. I have successfully constructed everything with Nginx and Puma using Capistrano and Capistrano Puma. Everything was great until one day I went to my site and saw the Nginx 504 message.
I opened the Nginx error log and saw that it could not connect to Puma:
connect() to unix:/home/deploy/myapp/shared/tmp/sockets/puma.sock failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: localhost, request: "GET / HTTP/1.0", upstream: "http://unix:/home/deploy/myapp/shared/tmp/sockets/puma.sock:/500.html", host: "myapp.com"
Having debugged this, I found out that Puma had stopped working. This is why Nginx was unable to connect to it. I think that there are two problems: firstly, the Puma should not stop. The server is tiny, but there is no traffic. secondly, that when Puma does not work, it should restart gracefully. However, right now I'm just focusing on the first question. Because if Puma constantly restarts, it seems reasonable that sometimes it kills the process in a harsh way.
To debug this, I opened htop. Of course, the machine worked without unnecessary memory. It makes sense - I am running a database, rails, webserver and memcache application on one tiny machine. He continues to run out of memory and kill Puma.
I looked at the Puma configuration I installed with Capistrano. In config / deploy.rb I had these lines -
set :puma_threads, [0, 8] set :puma_workers, 0
I read all about puma_workers and puma_threads. I also found out that Nginx has its own workers. Puma processes are very expensive. What makes Puma cool is that it is properly mule-threaded, so the independent processes are awesome. It seems that each worker has his own set of threads, so if there are 4 employees with 8 threads, there will be 32 processes. But in my case, I want to use very little memory. 2 processes sound good to me. <B> 1. Is my understanding of workers and threads correct?
I updated the config / deploy.rb file and deployed it with 0 puma_workers and min = 0, max = 2 threads.
The configuration for Nginx seems to live here: /etc/nginx/nginx.conf. And the configuration for Puma lives here: /home/deploy/myapp/shared/puma.rb. I would expect my updates in config / deploy.rb to force Capistano to edit the configuration files. No luck - my minimum threads were still set to 0.8. 2. Is it correct to try updating these values ββthrough config / deploy.rb when using Capistano?
Also - I opened nginx.conf and saw worker_processes 4; . 3. Was it installed four times when I installed Nginx or did Capistano by default?
I opened htop and, of course, I had many Puma processes. So I manually edited my configuration files and restarted Puma and Nginx.
I changed the number of Nginx workers from 4 to 1. Looking at htop, this worked. I now have only 1 working Nginx. However, Nginx workers have never been very expensive (compared to Puma threads). Therefore, I do not think this is of great importance.
However, there were even more than two Puma threads - there were 6. In the lark I changed the minimum number of threads from 0 to 1 - thinking 0 is not a possible number, so maybe he set the default value, This increased the number of Puma processes to 9 I also tried changing the number of puma_workers to 1 for the same reason, and the number of processes increased. 4. What does it mean to have 0 threads and / or workers?
Then I tried to kill one of the puma processes manually (sudo kill xxxxx), and then all the Puma processes died.
5. What do I need to do to have only 2 puma processes?
As you can see, my understanding of Puma is not very great, and the lines between Puma vs Nginx vs Capistano are not clear. Any help is appreciated. I could not find great resources on this issue.