Module: CapistranoUnicorn::Utility

Defined in:
lib/capistrano-unicorn/utility.rb

Instance Method Summary collapse

Instance Method Details

#duplicate_unicornObject



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/capistrano-unicorn/utility.rb', line 129

def duplicate_unicorn
  script = <<-END
    if #{unicorn_is_running?}; then
      echo "Duplicating Unicorn...";
      #{unicorn_send_signal('USR2')};
    else
      #{start_unicorn}
    fi;
  END

  script
end

#extract_pid_fileObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/capistrano-unicorn/utility.rb', line 10

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_pidObject

Get unicorn master (old) process PID



68
69
70
# File 'lib/capistrano-unicorn/utility.rb', line 68

def get_old_unicorn_pid
  get_unicorn_pid(old_unicorn_pid)
end

#get_unicorn_pid(pid_file = unicorn_pid) ⇒ Object

Get unicorn master process PID (using the shell)



62
63
64
# File 'lib/capistrano-unicorn/utility.rb', line 62

def get_unicorn_pid(pid_file=unicorn_pid)
  "`cat #{pid_file}`"
end

#kill_unicorn(signal) ⇒ Object

Kill Unicorns in multiple ways O_O



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/capistrano-unicorn/utility.rb', line 87

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
end

#local_unicorn_configObject



4
5
6
7
8
# File 'lib/capistrano-unicorn/utility.rb', line 4

def local_unicorn_config
  File.exist?(unicorn_config_rel_file_path) ?
      unicorn_config_rel_file_path
    : unicorn_config_stage_rel_file_path
end

#old_unicorn_is_running?Boolean

Command to check if stale Unicorn is running

Returns:

  • (Boolean)


56
57
58
# File 'lib/capistrano-unicorn/utility.rb', line 56

def old_unicorn_is_running?
  remote_process_exists?(old_unicorn_pid)
end

#old_unicorn_pidObject

Stale Unicorn process pid file



44
45
46
# File 'lib/capistrano-unicorn/utility.rb', line 44

def old_unicorn_pid
  "#{unicorn_pid}.oldbin"
end

#remote_process_exists?(pid_file) ⇒ Boolean

Check if a remote process exists using its pid file

Returns:

  • (Boolean)


38
39
40
# File 'lib/capistrano-unicorn/utility.rb', line 38

def remote_process_exists?(pid_file)
  "[ -e #{pid_file} ] && #{try_unicorn_user} kill -0 `cat #{pid_file}` > /dev/null 2>&1"
end

#start_unicornObject

Start the Unicorn server



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/capistrano-unicorn/utility.rb', line 102

def start_unicorn
  %Q%
    if [ -e "#{unicorn_config_file_path}" ]; then
      UNICORN_CONFIG_PATH=#{unicorn_config_file_path};
    else
      if [ -e "#{unicorn_config_stage_file_path}" ]; then
        UNICORN_CONFIG_PATH=#{unicorn_config_stage_file_path};
      else
        echo "Config file for "#{unicorn_env}" environment was not found at either "#{unicorn_config_file_path}" or "#{unicorn_config_stage_file_path}"";
        exit 1;
      fi;
    fi;

    if [ -e "#{unicorn_pid}" ]; then
      if #{try_unicorn_user} kill -0 `cat #{unicorn_pid}` > /dev/null 2>&1; then
        echo "Unicorn is already running!";
        exit 0;
      fi;

      #{try_unicorn_user} rm #{unicorn_pid};
    fi;

    echo "Starting Unicorn...";
    cd #{app_path} && #{try_unicorn_user} RAILS_ENV=#{rails_env} BUNDLE_GEMFILE=#{bundle_gemfile} #{unicorn_bundle} exec #{unicorn_bin} -c $UNICORN_CONFIG_PATH -E #{unicorn_rack_env} -D #{unicorn_options};
  %
end

#try_unicorn_userObject

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



81
82
83
# File 'lib/capistrano-unicorn/utility.rb', line 81

def try_unicorn_user
  "#{sudo :as => unicorn_user.to_s}" if unicorn_user.kind_of?(String)
end

#unicorn_is_running?Boolean

Command to check if Unicorn is running

Returns:

  • (Boolean)


50
51
52
# File 'lib/capistrano-unicorn/utility.rb', line 50

def unicorn_is_running?
  remote_process_exists?(unicorn_pid)
end

#unicorn_rolesObject



142
143
144
# File 'lib/capistrano-unicorn/utility.rb', line 142

def unicorn_roles
  defer{ fetch(:unicorn_roles, :app) }
end

#unicorn_send_signal(signal, pid = get_unicorn_pid) ⇒ Object

Send a signal to a unicorn master processes



74
75
76
# File 'lib/capistrano-unicorn/utility.rb', line 74

def unicorn_send_signal(signal, pid=get_unicorn_pid)
  "#{try_unicorn_user} kill -s #{signal} #{pid}"
end