16
17
18
19
20
21
22
23
24
25
26
27
28
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/capistrano-unicorn/capistrano_integration.rb', line 16
def self.load_into(capistrano_config)
capistrano_config.load do
before(CapistranoIntegration::TASKS) do
_cset(:app_env) { (fetch(:rails_env) rescue 'production') }
_cset(:unicorn_pid) { "#{fetch(:current_path)}/tmp/pids/unicorn.pid" }
_cset(:unicorn_env) { fetch(:app_env) }
_cset(:unicorn_bin) { "unicorn" }
_cset(:unicorn_bundle) { fetch(:bundle_cmd) rescue 'bundle' }
end
def remote_process_exists?(pid_file)
"[ -e #{pid_file} ] && kill -0 `cat #{pid_file}` > /dev/null 2>&1"
end
def old_unicorn_pid
"#{unicorn_pid}.oldbin"
end
def unicorn_is_running?
remote_process_exists?(unicorn_pid)
end
def old_unicorn_is_running?
remote_process_exists?(old_unicorn_pid)
end
def get_unicorn_pid(pid_file=unicorn_pid)
"`cat #{pid_file}`"
end
def get_old_unicorn_pid
get_unicorn_pid(old_unicorn_pid)
end
def unicorn_send_signal(signal, pid=get_unicorn_pid)
"#{try_sudo} kill -s #{signal} #{pid}"
end
def kill_unicorn(signal)
script = " if \#{unicorn_is_running?}; then\n echo \"Stopping Unicorn...\";\n \#{unicorn_send_signal(signal)};\n else\n echo \"Unicorn is not running.\";\n fi;\n END\n\n script\n end\n\n # Start the Unicorn server\n #\n def start_unicorn\n primary_config_path = \"\#{current_path}/config/unicorn.rb\"\n secondary_config_path = \"\#{current_path}/config/unicorn/\#{unicorn_env}.rb\"\n\n script = <<-END\n if [ -e \#{primary_config_path} ]; then\n UNICORN_CONFIG_PATH=\#{primary_config_path};\n else\n if [ -e \#{secondary_config_path} ]; then\n UNICORN_CONFIG_PATH=\#{secondary_config_path};\n else\n echo \"Config file for \\\"\#{unicorn_env}\\\" environment was not found at either \\\"\#{primary_config_path}\\\" or \\\"\#{secondary_config_path}\\\"\";\n exit 1;\n fi;\n fi;\n\n if [ -e \#{unicorn_pid} ]; then\n if kill -0 `cat \#{unicorn_pid}` > /dev/null 2>&1; then\n echo \"Unicorn is already running!\";\n exit 0;\n fi;\n\n rm \#{unicorn_pid};\n fi;\n\n echo \"Starting Unicorn...\";\n cd \#{current_path} && BUNDLE_GEMFILE=\#{current_path}/Gemfile \#{unicorn_bundle} exec \#{unicorn_bin} -c $UNICORN_CONFIG_PATH -E \#{app_env} -D;\n END\n\n script\n end\n\n #\n # Unicorn cap tasks\n #\n namespace :unicorn do\n desc 'Start Unicorn master process'\n task :start, :roles => :app, :except => {:no_release => true} do\n run start_unicorn\n end\n\n desc 'Stop Unicorn'\n task :stop, :roles => :app, :except => {:no_release => true} do\n run kill_unicorn('QUIT')\n end\n\n desc 'Immediately shutdown Unicorn'\n task :shutdown, :roles => :app, :except => {:no_release => true} do\n run kill_unicorn('TERM')\n end\n\n desc 'Restart Unicorn'\n task :restart, :roles => :app, :except => {:no_release => true} do\n run <<-END\n if \#{unicorn_is_running?}; then\n echo \"Restarting Unicorn...\";\n \#{unicorn_send_signal('USR2')};\n else\n \#{start_unicorn}\n fi;\n\n sleep 2; # in order to wait for the (old) pidfile to show up\n\n if \#{old_unicorn_is_running?}; then\n \#{unicorn_send_signal('QUIT', get_old_unicorn_pid)};\n fi;\n END\n end\n\n desc 'Reload Unicorn'\n task :reload, :roles => :app, :except => {:no_release => true} do\n run <<-END\n if \#{unicorn_is_running?}; then\n echo \"Reloading Unicorn...\";\n \#{unicorn_send_signal('HUP')};\n else\n \#{start_unicorn}\n fi;\n END\n end\n\n desc 'Add a new worker'\n task :add_worker, :roles => :app, :except => {:no_release => true} do\n run <<-END\n if \#{unicorn_is_running?}; then\n echo \"Adding a new Unicorn worker...\";\n \#{unicorn_send_signal('TTIN')};\n else\n echo \"Unicorn is not running.\";\n fi;\n END\n end\n\n desc 'Remove amount of workers'\n task :remove_worker, :roles => :app, :except => {:no_release => true} do\n run <<-END\n if \#{unicorn_is_running?}; then\n echo \"Removing a Unicorn worker...\";\n \#{unicorn_send_signal('TTOU')};\n else\n echo \"Unicorn is not running.\";\n fi;\n END\n end\n end\n end\nend\n"
|