LoginSignup
10
9

More than 5 years have passed since last update.

Unicorn の起動と停止

Posted at

こちらの記事では Rakefile に追記していたが、 lib/tasks に入れた方がまとまりが良いと思ってやってみた覚書。

新しい task を作成

bundle exec rails generate task unicornctl

ファイルを編集

lib/tasks/unicornctl.rb というファイルができるので内容を以下のようにする。

unicornctl.rb
namespace :unicornctl do
  desc "Start unicorn for development env."
  task start: :environment do
    config = File.expand_path('config/unicorn.rb', Rails.root)
    sh "bundle exec unicorn_rails -c #{config} -E development -D"
  end

  desc "Stop unicorn"
  task stop: :environment do
    unicorn_signal :QUIT
  end

  desc "Restart unicorn with USR2"
  task restart: :environment do
    unicorn_signal :USR2
  end

  desc "Increment number of worker processes"
  task increment: :environmen do
    unicorn_signal :TTIN
  end

  desc "Decrement number of worker processes"
  task decrement: :environment do
    unicorn_signal :TTOU
  end

  desc "Unicorn pstree (depends on pstree command)"
  task pstree: :environment do
    sh "pstree '#{unicorn_pid}'"
  end

  def unicorn_signal signal
    Process.kill signal, unicorn_pid
  end

  def unicorn_pid
    begin
      File.read(File.expand_path('tmp/pids/unicorn.pid', Rails.root)).to_i
    rescue Errno::ENOENT
      raise "Unicorn doesn't seem to be running"
    end
  end
end

タスクの確認

編集が完了したらタスクを確認する。

$ ./bin/bundle exec rake -vT unicornctl
rake unicornctl:decrement  # Decrement number of worker processes
rake unicornctl:increment  # Increment number of worker processes
rake unicornctl:pstree     # Unicorn pstree (depends on pstree command)
rake unicornctl:restart    # Restart unicorn with USR2
rake unicornctl:start      # Start unicorn for development env
rake unicornctl:stop       # Stop unicorn

起動・停止

起動・停止はこちらの記事と同じ。

bundle exec rake unicornctl:start # 起動
bundle exec rake unicornctl:stop  # 停止
10
9
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
10
9