module CapistranoUnicorn::Utility

Public Instance Methods

duplicate_unicorn() click to toggle source
# File lib/capistrano/unicorn/utility.rb, line 139
def duplicate_unicorn
  if unicorn_is_running?
    unicorn_send_signal('USR2')
  else
    start_unicorn
  end
end
extract_pid_file() click to toggle source
# File lib/capistrano/unicorn/utility.rb, line 14
    def extract_pid_file
      tmp = Tempfile.new('unicorn.rb')
      begin
        conf = local_unicorn_config
        tmp.write <<-EOF.gsub(/^ */, '')
          config_file = "#{conf}"

          # stub working_directory to avoid chdir failure since this will
          # run client-side:
          def working_directory(path); end

          instance_eval(File.read(config_file), config_file) if config_file
          puts set[:pid]
          exit 0
        EOF
        tmp.close
        extracted_pid = `unicorn -c "#{tmp.path}"`
        $?.success? ? extracted_pid.rstrip : nil
      rescue StandardError => e
        return nil
      ensure
        tmp.close
        tmp.unlink
      end
    end
get_old_unicorn_pid() click to toggle source

Get unicorn master (old) process PID

# File lib/capistrano/unicorn/utility.rb, line 72
def get_old_unicorn_pid
  get_unicorn_pid(old_unicorn_pid)
end
get_unicorn_pid(pid_file=fetch(:unicorn_pid)) click to toggle source

Get unicorn master process PID (using the shell)

# File lib/capistrano/unicorn/utility.rb, line 66
def get_unicorn_pid(pid_file=fetch(:unicorn_pid))
  capture "cat #{pid_file}"
end
kill_unicorn(signal) click to toggle source

Kill Unicorns in multiple ways O_O

# File lib/capistrano/unicorn/utility.rb, line 95
def kill_unicorn(signal)
  if unicorn_is_running?
    puts 'Stopping unicorn...'
    unicorn_send_signal(signal)
  else
    puts 'Unicorn is not running'
  end
end
local_unicorn_config() click to toggle source

In Capistrano 3, shell scripts must be invoked with SSHKit’s execute, instead of run.

# File lib/capistrano/unicorn/utility.rb, line 6
def local_unicorn_config
  if File.exist? fetch(:unicorn_config_rel_file_path)
    fetch(:unicorn_config_rel_file_path)
  else
    fetch(:unicorn_config_stage_rel_file_path)
  end
end
old_unicorn_is_running?() click to toggle source

Command to check if stale Unicorn is running

# File lib/capistrano/unicorn/utility.rb, line 60
def old_unicorn_is_running?
  remote_process_exists?(old_unicorn_pid)
end
old_unicorn_pid() click to toggle source

Stale Unicorn process pid file

# File lib/capistrano/unicorn/utility.rb, line 48
def old_unicorn_pid
  "#{fetch :unicorn_pid}.oldbin"
end
remote_process_exists?(pid_file) click to toggle source

Check if a remote process exists using its pid file

# File lib/capistrano/unicorn/utility.rb, line 42
def remote_process_exists?(pid_file)
  test("[ -e #{pid_file} ]") && execute("#{try_unicorn_user} kill -0 `cat #{pid_file}` > /dev/null 2>&1")
end
start_unicorn() click to toggle source

Start the Unicorn server

# File lib/capistrano/unicorn/utility.rb, line 106
def start_unicorn
  if test("[ -e #{fetch(:unicorn_config_stage_file_path)} ]")
    unicorn_config_file_path = fetch(:unicorn_config_stage_file_path)
  elsif test("[ -e #{fetch(:unicorn_config_file_path)} ]")
    unicorn_config_file_path = fetch(:unicorn_config_file_path)
  else
    fail "Config file for \"#{fetch(:unicorn_env)}\" environment was not found at either \"#{fetch(:unicorn_config_file_path)}\" or \"#{fetch(:unicorn_config_stage_file_path)}\""
  end

  if test("[ -e #{fetch(:unicorn_pid)} ]")
    if unicorn_is_running?
      puts 'Unicorn is already running!'
      return
    else
      execute :rm, fetch(:unicorn_pid)
    end
  end

  puts 'Starting unicorn...'

  within fetch(:app_path) do
    if fetch(:bundle_gemfile).nil?
      with rails_env: fetch(:rails_env) do
        execute :bundle, 'exec', fetch(:unicorn_bin), '-c', unicorn_config_file_path, '-E', fetch(:unicorn_rack_env), '-D', fetch(:unicorn_options)
      end
    else
      with rails_env: fetch(:rails_env), bundle_gemfile: fetch(:bundle_gemfile) do
        execute :bundle, 'exec', fetch(:unicorn_bin), '-c', unicorn_config_file_path, '-E', fetch(:unicorn_rack_env), '-D', fetch(:unicorn_options)
      end
    end
  end
end
try_unicorn_user() click to toggle source

Run a command as the :unicorn_user user if :unicorn_user is a string. Otherwise run as default (:user) user.

# File lib/capistrano/unicorn/utility.rb, line 85
def try_unicorn_user
  if unicorn_user = fetch(:unicorn_user)
    "sudo -u #{unicorn_user}"
  else
    ''
  end
end
unicorn_is_running?() click to toggle source

Command to check if Unicorn is running

# File lib/capistrano/unicorn/utility.rb, line 54
def unicorn_is_running?
  remote_process_exists?(fetch(:unicorn_pid))
end
unicorn_roles() click to toggle source
# File lib/capistrano/unicorn/utility.rb, line 147
def unicorn_roles
  Proc.new{ fetch(:unicorn_roles, :app) }.call
end
unicorn_send_signal(signal, pid=get_unicorn_pid) click to toggle source

Send a signal to a unicorn master processes

# File lib/capistrano/unicorn/utility.rb, line 78
def unicorn_send_signal(signal, pid=get_unicorn_pid)
  execute try_unicorn_user, 'kill', '-s', signal, pid
end