Module: CapistranoUnicorn::Utility
- Defined in:
- lib/capistrano/unicorn/utility.rb
Instance Method Summary collapse
- #duplicate_unicorn ⇒ Object
- #extract_pid_file ⇒ Object
-
#get_old_unicorn_pid ⇒ Object
Get unicorn master (old) process PID.
-
#get_unicorn_pid(pid_file = fetch(:unicorn_pid)) ⇒ Object
Get unicorn master process PID (using the shell).
-
#kill_unicorn(signal) ⇒ Object
Kill Unicorns in multiple ways O_O.
-
#local_unicorn_config ⇒ Object
In Capistrano 3, shell scripts must be invoked with SSHKit’s execute, instead of run.
-
#old_unicorn_is_running? ⇒ Boolean
Command to check if stale Unicorn is running.
-
#old_unicorn_pid ⇒ Object
Stale Unicorn process pid file.
-
#remote_process_exists?(pid_file) ⇒ Boolean
Check if a remote process exists using its pid file.
-
#start_unicorn ⇒ Object
Start the Unicorn server.
-
#try_unicorn_user ⇒ Object
Run a command as the :unicorn_user user if :unicorn_user is a string.
-
#unicorn_is_running? ⇒ Boolean
Command to check if Unicorn is running.
- #unicorn_roles ⇒ Object
-
#unicorn_send_signal(signal, pid = get_unicorn_pid) ⇒ Object
Send a signal to a unicorn master processes.
Instance Method Details
#duplicate_unicorn ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/capistrano/unicorn/utility.rb', line 148 def duplicate_unicorn script = <<-END if #{unicorn_is_running?}; then echo "Duplicating Unicorn..."; #{unicorn_send_signal('USR2')}; else #{start_unicorn} fi; END script.split.join(' ') end |
#extract_pid_file ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/capistrano/unicorn/utility.rb', line 29 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 ⇒ Object
Get unicorn master (old) process PID
87 88 89 |
# File 'lib/capistrano/unicorn/utility.rb', line 87 def get_old_unicorn_pid get_unicorn_pid(old_unicorn_pid) end |
#get_unicorn_pid(pid_file = fetch(:unicorn_pid)) ⇒ Object
Get unicorn master process PID (using the shell)
81 82 83 |
# File 'lib/capistrano/unicorn/utility.rb', line 81 def get_unicorn_pid(pid_file=fetch(:unicorn_pid)) "`cat #{pid_file}`" end |
#kill_unicorn(signal) ⇒ Object
Kill Unicorns in multiple ways O_O
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/capistrano/unicorn/utility.rb', line 107 def kill_unicorn(signal) script = <<-END if #{unicorn_is_running?}; then echo "Stopping Unicorn..."; #{unicorn_send_signal(signal)}; else echo "Unicorn is not running."; fi; END script.split.join(' ') end |
#local_unicorn_config ⇒ Object
In Capistrano 3, shell scripts must be invoked with SSHKit’s execute, instead of run. SSHKit will “sanitize” all multi-line commands (here docs), replacing “n” with “;”. Sanitizing renders some shell scripts illegal, for instance:
if [ -e FILE ]; then
echo "Found."
fi
This would become
if [ -e FILE ]; then; echo "Found."; fi;
which is illegal because of the ‘;’ after ‘then’.
To avoid errors, replace all “n” with “ ” in shell scripts, before SSHKit gets a chance to replace “n” with “;”
21 22 23 24 25 26 27 |
# File 'lib/capistrano/unicorn/utility.rb', line 21 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? ⇒ Boolean
Command to check if stale Unicorn is running
75 76 77 |
# File 'lib/capistrano/unicorn/utility.rb', line 75 def old_unicorn_is_running? remote_process_exists?(old_unicorn_pid) end |
#old_unicorn_pid ⇒ Object
Stale Unicorn process pid file
63 64 65 |
# File 'lib/capistrano/unicorn/utility.rb', line 63 def old_unicorn_pid "#{fetch :unicorn_pid}.oldbin" end |
#remote_process_exists?(pid_file) ⇒ Boolean
Check if a remote process exists using its pid file
57 58 59 |
# File 'lib/capistrano/unicorn/utility.rb', line 57 def remote_process_exists?(pid_file) "[ -e #{pid_file} ] && #{unicorn_send_signal(0, get_unicorn_pid(pid_file))} > /dev/null 2>&1" end |
#start_unicorn ⇒ Object
Start the Unicorn server
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/capistrano/unicorn/utility.rb', line 121 def start_unicorn %Q% if [ -e "#{fetch :unicorn_config_file_path}" ]; then UNICORN_CONFIG_PATH=#{fetch :unicorn_config_file_path}; else if [ -e "#{fetch :unicorn_config_stage_file_path}" ]; then UNICORN_CONFIG_PATH=#{fetch :unicorn_config_stage_file_path}; else echo "Config file for "#{fetch :unicorn_env}" environment was not found at either "#{fetch :unicorn_config_file_path}" or "#{fetch :unicorn_config_stage_file_path}""; exit 1; fi; fi; if [ -e "#{fetch :unicorn_pid}" ]; then if #{try_unicorn_user} kill -0 `cat #{fetch :unicorn_pid}` > /dev/null 2>&1; then echo "Unicorn is already running!"; exit 0; fi; #{try_unicorn_user} rm #{fetch :unicorn_pid}; fi; echo "Starting Unicorn..."; cd #{fetch :app_path} && #{try_unicorn_user} RAILS_ENV=#{fetch :rails_env} BUNDLE_GEMFILE=#{fetch :bundle_gemfile} #{fetch :unicorn_bundle} exec #{fetch :unicorn_bin} -c $UNICORN_CONFIG_PATH -E #{fetch :unicorn_rack_env} -D #{fetch :unicorn_options}; %.split.join(' ') end |
#try_unicorn_user ⇒ Object
Run a command as the :unicorn_user user if :unicorn_user is a string. Otherwise run as default (:user) user.
101 102 103 |
# File 'lib/capistrano/unicorn/utility.rb', line 101 def try_unicorn_user "#{sudo :as => fetch(:unicorn_user).to_s}" if fetch(:unicorn_user).kind_of?(String) end |
#unicorn_is_running? ⇒ Boolean
Command to check if Unicorn is running
69 70 71 |
# File 'lib/capistrano/unicorn/utility.rb', line 69 def unicorn_is_running? remote_process_exists?(fetch :unicorn_pid) end |
#unicorn_roles ⇒ Object
160 161 162 163 164 |
# File 'lib/capistrano/unicorn/utility.rb', line 160 def unicorn_roles # TODO proc necessary here? Proc.new{ fetch(:unicorn_roles, :app) }.call #defer{ fetch(:unicorn_roles, :app) } end |
#unicorn_send_signal(signal, pid = get_unicorn_pid) ⇒ Object
Send a signal to a unicorn master processes
93 94 95 96 |
# File 'lib/capistrano/unicorn/utility.rb', line 93 def unicorn_send_signal(signal, pid=get_unicorn_pid) sig_prefix = Integer === signal ? '-' : '-s ' "#{try_unicorn_user} kill #{sig_prefix}#{signal} #{pid}" end |