From acd0f29fe49e8093de62623ebaf611332f82a801 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 6 Jun 2012 14:15:02 -0400 Subject: [PATCH] better restarts --- lib/capistrano-thin.rb | 60 ++++++++++++++++++++++++++++++++++++++---- skel/god/thin.god.erb | 14 ++++++++++ skel/thin/thin.yml.erb | 15 +++++++++++ 3 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 skel/god/thin.god.erb create mode 100644 skel/thin/thin.yml.erb diff --git a/lib/capistrano-thin.rb b/lib/capistrano-thin.rb index da71347..9697a26 100644 --- a/lib/capistrano-thin.rb +++ b/lib/capistrano-thin.rb @@ -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 diff --git a/skel/god/thin.god.erb b/skel/god/thin.god.erb new file mode 100644 index 0000000..d509731 --- /dev/null +++ b/skel/god/thin.god.erb @@ -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 + diff --git a/skel/thin/thin.yml.erb b/skel/thin/thin.yml.erb new file mode 100644 index 0000000..4bb079d --- /dev/null +++ b/skel/thin/thin.yml.erb @@ -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 %> +