0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unicorn+nginxで動かしてるRails をtouchでリスタートしちゃう

Posted at

経緯

  • エンジニアだけの開発チームにデザイナーが加わった
  • 開発機はなるべく本番と同じにするためWEBrick ではなくnginx+unicornで動いている
  • デザイナーはkill -USR2 とかは打てない

方針

  • 基本はこの記事に書いてあることをやる
  • しかし、デザイナーにtmp/unicorn_restart/restart.txt です!と言っても忘れるかもしれない
  • tmp/restart.txt でrestart出来るよう必要がある

結果

  • config/unicorn.rb にこんな事を追記した
  • 基本的にはListen.toの辺りを変えただけ
before_fork do |_server, _worker|

  # oldsig QUIT
  if File.exists?(old_pid) && _server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end

  # watch restart.txt
  if _worker.nr == 0 && Rails.env.development?
    listener = Listen.to(Rails.root.join('tmp').to_s, only: /\Arestart.txt\z/) do |modified, added, removed|
      pid = _server.config[:pid]
      if pid == _server.pid
        begin
          Process.kill("USR2", File.read(pid).to_i)
        rescue Errno::ENOENT, Errno::ESRCH
        end
      end
    end
    listener.start
  end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?