better restarts

This commit is contained in:
John Bintz 2012-06-06 14:15:02 -04:00
parent c03348938f
commit acd0f29fe4
3 changed files with 84 additions and 5 deletions

View File

@ -1,7 +1,21 @@
require 'erb'
Capistrano::Configuration.instance.load do
set(:thin_command) { 'bundle exec thin' }
set(:thin_config_file) { 'thin.yml' }
set(:thin_config) { "-C #{thin_config_file}" }
def self.cset(variable, *args, &block)
set(variable, *args, &block) if !exists?(variable)
end
cset(:thin_command) { 'bundle exec thin' }
cset(:thin_config_file) { "#{current_path}/thin.yml" }
cset(:thin_config) { "-C #{thin_config_file}" }
cset(:thin_port) { 3000 }
cset(:thin_pid) { 'tmp/pids/thin.pid' }
cset(:thin_log) { 'log/thin.log' }
cset(:thin_max_conns) { 1024 }
cset(:thin_max_persistent_conns) { 512 }
cset(:thin_servers) { 4 }
namespace :deploy do
task :start do
@ -13,9 +27,45 @@ Capistrano::Configuration.instance.load do
end
task :restart do
top.deploy.stop
top.deploy.start
run "cd #{current_path} && #{thin_command} #{thin_config} -O restart"
end
end
def skel_for(file)
File.join(File.expand_path('../../skel', __FILE__), file)
end
def render_skel(file, target)
run "mkdir -p #{File.dirname(target)}"
top.upload StringIO.new(ERB.new(File.read(skel_for(file))).result(binding)), target
end
def retrieve_env
fetch(:rails_env, fetch(:rack_env, fetch(:stage, :production)))
end
namespace :thin do
task :god do
render_skel "god/thin.god.erb", "#{shared_path}/god/#{application}-thin.god"
end
task :config do
render_skel "thin/thin.yml.erb", thin_config_file
end
task :shared_pids do
run "mkdir -p #{shared_path}/pids"
end
task :rolling_restart do
run "rm -Rf #{current_path}/tmp/pids && mkdir -p #{current_path}/tmp && ln -sf #{shared_path}/pids #{current_path}/tmp/pids"
top.deploy.restart
end
end
after 'deploy:setup', 'thin:god', 'thin:config', 'thin:shared_pids'
after 'deploy:symlink', 'thin:rolling_restart'
end

14
skel/god/thin.god.erb Normal file
View File

@ -0,0 +1,14 @@
God.watch do |w|
w.name = "<%= application %>-thin"
w.interval = 30.seconds
pid = "<%= thin_pid %>"
template = 'bash -c "cd <%= current_dir %> && RAILS_ENV=<%= retrieve_env %> <%= thin_command %> <%= thin_config %> %s"'
w.start = template % [ 'start' ]
w.stop = template % [ 'stop' ]
w.pid_file = pid
end

15
skel/thin/thin.yml.erb Normal file
View File

@ -0,0 +1,15 @@
pid: <%= thin_pid %>
address: 127.0.0.1
timeout: 30
wait: 30
port: <%= thin_port %>
log: <%= thin_log %>
max_conns: <%= thin_max_conns %>
require: []
environment: <%= retrieve_env %>
max_persistent_conns: <%= thin_max_persistent_conns %>
servers: <%= thin_servers %>
daemonize: true
chdir: <%= current_path %>