Skip to content

Commit

Permalink
attempt to auto-detect path of pid file (fixes sosedoff#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Spiers committed Aug 24, 2013
1 parent 6258ea2 commit 81407b0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Expand Up @@ -14,6 +14,9 @@ Significant changes since 0.1.10. Backwards-incompatible changes are *in bold*.
* Add `unicorn_options` variable which allows passing of arbitrary options to unicorn.
* Added `app_subdir` to support app running in a subdirectory.
* Updated documentation in [README](README.md) to fix inaccuracies and ambiguities.
* `unicorn_pid` defaults to attempting to auto-detect from unicorn config file.
This avoids having to keep two paths in sync.
https://github.com/sosedoff/capistrano-unicorn/issues/7
* Also added the `unicorn:show_vars` task to make it easier to debug
config variable values client-side.
* Defer calculation of `unicorn~roles`.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -87,7 +87,8 @@ You can modify any of the following Capistrano variables in your `deploy.rb` con
### Absolute path parameters

- `app_path` - Set path to app root. Default to `current_path + app_subdir`.
- `unicorn_pid` - Set unicorn PID file path. Default to `#{current_path}/tmp/pids/unicorn.pid`
- `unicorn_pid` - Set unicorn PID file path. By default, attempts to auto-detect from unicorn config file. On failure, falls back to value in `unicorn_default_pid`
- `unicorn_default_pid` - See above. Default to `current_path/tmp/pids/unicorn.pid`
- `bundle_gemfile` - Set path to Gemfile. Default to `#{app_path}/Gemfile`
- `unicorn_config_path` - Set the directory where unicorn config files reside. Default to `#{current_path}/config`.

Expand Down
46 changes: 45 additions & 1 deletion lib/capistrano-unicorn/capistrano_integration.rb
@@ -1,3 +1,5 @@
require 'tempfile'

require 'capistrano'
require 'capistrano/version'

Expand Down Expand Up @@ -46,12 +48,54 @@ def self.load_into(capistrano_config)
# If you find the following confusing, try running 'cap unicorn:show_vars' -
# it might help :-)
_cset(:app_path) { current_path + app_subdir }
_cset(:unicorn_pid) { app_path + "/tmp/pids/unicorn.pid" }
_cset(:bundle_gemfile) { app_path + '/Gemfile' }
_cset(:unicorn_config_path) { app_path + '/' + unicorn_config_rel_path }
_cset(:unicorn_config_file_path) { app_path + '/' + unicorn_config_rel_file_path }
_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
end

def local_unicorn_config
File.exist?(unicorn_config_rel_file_path) ?
unicorn_config_rel_file_path
: unicorn_config_stage_rel_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}"`.rstrip
$?.success? ? extracted_pid : nil
rescue
return nil
ensure
tmp.close
tmp.unlink
end
end

# Check if a remote process exists using its pid file
Expand Down

0 comments on commit 81407b0

Please sign in to comment.