diff --git a/lib/capistrano-unicorn/utility.rb b/lib/capistrano-unicorn/utility.rb index 1a56948..7e7ea6a 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 #{app_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"