simple-capistrano-unicorn 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +29 -2
- data/configs/deploy.rb +60 -0
- data/configs/unicorn.conf.rb +118 -0
- data/lib/simple-capistrano-unicorn/namespace.rb +4 -1
- data/lib/simple-capistrano-unicorn/version.rb +1 -1
- metadata +3 -1
data/README.md
CHANGED
@@ -30,9 +30,35 @@ after :deploy, "unicorn:restart"
|
|
30
30
|
|
31
31
|
### 4. Add unicorn.rb (only for single-stage setup)
|
32
32
|
|
33
|
-
Grab the sample Unicorn configuration here:
|
33
|
+
Grab the sample Unicorn configuration here: https://github.com/kaspergrubbe/simple-capistrano-unicorn/blob/master/configs/unicorn.conf.rb
|
34
34
|
|
35
|
-
And place it here: `RAILS_ROOT/config/unicorn.rb`
|
35
|
+
And place it here: `RAILS_ROOT/config/unicorn.rb` change the `app_root` variable if you deploy user isn't named `deployer`.
|
36
|
+
|
37
|
+
#### 4.1 Should my Unicorn suicide or not?
|
38
|
+
|
39
|
+
My prefered way of killing off Unicorns is to let Unicorn kill it old master after forking, this means that workers is up when you kill off the old master. If you use the sample unicorn config described here, Unicorn is doing exactly this. You can see it by this:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
after_fork do |server, worker|
|
43
|
+
# (...)
|
44
|
+
|
45
|
+
# Kill off the new master after forking
|
46
|
+
old_pid = "#{app_dir}/shared/pids/unicorn.pid.oldbin"
|
47
|
+
if File.exists?(old_pid) && server.pid != old_pid
|
48
|
+
begin
|
49
|
+
Process.kill("QUIT", File.read(old_pid).to_i)
|
50
|
+
rescue Errno::ENOENT, Errno::ESRCH
|
51
|
+
# someone else did our job for us
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
If you want Unicorn to suicide, set this variable in your `deploy.rb`-file:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
set(:unicorn_suicide) { true }
|
61
|
+
```
|
36
62
|
|
37
63
|
### 5. Add unicorn stage files (only for multi-stage setup)
|
38
64
|
|
@@ -71,6 +97,7 @@ The gem gives you access to the following methods within the `unicorn.<method>`
|
|
71
97
|
You can customize the gems behavior by setting any (or all) of the following options within capistrano's configuration:
|
72
98
|
|
73
99
|
* `unicorn_pid` indicates the path for the pid file. Defaults to `"#{shared_path}/pids/unicorn.pid"`.
|
100
|
+
* `unicorn_suicide` indicates whether Unicorn kills it own master after forking or not. Defaults to `false`.
|
74
101
|
* `unicorn_old_pid` indicates the path for the old pid file, which Unicorn creates when forking a new master. Defaults to `#{shared_path}/pids/unicorn.pid.oldbin`.
|
75
102
|
* `unicorn_config` the path to the unicorn config file. Defaults to `"#{current_path}/config/unicorn.rb"`.
|
76
103
|
* `unicorn_log` the path where unicorn places its STDERR-log. Defaults to `"#{shared_path}/log/unicorn.stderr.log"`.
|
data/configs/deploy.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
set :application, "testapp"
|
2
|
+
set :repository, "git@github.com:kaspergrubbe/testapp.git"
|
3
|
+
set :branch, "master"
|
4
|
+
set :scm, :git
|
5
|
+
|
6
|
+
# Server settings
|
7
|
+
set :user, "deployer"
|
8
|
+
#set :ssh_options, { :forward_agent => true }
|
9
|
+
set :use_sudo, false
|
10
|
+
set :use_bundler, true
|
11
|
+
set :deploy_to, "/home/#{user}/apps/#{application}"
|
12
|
+
|
13
|
+
# Unicorn options
|
14
|
+
set :unicorn_suicide, true
|
15
|
+
|
16
|
+
role :web, "176.58.122.173" # Your HTTP server, Apache/etc
|
17
|
+
role :app, "176.58.122.173" # This may be the same as your `Web` server
|
18
|
+
role :db, "176.58.122.173", :primary => true # This is where Rails migrations will run
|
19
|
+
role :db, "176.58.122.173"
|
20
|
+
|
21
|
+
# if you want to clean up old releases on each deploy uncomment this:
|
22
|
+
after "deploy:restart", "deploy:cleanup"
|
23
|
+
|
24
|
+
# Restart unicorn after deploy
|
25
|
+
after :deploy, "unicorn:restart"
|
26
|
+
|
27
|
+
# The deploy strategies are:
|
28
|
+
# checkout (default) - This makes the servers do a git clone to update code
|
29
|
+
# export - This makes a git export instead of checkout (But what really happens is a checkout
|
30
|
+
# followed by a deletion of the .git-dirs, use checkout instead)
|
31
|
+
# remote_cache - This keeps a remote git repo on the servers, when deploying it does a git pull
|
32
|
+
# and copies the files to the release path.
|
33
|
+
# copy - This strategy checks out the branch to your local machine, compresses it, and copies
|
34
|
+
# the code to each server and uncompress it. This is smart when Github is failing.
|
35
|
+
# But if you live in Belgium and need to upload it to the danish servers, you might
|
36
|
+
# not want to use it.
|
37
|
+
#
|
38
|
+
# source: https://github.com/capistrano/capistrano/tree/master/lib/capistrano/recipes/deploy/strategy
|
39
|
+
# https://help.github.com/articles/deploying-with-capistrano
|
40
|
+
set :deploy_via, :copy
|
41
|
+
|
42
|
+
# rbenv
|
43
|
+
set :default_environment, {
|
44
|
+
"PATH" => "/home/#{user}/.rbenv/shims:/home/#{user}/.rbenv/bin:$PATH",
|
45
|
+
}
|
46
|
+
|
47
|
+
# if you're still using the script/reaper helper you will need
|
48
|
+
# these http://github.com/rails/irs_process_scripts
|
49
|
+
|
50
|
+
# If you are using Passenger mod_rails uncomment this:
|
51
|
+
# namespace :deploy do
|
52
|
+
# task :start do ; end
|
53
|
+
# task :stop do ; end
|
54
|
+
# task :restart, :roles => :app, :except => { :no_release => true } do
|
55
|
+
# run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
|
56
|
+
# end
|
57
|
+
# end
|
58
|
+
|
59
|
+
require "bundler/capistrano"
|
60
|
+
require 'simple-capistrano-unicorn'
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# config/unicorn.rb
|
2
|
+
|
3
|
+
# Sample verbose configuration file for Unicorn (not Rack)
|
4
|
+
#
|
5
|
+
# This configuration file documents many features of Unicorn
|
6
|
+
# that may not be needed for some applications. See
|
7
|
+
# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
|
8
|
+
# for a much simpler configuration file.
|
9
|
+
#
|
10
|
+
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
|
11
|
+
# documentation.
|
12
|
+
|
13
|
+
# Use at least one worker per core if you're on a dedicated server,
|
14
|
+
# more will usually help for _short_ waits on databases/caches.
|
15
|
+
worker_processes 4
|
16
|
+
|
17
|
+
app_name = "testapp"
|
18
|
+
app_dir = "/home/deployer/apps/#{app_name}"
|
19
|
+
|
20
|
+
# Since Unicorn is never exposed to outside clients, it does not need to
|
21
|
+
# run on the standard HTTP port (80), there is no reason to start Unicorn
|
22
|
+
# as root unless it's from system init scripts.
|
23
|
+
# If running the master process as root and the workers as an unprivileged
|
24
|
+
# user, do this to switch euid/egid in the workers (also chowns logs):
|
25
|
+
# user "unprivileged_user", "unprivileged_group"
|
26
|
+
|
27
|
+
# Help ensure your application will always spawn in the symlinked
|
28
|
+
# "current" directory that Capistrano sets up.
|
29
|
+
working_directory "#{app_dir}/current" # available in 0.94.0+
|
30
|
+
|
31
|
+
# listen on both a Unix domain socket and a TCP port,
|
32
|
+
# we use a shorter backlog for quicker failover when busy
|
33
|
+
listen "#{app_dir}/shared/system/unicorn.sock", :backlog => 64
|
34
|
+
listen 8080, :tcp_nopush => true
|
35
|
+
|
36
|
+
# nuke workers after 30 seconds instead of 60 seconds (the default)
|
37
|
+
timeout 30
|
38
|
+
|
39
|
+
# feel free to point this anywhere accessible on the filesystem
|
40
|
+
pid "#{app_dir}/shared/pids/unicorn.pid"
|
41
|
+
|
42
|
+
# By default, the Unicorn logger will write to stderr.
|
43
|
+
# Additionally, ome applications/frameworks log to stderr or stdout,
|
44
|
+
# so prevent them from going to /dev/null when daemonized here:
|
45
|
+
stderr_path "#{app_dir}/shared/log/unicorn.stderr.log"
|
46
|
+
stdout_path "#{app_dir}/shared/log/unicorn.stdout.log"
|
47
|
+
|
48
|
+
# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
|
49
|
+
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
|
50
|
+
preload_app true
|
51
|
+
GC.respond_to?(:copy_on_write_friendly=) and
|
52
|
+
GC.copy_on_write_friendly = true
|
53
|
+
|
54
|
+
# Enable this flag to have unicorn test client connections by writing the
|
55
|
+
# beginning of the HTTP headers before calling the application. This
|
56
|
+
# prevents calling the application for connections that have disconnected
|
57
|
+
# while queued. This is only guaranteed to detect clients on the same
|
58
|
+
# host unicorn runs on, and unlikely to detect disconnects even on a
|
59
|
+
# fast LAN.
|
60
|
+
check_client_connection false
|
61
|
+
|
62
|
+
before_fork do |server, worker|
|
63
|
+
# the following is highly recomended for Rails + "preload_app true"
|
64
|
+
# as there's no need for the master process to hold a connection
|
65
|
+
defined?(ActiveRecord::Base) and
|
66
|
+
ActiveRecord::Base.connection.disconnect!
|
67
|
+
|
68
|
+
# The following is only recommended for memory/DB-constrained
|
69
|
+
# installations. It is not needed if your system can house
|
70
|
+
# twice as many worker_processes as you have configured.
|
71
|
+
#
|
72
|
+
# # This allows a new master process to incrementally
|
73
|
+
# # phase out the old master process with SIGTTOU to avoid a
|
74
|
+
# # thundering herd (especially in the "preload_app false" case)
|
75
|
+
# # when doing a transparent upgrade. The last worker spawned
|
76
|
+
# # will then kill off the old master process with a SIGQUIT.
|
77
|
+
# old_pid = "#{server.config[:pid]}.oldbin"
|
78
|
+
# if old_pid != server.pid
|
79
|
+
# begin
|
80
|
+
# sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
|
81
|
+
# Process.kill(sig, File.read(old_pid).to_i)
|
82
|
+
# rescue Errno::ENOENT, Errno::ESRCH
|
83
|
+
# end
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# Throttle the master from forking too quickly by sleeping. Due
|
87
|
+
# to the implementation of standard Unix signal handlers, this
|
88
|
+
# helps (but does not completely) prevent identical, repeated signals
|
89
|
+
# from being lost when the receiving process is busy.
|
90
|
+
# sleep 1
|
91
|
+
end
|
92
|
+
|
93
|
+
after_fork do |server, worker|
|
94
|
+
# per-process listener ports for debugging/admin/migrations
|
95
|
+
# addr = "127.0.0.1:#{9293 + worker.nr}"
|
96
|
+
# server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
|
97
|
+
|
98
|
+
# the following is *required* for Rails + "preload_app true",
|
99
|
+
defined?(ActiveRecord::Base) and
|
100
|
+
ActiveRecord::Base.establish_connection
|
101
|
+
|
102
|
+
# if preload_app is true, then you may also want to check and
|
103
|
+
# restart any other shared sockets/descriptors such as Memcached,
|
104
|
+
# and Redis. TokyoCabinet file handles are safe to reuse
|
105
|
+
# between any number of forked children (assuming your kernel
|
106
|
+
# correctly implements pread()/pwrite() system calls)
|
107
|
+
|
108
|
+
|
109
|
+
# Kill off the new master after forking
|
110
|
+
old_pid = "#{app_dir}/shared/pids/unicorn.pid.oldbin"
|
111
|
+
if File.exists?(old_pid) && server.pid != old_pid
|
112
|
+
begin
|
113
|
+
Process.kill("QUIT", File.read(old_pid).to_i)
|
114
|
+
rescue Errno::ENOENT, Errno::ESRCH
|
115
|
+
# someone else did our job for us
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -10,6 +10,7 @@ module SimpleCapistranoUnicorn
|
|
10
10
|
_cset(:unicorn_old_pid) { "#{shared_path}/pids/unicorn.pid.oldbin" }
|
11
11
|
_cset(:unicorn_config) { "#{current_path}/config/unicorn.rb" }
|
12
12
|
_cset(:unicorn_log) { "#{shared_path}/log/unicorn.stderr.log" }
|
13
|
+
_cset(:unicorn_suicide) { false }
|
13
14
|
_cset(:use_bundler) { true }
|
14
15
|
_cset(:rails_env) { "production" }
|
15
16
|
_cset(:unicorn_command) { "unicorn" }
|
@@ -97,7 +98,8 @@ module SimpleCapistranoUnicorn
|
|
97
98
|
logger.info nice_output("Started Unicorn!", server)
|
98
99
|
end
|
99
100
|
end
|
100
|
-
unicorn
|
101
|
+
# Only clean-up if unicorn don't kill its old master
|
102
|
+
unicorn.cleanup unless unicorn_suicide
|
101
103
|
end
|
102
104
|
|
103
105
|
desc "Restart of Unicorn with downtime"
|
@@ -132,6 +134,7 @@ module SimpleCapistranoUnicorn
|
|
132
134
|
logger.info "unicorn_pid:\t#{fetch(:unicorn_pid)}"
|
133
135
|
logger.info "unicorn_old_pid:\t#{fetch(:unicorn_old_pid)}"
|
134
136
|
logger.info "unicorn_config:\t#{fetch(:unicorn_config)}"
|
137
|
+
logger.info "unicorn_suicide:\t#{fetch(:unicorn_suicide)}"
|
135
138
|
logger.info "unicorn_log:\t#{fetch(:unicorn_log)}"
|
136
139
|
logger.info "use_bundler:\t#{fetch(:use_bundler)}"
|
137
140
|
logger.info "rails_env: \t#{fetch(:rails_env)}"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-capistrano-unicorn
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -72,6 +72,8 @@ files:
|
|
72
72
|
- Gemfile
|
73
73
|
- README.md
|
74
74
|
- Rakefile
|
75
|
+
- configs/deploy.rb
|
76
|
+
- configs/unicorn.conf.rb
|
75
77
|
- lib/simple-capistrano-unicorn.rb
|
76
78
|
- lib/simple-capistrano-unicorn/namespace.rb
|
77
79
|
- lib/simple-capistrano-unicorn/version.rb
|