From d6d09d9f998e1af34d8c668c4db210ec974d4e1b Mon Sep 17 00:00:00 2001 From: Kazunori Kajihiro Date: Fri, 13 Dec 2013 14:23:56 +0900 Subject: [PATCH 1/3] Fix unicorn pid auto-detection behavior * run auto-detection on remote * convert relative path to absolute * use `unicorn -e` instead of `unicorn -c` with Tempfile --- lib/capistrano-unicorn/config.rb | 11 +-------- lib/capistrano-unicorn/utility.rb | 37 +++++++++---------------------- spec/config_spec.rb | 30 ++++++------------------- 3 files changed, 19 insertions(+), 59 deletions(-) diff --git a/lib/capistrano-unicorn/config.rb b/lib/capistrano-unicorn/config.rb index d399864..2fa5b08 100644 --- a/lib/capistrano-unicorn/config.rb +++ b/lib/capistrano-unicorn/config.rb @@ -36,16 +36,7 @@ def self.load(config) _cset(:unicorn_config_stage_file_path) \ { app_path + '/' + unicorn_config_stage_rel_file_path } _cset(:unicorn_default_pid) { app_path + "/tmp/pids/unicorn.pid" } - _cset(:unicorn_pid) do - extracted_pid = extract_pid_file - if extracted_pid - extracted_pid - else - logger.important "err :: failed to auto-detect pid from #{local_unicorn_config}" - logger.important "err :: falling back to default: #{unicorn_default_pid}" - unicorn_default_pid - end - end + _cset(:unicorn_pid) { extract_pid_file || unicorn_default_pid } end end end diff --git a/lib/capistrano-unicorn/utility.rb b/lib/capistrano-unicorn/utility.rb index 1a56948..ef78e09 100644 --- a/lib/capistrano-unicorn/utility.rb +++ b/lib/capistrano-unicorn/utility.rb @@ -2,35 +2,20 @@ module CapistranoUnicorn module Utility def local_unicorn_config - File.exist?(unicorn_config_rel_file_path) ? - unicorn_config_rel_file_path - : unicorn_config_stage_rel_file_path + File.exist?(unicorn_config_file_path) ? + unicorn_config_file_path + : unicorn_config_stage_file_path end def extract_pid_file - tmp = Tempfile.new('unicorn.rb') - begin - conf = local_unicorn_config - tmp.write <<-EOF.gsub(/^ */, '') - config_file = "#{conf}" - - # stub working_directory to avoid chdir failure since this will - # run client-side: - def working_directory(path); end - - instance_eval(File.read(config_file), config_file) if config_file - puts set[:pid] - exit 0 - EOF - tmp.close - extracted_pid = `unicorn -c "#{tmp.path}"` - $?.success? ? extracted_pid.rstrip : nil - rescue StandardError => e - return nil - ensure - tmp.close - tmp.unlink - end + code = <<-EOC.gsub(/^ */, '').gsub(/\n/, '; ') + cfg = Unicorn::Configurator.new(:config_file => '#{local_unicorn_config}') + puts cfg.set[:pid] + exit 0 + EOC + + pid = capture("cd #{current_path} && unicorn -e \"#{code}\"", :roles => unicorn_roles).rstrip + pid == "unset" ? nil : File.expand_path(pid, app_path) end # Check if a remote process exists using its pid file diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 7fecf13..d891e23 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -32,39 +32,23 @@ end it "should default to a sensible pid file when auto-detection failed" do - @configuration.should_receive(shell).with(/unicorn -c /).and_return('') do |cmd| - `false` # Simulate failure by setting $? - end + @configuration.should_receive(:capture).and_return('unset') @configuration.logger.stub(:important) @configuration.fetch(:unicorn_pid).should == app_path + "/tmp/pids/unicorn.pid" end - shared_examples "auto-detect pid file from unicorn config" do - |pid_file, primary_exists, config_file| - which_config = primary_exists ? 'primary' : 'stage' - it "should auto-detect pid file from #{which_config} unicorn config" do - # Tempfile.new in Ruby 1.9.2 will call File.exist? - allow(File).to receive(:exist?).with(/tmp/) - - File.should_receive(:exist?).with('config/unicorn.rb').and_return(primary_exists) - tmpfile = nil - @configuration.should_receive(shell).with(/unicorn -c /) do |cmd| - (cmd =~ /^unicorn -c "(.+)"$/).should be_true - tmpfile = $~[1] - tmpfile.should include("tmp") - File.read(tmpfile).should include(%!config_file = "#{config_file}"!) - `true` # Simulate success by setting $? - pid_file - end - @configuration.fetch(:unicorn_pid).should == pid_file + shared_examples "auto-detect pid file from unicorn config" do |pid_file| + it "should auto-detect pid file from unicorn config" do + @configuration.should_receive(:capture).and_return(pid_file) + @configuration.fetch(:unicorn_pid).should == File.expand_path(pid_file, app_path) end end include_examples "auto-detect pid file from unicorn config", \ - '/path/to/pid/from/config/file', true, "config/unicorn.rb" + '/path/to/pid/from/config/file' include_examples "auto-detect pid file from unicorn config", \ - '/path/to/pid/from/stage/config/file', false, "config/unicorn/production.rb" + 'relative/path/to/pid' specify "Gemfile should default correctly" do @configuration.fetch(:bundle_gemfile).should == app_path + "/Gemfile" From bcdf2629d81c14f4d32f79edd04eb749ff951af2 Mon Sep 17 00:00:00 2001 From: Kazunori Kajihiro Date: Mon, 16 Dec 2013 15:42:19 +0900 Subject: [PATCH 2/3] Revert deleting auto-detection failure log --- lib/capistrano-unicorn/config.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/capistrano-unicorn/config.rb b/lib/capistrano-unicorn/config.rb index 2fa5b08..d399864 100644 --- a/lib/capistrano-unicorn/config.rb +++ b/lib/capistrano-unicorn/config.rb @@ -36,7 +36,16 @@ def self.load(config) _cset(:unicorn_config_stage_file_path) \ { app_path + '/' + unicorn_config_stage_rel_file_path } _cset(:unicorn_default_pid) { app_path + "/tmp/pids/unicorn.pid" } - _cset(:unicorn_pid) { extract_pid_file || unicorn_default_pid } + _cset(:unicorn_pid) do + extracted_pid = extract_pid_file + if extracted_pid + extracted_pid + else + logger.important "err :: failed to auto-detect pid from #{local_unicorn_config}" + logger.important "err :: falling back to default: #{unicorn_default_pid}" + unicorn_default_pid + end + end end end end From 95e48349abd5bca052e0dce2aa6dd89b83dabfb4 Mon Sep 17 00:00:00 2001 From: Kazunori Kajihiro Date: Mon, 16 Dec 2013 15:43:31 +0900 Subject: [PATCH 3/3] Use app_path instead of current_path --- lib/capistrano-unicorn/utility.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/capistrano-unicorn/utility.rb b/lib/capistrano-unicorn/utility.rb index ef78e09..7e7ea6a 100644 --- a/lib/capistrano-unicorn/utility.rb +++ b/lib/capistrano-unicorn/utility.rb @@ -14,7 +14,7 @@ def extract_pid_file exit 0 EOC - pid = capture("cd #{current_path} && unicorn -e \"#{code}\"", :roles => unicorn_roles).rstrip + pid = capture("cd #{app_path} && unicorn -e \"#{code}\"", :roles => unicorn_roles).rstrip pid == "unset" ? nil : File.expand_path(pid, app_path) end