使用 init.d 重启 Puma

使用 init.d 重启 Puma

我有彪马安装并运行为生产 Rails 站点的 Web 服务器,使用 Nginx 作为反向代理。

我想使用初始化文件管理 Nginx 和 Puma 的服务。它出现对我来说,我已经设置好了这样做,但是在应用程序更改后重新启动 Puma 时,旧的更改仍然有效。

如何正确启动和冷启动 Puma 来服务新的应用程序变更?

我能想到的真正使新更改得以应用的唯一方法是重新启动服务器。

显然,这是不对的,有一个命令,但是当我运行它时sudo /etc/init.d/puma restart,它看起来重新启动了(使用新的 PID 和所有内容),但仍然看不到应用程序更改。

操作系统:Ubuntu 16.04
Nginx:1.12.1
Puma:3.11.0
Ruby:2.5.0

Rails 配置

/path/to/app/Gemfile (摘录)

gem 'puma', "~> 3.10"

/路径/到/应用程序/配置/puma.rb

# Change to match your CPU core count
workers Integer(ENV['WEB_CONCURRENCY'] || 2)

# Min and Max threads per worker
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count

app_dir = File.expand_path("../..", __FILE__)
# shared_dir = "#{app_dir}/shared"

# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env

# Set up socket location
bind "unix://#{app_dir}/tmp/sockets/myapp.sock"

# Logging
# stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true

# Set master PID and state locations
# pidfile "#{shared_dir}/pids/puma.pid"
# state_path "#{shared_dir}/pids/puma.state"
activate_control_app

on_worker_boot do
  require "active_record"
  require 'erb'
  begin
    ActiveRecord::Base.connection.disconnect!
  rescue
    ActiveRecord::ConnectionNotEstablished
  end
  ActiveRecord::Base.establish_connection(YAML.safe_load(ERB.new(File.read("/path/to/app/config/database.yml")).result, [], [], true)[rails_env])
end

美洲狮丛林

https://github.com/puma/puma/tree/master/tools/jungle/init.d

我运行了下面的设置初始化文件使用 puma,按照上面链接中的说明进行操作:

  cd ~
  wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/init.d/puma
  # Copy the init script to services directory
  sudo mv puma /etc/init.d
  sudo chmod +x /etc/init.d/puma

  # Make it start at boot time.
  sudo update-rc.d -f puma defaults

  wget https://raw.githubusercontent.com/puma/puma/master/tools/jungle/init.d/run-puma
  sudo mv run-puma /usr/local/bin
  sudo chmod +x /usr/local/bin/run-puma

  # Create an empty configuration file
  sudo touch /etc/puma.conf
  sudo /etc/init.d/puma add /path/to/app deploy /path/to/app/config/puma.rb /path/to/app/log/puma.log

  touch /path/to/app/tmp/sockets/myapp.sock
  mkdir -p /path/to/app/tmp/puma

nginx 配置

  upstream myapp {
    server unix:///path/to/app/tmp/sockets/myapp.sock;
  }

  server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name example.com;

    root /path/to/app/public;

    location / {
      proxy_pass          http://myapp;
      proxy_set_header    X-Real-IP $remote_addr;
      proxy_set_header    Host $http_host;
      proxy_set_header    X-Forwarded-Proto $scheme;
      proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_redirect      off;
      proxy_next_upstream error timeout invalid_header http_502;
    }
  }

以下是来自 init.d status 的一些 puma 输出puma.log

sudo /etc/init.d/puma 状态

● puma.service - LSB: Puma web server
   Loaded: loaded (/etc/init.d/puma; bad; vendor preset: enabled)
   Active: active (running) since Mon 2018-01-15 23:01:50 UTC; 25min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1119 ExecStart=/etc/init.d/puma start (code=exited, status=0/SUCCESS)
    Tasks: 36
   Memory: 309.6M
      CPU: 11.541s
   CGroup: /system.slice/puma.service
           ├─1240 puma 3.11.0 (unix:///path/to/app/tmp/sockets/myapp.sock) [myapp]
           ├─2013 puma: cluster worker 0: 1240 [myapp]
           └─2017 puma: cluster worker 1: 1240 [myapp]

Jan 15 23:01:50 web2 systemd[1]: Starting LSB: Puma web server...
Jan 15 23:01:50 web2 puma[1119]:  * => Running the jungle...
Jan 15 23:01:50 web2 puma[1119]:  * --> Woke up puma /path/to/app
Jan 15 23:01:50 web2 puma[1119]:  * user deploy
Jan 15 23:01:50 web2 puma[1119]:  * log to /path/to/app/log/puma.log
Jan 15 23:01:50 web2 systemd[1]: Started LSB: Puma web server.

/路径/到/app/log/puma.log

[3014] Puma starting in cluster mode...
[3014] * Version 3.11.0 (ruby 2.5.0-p0), codename: Love Song
[3014] * Min threads: 5, max threads: 5
[3014] * Environment: production
[3014] * Process workers: 2
[3014] * Phased restart available
[3014] * Listening on unix:///path/to/app/tmp/sockets/myapp.sock
[3014] Use Ctrl-C to stop
[3014] * Starting control server on unix:///tmp/puma-status-1515998276357-3014
[3014] - Worker 1 (pid: 3123) booted, phase: 0
[3014] - Worker 0 (pid: 3119) booted, phase: 0
[9913] Puma starting in cluster mode...
[9913] * Version 3.11.0 (ruby 2.5.0-p0), codename: Love Song
[9913] * Min threads: 5, max threads: 5
[9913] * Environment: production
[9913] * Process workers: 2
[9913] * Phased restart available
[9913] * Listening on unix:///path/to/app/tmp/sockets/myapp.sock
bundler: failed to load command: puma (/home/deploy/.rbenv/versions/2.5.0/bin/puma)
[3014] - Gracefully shutting down workers...
[1240] Puma starting in cluster mode...
[1240] * Version 3.11.0 (ruby 2.5.0-p0), codename: Love Song
[1240] * Min threads: 5, max threads: 5
[1240] * Environment: production
[1240] * Process workers: 2
[1240] * Phased restart available
[1240] * Listening on unix:///path/to/app/tmp/sockets/myapp.sock
[1240] Use Ctrl-C to stop
[1240] * Starting control server on unix:///tmp/puma-status-1516057427440-1240
[1240] - Worker 1 (pid: 2017) booted, phase: 0
[1240] - Worker 0 (pid: 2013) booted, phase: 0

答案1

我的解决方案是按照上面的一条评论中的建议切换到 systemd。

删除init.dpuma

sudo rv /etc/init.d/puma
sudo update-rc.d -f puma remove
sudo rm /usr/local/bin/run-puma
sudo rm /etc/puma.conf

用于systemd美洲狮

创建服务文件:

sudo sh -c "cat << EOF > /etc/systemd/system/puma.service
[Unit]
Description=Puma HTTP Server
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/path/to/app
ExecStart=/home/deploy/.rbenv/shims/bundle exec puma -e production -C /path/to/app/config/puma.rb /path/to/app/config.ru
PIDFile=/path/to/app/tmp/pids/puma.pid
Restart=always

[Install]
WantedBy=multi-user.target
EOF

然后启用全部:

# After installing or making changes to puma.service
sudo systemctl daemon-reload

# Enable so it starts on boot
sudo systemctl enable puma.service

# Initial start up.
sudo systemctl start puma.service

# Check status
sudo systemctl status puma.service

现在运行重启会立即进行热/分阶段重启,就像我所寻找的那样:

sudo systemctl restart puma.service

相关内容