简体   繁体   English

在 Ruby on Rails v5.2 和 Puma v4 中,在 logrotate 时重新启动 puma 后,不必要的工人继续存在

[英]In Ruby on Rails v5.2 and Puma v4, after restarting puma at the time of logrotate, unnecessary workers continue to remain

We are uses Ruby on Rails v5.2 and Puma v4, rotating the Rails application log with logrotate, and restarting puma when it is rotated.我们使用 Ruby on Rails v5.2 和 Puma v4,使用 logrotate 旋转 Rails 应用程序日志,并在旋转时重新启动 puma。

We uses puma restart with pumactl phased-restart , but we used to send SIGUSR1 signal to restart it.我们使用 puma restart 和pumactl phased-restart ,但我们曾经发送 SIGUSR1 信号来重新启动它。

Specifically, it is the following code.具体就是下面的代码。

test -s $pid && kill -USR1 "$(cat /path/to/app/tmp/pids/puma.pid)"

Refs: https://github.com/puma/puma/blob/master/docs/restart.md参考: https : //github.com/puma/puma/blob/master/docs/restart.md

Although puma restarts without any problem for a while, after several restarts, unnecessary workers will be left as shown below.虽然puma重启了一段时间没有问题,但是重启几次后,会留下不必要的worker,如下图。 In the following, there are two worker 7s.下面有两个worker 7。 Perhaps pid = 15241 seems to be an unnecessary worker.也许 pid = 15241 似乎是一个不必要的工人。

$ ps aux | grep [p]uma
username     10096 22.8  3.0 3357088 1012940 ?     Sl   13:01   1:12 puma: cluster worker 0: 14749 [app]
username     10135 26.6  3.0 3562476 1016716 ?     Sl   13:01   1:22 puma: cluster worker 1: 14749 [app]
username     10196 17.9  3.4 3688000 1145328 ?     Sl   13:01   0:54 puma: cluster worker 2: 14749 [app]
username     10243 15.6  3.9 3753452 1287424 ?     Sl   13:01   0:46 puma: cluster worker 3: 14749 [app]
username     10325 16.8  3.6 3758280 1189004 ?     Sl   13:01   0:48 puma: cluster worker 4: 14749 [app]
username     10381 15.9  3.1 3423564 1034904 ?     Sl   13:01   0:45 puma: cluster worker 5: 14749 [app]
username     10420 16.6  3.2 3689844 1070112 ?     Sl   13:01   0:46 puma: cluster worker 6: 14749 [app]
username     10464 21.9  2.6 3155296 888148 ?      Sl   13:01   0:59 puma: cluster worker 7: 14749 [app]
username     14749  0.0  0.0 216884 20340 ?        Sl    7月19   0:56 puma 4.0.0 (tcp://0.0.0.0:9292) [app]
username     15241 60.7  9.2 5273704 3049272 ?     Sl   11:02  75:20 puma: cluster worker 7: 14749 [app]

My question is, I would like to know how to restart puma without leaving unnecessary workers.我的问题是,我想知道如何在不留下不必要的工人的情况下重新启动 puma。

Or I would like to know if there is a way to find out what each worker is doing to find out why this is the case.或者我想知道是否有办法找出每个工人正在做什么以找出为什么会这样。

Or, please tell me if there is a way to put in logs etc and check the situation after the fact.或者,请告诉我是否有办法放入日志等并在事后检查情况。

The main usage version is as follows.主要使用版本如下。

  • Ruby: 2.4.2红宝石:2.4.2
  • Ruby on Rails: 5.2.2.1 Ruby on Rails:5.2.2.1
  • Puma: 4.0.0彪马:4.0.0

As mentioned above, puma restarts are performed by pumactl phased-restart , but the situation was the same by sending SIGUSR1 signal.如上所述,puma 重启由pumactl phased-restart ,但情况与发送 SIGUSR1 信号相同。

Puma's phased-restart is a rolling restart. Puma 的phased-restart是滚动重启。 Each worker is restarted one by one.每个worker都被一个一个的重启。 The other thing, is that the command returns before the workers have been restarted.另一件事是命令在重新启动工作程序之前返回。

I also use phased-restart , but I don't have logrotated restart Puma, but instead just have it copytruncate the log files.我也使用phased-restart ,但我没有logrotated restart Puma,而是让它copytruncate日志文件。 It allows the file descriptors to remain the same.它允许文件描述符保持不变。 Now, maybe there are unforeseen problems with my approach, but I haven't seen any issues yet.现在,也许我的方法存在不可预见的问题,但我还没有看到任何问题。

Example logrotate config:示例 logrotate 配置:

/path/to/rails/app/shared/log/*.log {
  compress
  copytruncate
  daily
  dateext
  dateformat .%Y%m%d
  dateyesterday
  delaycompress
  missingok
  notifempty
  rotate 36
  su app_user app_user
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM