module CapistranoUnicorn::Utility
Public Instance Methods
duplicate_unicorn()
click to toggle source
# File lib/capistrano/unicorn/utility.rb, line 142 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 78 def get_old_unicorn_pid within fetch(:app_path) do get_unicorn_pid(old_unicorn_pid) end 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 70 def get_unicorn_pid(pid_file=fetch(:unicorn_pid)) within fetch(:app_path) do capture :cat, pid_file end end
kill_unicorn(signal)
click to toggle source
Kill Unicorns in multiple ways O_O
# File lib/capistrano/unicorn/utility.rb, line 104 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 64 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 52 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) begin execute(*try_unicorn_user, 'kill', '-0', get_unicorn_pid) if within(fetch(:app_path)) { test('[', '-e', pid_file, ']') } rescue SSHKit::Command::Failed => e false end end
start_unicorn()
click to toggle source
Start the Unicorn server
# File lib/capistrano/unicorn/utility.rb, line 115 def start_unicorn if test("[ -e #{fetch(:unicorn_config_file_path)} ]") unicorn_config_file_path = fetch(:unicorn_config_file_path) elsif test("[ -e #{fetch(:unicorn_config_stage_file_path)} ]") unicorn_config_file_path = fetch(:unicorn_config_stage_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 with rails_env: fetch(:rails_env), bundle_gemfile: fetch(:bundle_gemfile) do execute *try_unicorn_user, :bundle, 'exec', fetch(:unicorn_bin), '-c', unicorn_config_file_path, '-E', fetch(:unicorn_rack_env), '-D', fetch(:unicorn_options) 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 94 def try_unicorn_user if unicorn_user = fetch(:unicorn_user) [:sudo, '-Eu', 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 58 def unicorn_is_running? remote_process_exists?(fetch(:unicorn_pid)) end
unicorn_roles()
click to toggle source
# File lib/capistrano/unicorn/utility.rb, line 150 def unicorn_roles fetch(:unicorn_roles, :app) 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 86 def unicorn_send_signal(signal, pid=get_unicorn_pid) sig_prefix = Integer === signal ? '-' : '-s ' execute *try_unicorn_user, 'kill', sig_prefix, signal, pid end