Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unicorn pid auto-detection behavior #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions lib/capistrano-unicorn/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not keen on this - why did you remove the error logging?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it's just mistake. I will revert it.

end
end
end
Expand Down
37 changes: 11 additions & 26 deletions lib/capistrano-unicorn/utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can you explain why these 3 lines need to be changed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • To assure all paths auto-detection uses is absolute.
  • The method name doesn't include _rel_ so it leads to thinking the method returns absolute path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so there are two separate issues: one is finding the path to the unicorn config file (like @michalorman experienced), and the other is finding the path for the pid file, right? If so, these should be handled in separate commits.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching from -c to -e sounds like a nice trick, but it's not at all clear from your commit what effect this has. Please can you provide (much) more detail in the commit message to help us review this? Thanks.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using -e has some advantage compared to -c:

-e executes argument code immediately and don't execute any extra code

See here. In contrast -c executes some bootstrap process.

-c strategy on server-side is impossible to read

To run the -c and Tmpfile strategy on server-side, We have to write shell script to execute ruby code to create tmp config file and then execute unicorn -c.

It's too complicated to read code.

pid == "unset" ? nil : File.expand_path(pid, app_path)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is probably the culprit which caused #85 in the case where the pid file is relative.


# Check if a remote process exists using its pid file
Expand Down
30 changes: 7 additions & 23 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this looks sloppy to me. It means that most of the code in extract_pid_file and all of the code in local_unicorn_config is no longer tested.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better solution is to add new spec file utility_spec.rb and test local_unicorn_config and extract_pid_file separately. Any thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably fine, as long as all the code paths get tested.

@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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary_exists parameter should not be removed, otherwise the conditional in local_unicorn_config cannot be tested properly.


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"
Expand Down