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
142 143 144 145 146 147 148 |
# 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 ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 ⇒ Object
Get unicorn master (old) process PID
78 79 80 81 82 |
# 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)) ⇒ Object
Get unicorn master process PID (using the shell)
70 71 72 73 74 |
# 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) ⇒ Object
Kill Unicorns in multiple ways O_O
104 105 106 107 108 109 110 111 |
# 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 ⇒ Object
In Capistrano 3, shell scripts must be invoked with SSHKit’s execute, instead of run.
6 7 8 9 10 11 12 |
# 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? ⇒ Boolean
Command to check if stale Unicorn is running
64 65 66 |
# File 'lib/capistrano/unicorn/utility.rb', line 64 def old_unicorn_is_running? remote_process_exists?(old_unicorn_pid) end |
#old_unicorn_pid ⇒ Object
Stale Unicorn process pid file
52 53 54 |
# File 'lib/capistrano/unicorn/utility.rb', line 52 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
42 43 44 45 46 47 48 |
# 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 ⇒ Object
Start the Unicorn server
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# 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 ⇒ Object
Run a command as the :unicorn_user user if :unicorn_user is a string. Otherwise run as default (:user) user.
94 95 96 97 98 99 100 |
# 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? ⇒ Boolean
Command to check if Unicorn is running
58 59 60 |
# File 'lib/capistrano/unicorn/utility.rb', line 58 def unicorn_is_running? remote_process_exists?(fetch(:unicorn_pid)) end |
#unicorn_roles ⇒ Object
150 151 152 |
# File 'lib/capistrano/unicorn/utility.rb', line 150 def unicorn_roles fetch(:unicorn_roles, :app) end |
#unicorn_send_signal(signal, pid = get_unicorn_pid) ⇒ Object
Send a signal to a unicorn master processes
86 87 88 89 |
# 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 |