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

Add support for Unicorn sockets #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ And load it into your deployment script `config/deploy.rb`:
require 'capistrano-unicorn'
```

Add unicorn setup task hook:

```ruby
after 'deploy:setup', 'unicorn:setup' # Needed if you've configured Unicorn to use a UNIX Socket file.
```

Add unicorn restart task hook:

```ruby
after 'deploy:restart', 'unicorn:reload' # app IS NOT preloaded
after 'deploy:restart', 'unicorn:restart' # app preloaded
```

Add unicorn shared sockets hook:

```ruby
after 'deploy:finalize_update', 'unicorn:link_socket_dir' # Needed if you've configured Unicorn to use a UNIX Socket file
```

Create a new configuration file `config/unicorn.rb` or `config/unicorn/STAGE.rb`, where stage is your deployment environment.

Example config - [examples/rails3.rb](https://github.com/sosedoff/capistrano-unicorn/blob/master/examples/rails3.rb). Please refer to unicorn documentation for more examples and configuration options.
Expand All @@ -59,14 +71,15 @@ cap unicorn:reload

You can modify any of the following options in your `deploy.rb` config.

- `unicorn_env` - Set unicorn environment. Default to `rails_env` variable.
- `unicorn_pid` - Set unicorn PID file path. Default to `current_path/tmp/pids/unicorn.pid`
- `unicorn_bin` - Set unicorn executable file. Default to `unicorn`.
- `unicorn_bundle` - Set bundler command for unicorn. Default to `bundle`.
- `unicorn_user` - Launch unicorn master as the specified user. Default to `user` variable.
- `unicorn_roles` - Define which roles to perform unicorn recpies on. Default to `:app`.
- `unicorn_config_path` - Set the directory where unicorn config files reside. Default to `current_path/config`.
- `unicorn_config_filename` - Set the filename of the unicorn config file. Not used in multistage installations. Default to `unicorn.rb`.
- `unicorn_env` - Set unicorn environment. Default to `rails_env` variable.
- `unicorn_pid` - Set unicorn PID file path. Default to `current_path/tmp/pids/unicorn.pid`
- `unicorn_bin` - Set unicorn executable file. Default to `unicorn`.
- `unicorn_bundle` - Set bundler command for unicorn. Default to `bundle`.
- `unicorn_user` - Launch unicorn master as the specified user. Default to `user` variable.
- `unicorn_roles` - Define which roles to perform unicorn recpies on. Default to `:app`.
- `unicorn_config_path` - Set the directory where unicorn config files reside. Default to `current_path/config`.
- `unicorn_config_filename` - Set the filename of the unicorn config file. Not used in multistage installations. Default to `unicorn.rb`.
- `unicorn_shared_socket_dir` - Set the shared socket path where the unicorn sockets should live. Default to `shared_path/sockets`.

### Multistage

Expand All @@ -84,6 +97,8 @@ cap unicorn:restart # Restart Unicorn
cap unicorn:shutdown # Immediately shutdown Unicorn
cap unicorn:start # Start Unicorn master process
cap unicorn:stop # Stop Unicorn
cap unicorn:setup # Setup the project (i.e. create the shared sockets directory)
cap unicorn:link_socket_dir # Link the shared sockets directory to tmp/sockets
```

## License
Expand Down
16 changes: 15 additions & 1 deletion lib/capistrano-unicorn/capistrano_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class CapistranoIntegration
'unicorn:reload',
'unicorn:shutdown',
'unicorn:add_worker',
'unicorn:remove_worker'
'unicorn:remove_worker',
'unicorn:setup',
'unicorn:link_socket_dir'
]

def self.load_into(capistrano_config)
Expand All @@ -26,6 +28,7 @@ def self.load_into(capistrano_config)
_cset(:unicorn_user) { nil }
_cset(:unicorn_config_path) { "#{fetch(:current_path)}/config" }
_cset(:unicorn_config_filename) { "unicorn.rb" }
_cset(:unicorn_shared_socket_dir) { "#{fetch(:shared_path)}/sockets" }
end

# Check if a remote process exists using its pid file
Expand Down Expand Up @@ -215,6 +218,17 @@ def unicorn_roles
fi;
END
end

desc 'Setup project to use Unicorn'
task :setup, :roles => unicorn_roles, :except => {:no_release => true} do
# Create a sockets directory if one doesn't already exist
run "mkdir -p #{unicorn_shared_socket_dir}"
end

desc 'Link unicorn_shared_socket_dir to tmp/sockets'
task :link_socket_dir, :roles => unicorn_roles, :except => {:no_release => true} do
run "ln -s #{unicorn_shared_socket_dir} #{latest_release}/tmp/sockets"
end
end
end
end
Expand Down