Skip to content

Instantly share code, notes, and snippets.

@mashijp
Created January 27, 2014 03:13
Show Gist options
  • Save mashijp/8642762 to your computer and use it in GitHub Desktop.
Save mashijp/8642762 to your computer and use it in GitHub Desktop.
namespace :app do
desc 'アプリケーションの再起動'
task :restart do
on release_roles :app do
fullpath_pidfile_default = "#{current_path}/shared/#{fetch :app_pidfile_default}"
fullpath_pidfile_sub = "#{current_path}/shared/#{fetch :app_pidfile_sub}"
now_running = nil
if test "[ -f #{fullpath_pidfile_default} ]"
now_running = :default
elsif test "[ -f #{fullpath_pidfile_sub} ]"
now_running = :sub
end
next_running = now_running == :default ? :sub : :default
next_port = next_running == :default ? (fetch :app_port_default) : (fetch :app_port_sub)
execute "cd #{current_path}; ( ( nohup ./bin/#{fetch :application} "+
"-Dhttp.port=#{next_port} -Dconfig.file=shared/environment.conf "+
"-Dpidfile.path=#{next_running == :default ? fullpath_pidfile_default : fullpath_pidfile_sub} "+
"&>/dev/null 2>./logs/stderr.log ) & echo $!)"
sleep 5
tried = 0
while test "[ $(curl -i http://localhost:#{next_port}/ -S -s -o /dev/null -w '%{http_code}') -ne 200 ]"
tried += 1
raise "MAX RETRY EXCEEDED!" if tried > 10
sleep 3
end
execute "cat #{current_path}/nginx/backend.conf.template | "+
"sed -e 's/%HTTP_PORT%/#{next_port}/g' "+
"> #{current_path}/nginx/backend.conf"
sudo "service nginx upgrade"
if now_running
tried = 0
while tried < 10
sleep 5
if test "[ \"$(ps aux | grep nginx | grep 'shutting down' | grep -v grep | wc -l)\" -eq 0 ]"
break
else
tried += 1
end
end
execute :kill, "`cat #{now_running == :default ? fullpath_pidfile_default : fullpath_pidfile_sub}`"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment