module Mina::Unicorn::Utility

Public Instance Methods

kill_unicorn(signal) click to toggle source
# File lib/mina/unicorn/utility.rb, line 22
def kill_unicorn(signal)
  %{
    if #{unicorn_is_running?}; then
      echo "-----> Stopping Unicorn...";
      #{unicorn_send_signal(signal)};
    else
      echo "-----> Unicorn is not running.";
    fi
  }
end
restart_unicorn() click to toggle source
# File lib/mina/unicorn/utility.rb, line 33
def restart_unicorn
  %{
    #{duplicate_unicorn}

    sleep #{fetch(:unicorn_restart_sleep_time)}; # in order to wait for the (old) pidfile to show up

    if #{old_unicorn_is_running?}; then
      #{unicorn_send_signal("QUIT", get_old_unicorn_pid)};
    fi
  }
end
start_unicorn() click to toggle source
# File lib/mina/unicorn/utility.rb, line 6
def start_unicorn
  %{
    if [ -e "#{unicorn_pid}" ]; then
      if #{unicorn_user} kill -0 `cat #{unicorn_pid}` > /dev/null 2>&1; then
        echo "-----> Unicorn is already running!";
        exit 0;
      fi;

      #{unicorn_user} rm #{unicorn_pid};
    fi;

    echo "-----> Starting Unicorn...";
    cd #{fetch(:current_path)} && #{unicorn_user} BUNDLE_GEMFILE=#{fetch(:bundle_gemfile)} #{fetch(:unicorn_cmd)} -c #{fetch(:unicorn_config)} -E #{fetch(:unicorn_env)} -D
  }
end
unicorn_send_signal(signal, pid=get_unicorn_pid) click to toggle source

Send a signal to a unicorn master processes

# File lib/mina/unicorn/utility.rb, line 46
def unicorn_send_signal(signal, pid=get_unicorn_pid)
  "#{unicorn_user} kill -s #{signal} #{pid}"
end

Private Instance Methods

duplicate_unicorn() click to toggle source
# File lib/mina/unicorn/utility.rb, line 64
def duplicate_unicorn
  %{
    if #{unicorn_is_running?}; then
      echo "-----> Duplicating Unicorn...";
      #{unicorn_send_signal("USR2")};
    else
      #{start_unicorn}
    fi
  }
end
get_old_unicorn_pid() click to toggle source

Get unicorn master (old) process PID

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

Get unicorn master process PID (using the shell)

# File lib/mina/unicorn/utility.rb, line 102
def get_unicorn_pid(pid_file=unicorn_pid)
  "`cat #{pid_file}`"
end
old_unicorn_is_running?() click to toggle source

Command to check if stale Unicorn is running

# File lib/mina/unicorn/utility.rb, line 83
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/mina/unicorn/utility.rb, line 92
def old_unicorn_pid
  "#{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/mina/unicorn/utility.rb, line 60
def remote_process_exists?(pid_file)
  "[ -e #{pid_file} ] && #{unicorn_user} kill -0 `cat #{pid_file}` > /dev/null 2>&1"
end
unicorn_is_running?() click to toggle source

Command to check if Unicorn is running

# File lib/mina/unicorn/utility.rb, line 77
def unicorn_is_running?
  remote_process_exists?(unicorn_pid)
end
unicorn_pid() click to toggle source
# File lib/mina/unicorn/utility.rb, line 87
def unicorn_pid
  fetch(:unicorn_pid)
end
unicorn_user() click to toggle source

Run a command as the :unicorn_user user if :unicorn_user is set Otherwise run without sudo

# File lib/mina/unicorn/utility.rb, line 54
def unicorn_user
  "sudo -u #{fetch(:unicorn_user)}" if set?(:unicorn_user)
end