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 = <<-END
if #{unicorn_is_running?}; then
echo "Stopping Unicorn...";
#{unicorn_send_signal(signal)};
else
echo "Unicorn is not running.";
fi;
END
script
end
def start_unicorn
primary_config_path = "#{current_path}/config/unicorn.rb"
secondary_config_path = "#{current_path}/config/unicorn/#{unicorn_env}.rb"
script = <<-END
if [ -e #{primary_config_path} ]; then
UNICORN_CONFIG_PATH=#{primary_config_path};
else
if [ -e #{secondary_config_path} ]; then
UNICORN_CONFIG_PATH=#{secondary_config_path};
else
echo "Config file for \"#{unicorn_env}\" environment was not found at either \"#{primary_config_path}\" or \"#{secondary_config_path}\"";
exit 1;
fi;
fi;
if [ -e #{unicorn_pid} ]; then
if kill -0 `cat #{unicorn_pid}` > /dev/null 2>&1; then
echo "Unicorn is already running!";
exit 0;
fi;
rm #{unicorn_pid};
fi;
echo "Starting Unicorn...";
cd #{current_path} && BUNDLE_GEMFILE=#{current_path}/Gemfile #{unicorn_bundle} exec #{unicorn_bin} -c $UNICORN_CONFIG_PATH -E #{app_env} -D;
END
script
end
namespace :unicorn do
desc 'Start Unicorn master process'
task :start, :roles => :app, :except => {:no_release => true} do
run start_unicorn
end
desc 'Stop Unicorn'
task :stop, :roles => :app, :except => {:no_release => true} do
run kill_unicorn('QUIT')
end
desc 'Immediately shutdown Unicorn'
task :shutdown, :roles => :app, :except => {:no_release => true} do
run kill_unicorn('TERM')
end
desc 'Restart Unicorn'
task :restart, :roles => :app, :except => {:no_release => true} do
run <<-END
if #{unicorn_is_running?}; then
echo "Restarting Unicorn...";
#{unicorn_send_signal('USR2')};
else
#{start_unicorn}
fi;
sleep 2; # in order to wait for the (old) pidfile to show up
if #{old_unicorn_is_running?}; then
#{unicorn_send_signal('QUIT', get_old_unicorn_pid)};
fi;
END
end
desc 'Reload Unicorn'
task :reload, :roles => :app, :except => {:no_release => true} do
run <<-END
if #{unicorn_is_running?}; then
echo "Reloading Unicorn...";
#{unicorn_send_signal('HUP')};
else
#{start_unicorn}
fi;
END
end
desc 'Add a new worker'
task :add_worker, :roles => :app, :except => {:no_release => true} do
run <<-END
if #{unicorn_is_running?}; then
echo "Adding a new Unicorn worker...";
#{unicorn_send_signal('TTIN')};
else
echo "Unicorn is not running.";
fi;
END
end
desc 'Remove amount of workers'
task :remove_worker, :roles => :app, :except => {:no_release => true} do
run <<-END
if #{unicorn_is_running?}; then
echo "Removing a Unicorn worker...";
#{unicorn_send_signal('TTOU')};
else
echo "Unicorn is not running.";
fi;
END
end
end
end
end
|