capistrano-simple-unicorn 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f64084737f469cb0dcf81ff40b7c3c1130a0a450
4
- data.tar.gz: af00481fadf86312dcb2d9c5b5fda7048151f96a
3
+ metadata.gz: dc900d264d6068259766d0fd89b6a73eccd7fe8b
4
+ data.tar.gz: 4815ed5cbe0251e1f4d06cbea417cd914223e9bf
5
5
  SHA512:
6
- metadata.gz: ad68e3d32476967894822e35cd88b2dff20f75286635594d97d6cc3c65a07737091145160a5caa6561c97afad716cb1a1d477ddc6fd7986ac769d52148e00e6a
7
- data.tar.gz: 6ad7e35e6e67762392178e557743c7e458ad18944b36c0c370b354ef47e1fa63569e3ce0fc3c53a749c22a7d7c3610b89828d27e30ba7a9fd5417c05a41cc86a
6
+ metadata.gz: ab7943a6de75ce9c6e43c646029f48716ff3156176a5f1c1fbdea7de0414469f4d190abd3c405de8bc7a1724d5baaaa2028733ed115f68eb9b89884794cd3f62
7
+ data.tar.gz: 44f112be6584c1dd2f7d793d9c73cbf33d904fba841d2c638007903ee1a9fa7cdd9a14bbbf2743dbd8a7494fdf3fa5df2000b70cd9744defb04fbf094b84946b
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Capistrano::SimpleNginx
1
+ # Capistrano::SimpleUnicorn
2
2
  Support for Capistrano 3.x
3
3
 
4
4
  Capistrano task for automatic and unicorn configuration
@@ -40,9 +40,9 @@ Default value:
40
40
  ```
41
41
  set :unicorn_service, -> { "unicorn_#{fetch(:application)}" }
42
42
  set :user_home_path, -> { "/home/#{fetch(:user)}" }
43
- set :unicorn_config_file, -> { shared_path.join("config", "unicorn.rb") }
44
- set :unicorn_pid_file, -> { shared_path.join("tmp", "pids", "unicorn.pid") }
45
- set :unicorn_sock_file, -> { shared_path.join("tmp", "unicorn.sock") }
43
+ set :unicorn_config_path, -> { shared_path.join("config", "unicorn.rb") }
44
+ set :unicorn_pid_path, -> { shared_path.join("tmp", "pids", "unicorn.pid") }
45
+ set :unicorn_sock_path, -> { shared_path.join("tmp", "unicorn.sock") }
46
46
  set :unicorn_log_file, -> { shaed_path.join("log", "unicorn.stdout.log") }
47
47
  set :unicorn_error_log_file, -> {shared_path.join("log","unicorn.stderr.log")}
48
48
  set :ruby_version, -> { fetch(:rvm_ruby_version) || fetch(:rbenv_ruby) }
@@ -96,5 +96,5 @@ set :nginx_upstream_file, "/tmp/unicorn.sock"
96
96
  set :nginx_fail_timeout, 0
97
97
  set :nginx_access_log_file, -> { "/var/log/nginx/#{fetch(:nginx_config_name)}.access.log" } # access log file
98
98
  set :nginx_error_log_file, -> { "/var/log/nginx/#{fetch(:nginx_config_name)}.error.log" } # error log file
99
- set :nginx_upstream_file, -> { "#{fetch(:unicorn_sock_file)}" } # .sock file path
99
+ set :nginx_upstream_file, -> { "#{fetch(:unicorn_sock_path)}" } # .sock file path
100
100
  ```
@@ -44,15 +44,15 @@ module Capistrano
44
44
  "/etc/init.d/#{fetch(:unicorn_service)}"
45
45
  end
46
46
 
47
- def unicorn_sock_file
47
+ def unicorn_sock_path
48
48
  shared_path.join("tmp", "unicorn.sock")
49
49
  end
50
50
 
51
- def unicorn_config_file
51
+ def unicorn_config_path
52
52
  shared_path.join("config", "unicorn.rb")
53
53
  end
54
54
 
55
- def unicorn_pid_file
55
+ def unicorn_pid_path
56
56
  shared_path.join("tmp", "pids", "unicorn.pid")
57
57
  end
58
58
 
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module SimpleUnicorn
3
- VERSION = '1.0.0'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
5
5
  end
@@ -10,7 +10,7 @@ namespace :load do
10
10
  set :nginx_fail_timeout, 0
11
11
  set :nginx_access_log_file, -> { "/var/log/nginx/#{fetch(:nginx_config_name)}.access.log" }
12
12
  set :nginx_error_log_file, -> { "/var/log/nginx/#{fetch(:nginx_config_name)}.error.log" }
13
- set :nginx_upstream_file, -> { "#{fetch(:unicorn_sock_file)}" }
13
+ set :nginx_upstream_file, -> { "#{fetch(:unicorn_sock_path)}" }
14
14
  end
15
15
  end
16
16
 
@@ -5,11 +5,15 @@ namespace :load do
5
5
  task :defaults do
6
6
  set :unicorn_service, -> { "unicorn_#{fetch(:application)}" }
7
7
  set :user_home_path, -> { "/home/#{fetch(:user)}" }
8
- set :unicorn_config_file, -> { unicorn_config_file }
9
- set :unicorn_pid_file, -> { unicorn_pid_file }
10
- set :unicorn_sock_file, -> { unicorn_sock_file }
11
- set :unicorn_log_file, -> { unicorn_log_file }
12
- set :unicorn_error_log_file, -> { unicorn_error_log_file }
8
+ set :unicorn_config_path, -> { unicorn_config_path }
9
+ set :unicorn_pid_path, -> { unicorn_pid_path }
10
+ set :unicorn_sock_path, -> { unicorn_sock_path }
11
+ set :unicorn_stdout_path, -> { unicorn_log_file }
12
+ set :unicorn_stderr_path, -> { unicorn_error_log_file }
13
+ set :unicorn_roles, -> { :app }
14
+ set :unicorn_restart_sleep_time, 3
15
+ set :unicorn_options, -> { '' }
16
+ set :unicorn_env, -> { fetch(:rails_env) || 'deployment' }
13
17
  set :ruby_version, -> { fetch(:rvm_ruby_version) || fetch(:rbenv_ruby) }
14
18
  set :unicorn_worker_processes, 2
15
19
  set :unicorn_timeout, 30
@@ -19,52 +23,70 @@ namespace :load do
19
23
  end
20
24
 
21
25
  namespace :unicorn do
22
- desc 'Unicorn Initializer'
23
- task :unicorn_init do
24
- on roles :app do
25
- sudo_upload! template('unicorn_init.erb'), unicorn_initd_file
26
- execute :chmod, '+x', unicorn_initd_file
27
- if os_is_ubuntu?
28
- sudo 'update-rc.d', '-f', fetch(:unicorn_service), 'defaults'
29
- else
30
- sudo 'chkconfig', '--add', fetch(:unicorn_service)
26
+ desc 'Start Unicorn' do
27
+ task :start do
28
+ on roles(fetch(:unicorn_roles)) do
29
+ within current_path do
30
+ if test("[ -e #{fetch(:unicorn_pid)} ] && kill -0 #{pid}")
31
+ info 'unicorn is running...'
32
+ else
33
+ with rails_env: fetch(:rails_env) do
34
+ execute :bundle, 'exec unicorn', '-c', fetch(:unicorn_config_path), '-E', fetch(:unicorn_env), '-D', fetch(:unicorn_options)
35
+ end
36
+ end
37
+ end
31
38
  end
32
39
  end
33
40
  end
34
41
 
35
- desc 'Setup unicorn config'
36
- task :setup_unicorn_config do
37
- on roles :app do
38
- unless file_exists? fetch(:unicorn_config_file)
39
- execute :mkdir, '-pv', File.dirname(fetch(:unicorn_config_file))
42
+ desc 'Stop Unicorn (QUIT)'
43
+ task :stop do
44
+ on roles(fetch(:unicorn_roles)) do
45
+ within current_path do
46
+ if test("[ -e #{fetch(:unicorn_pid_path)} ]")
47
+ if test("kill -0 #{pid}")
48
+ info 'stopping unicorn...'
49
+ execute :kill, '-s QUIT', pid
50
+ else
51
+ info 'cleaning up dead unicorn pid...'
52
+ execute :rm, fetch(:unicorn_pid_path)
53
+ end
54
+ else
55
+ info 'unicorn is not running...'
56
+ end
40
57
  end
41
- upload! template('unicorn.rb.erb'), fetch(:unicorn_config_file)
42
58
  end
43
59
  end
44
60
 
45
- %w[start stop restart upgrade].each do |action|
46
- desc "#{action} unicorn"
47
- task action do
48
- on roles :app do
49
- sudo :service, fetch(:unicorn_service), action
61
+ desc 'Restart Unicorn (USR2); use this when preload_app: true'
62
+ task :restart do
63
+ invoke 'unicorn:start'
64
+ on roles(fetch(:unicorn_roles)) do
65
+ within current_path do
66
+ info 'unicorn restarting...'
67
+ execute :kill, '-s USR2', pid
50
68
  end
51
69
  end
52
70
  end
53
71
 
54
- desc 'restart unicorn'
55
- task 'reload' do
56
- on roles :app do
57
- invoke "unicorn:unicorn_init" unless file_exists?(unicorn_initd_file)
58
- invoke 'unicorn:setup_unicorn_config'
59
- if test "[ -f #{fetch(:unicorn_pid)} ]"
60
- sudo 'service', fetch(:unicorn_service), 'restart'
61
- else
62
- sudo 'service', fetch(:unicorn_service), 'start'
72
+ desc 'Legacy Restart (USR2 + QUIT); use this when preload_app: true and oldbin pid needs cleanup'
73
+ task :legacy_restart do
74
+ invoke 'unicorn:restart'
75
+ on roles(fetch(:unicorn_roles)) do
76
+ within current_path do
77
+ execute :sleep, fetch(:unicorn_restart_sleep_time)
78
+ if test("[ -e #{fetch(:unicorn_pid_path)}.oldbin ]")
79
+ execute :kill, '-s QUIT', pid_oldbin
80
+ end
63
81
  end
64
82
  end
65
83
  end
66
84
  end
67
85
 
68
- namespace :deploy do
69
- after :publishing, 'unicorn:reload'
86
+ def pid
87
+ "`cat #{fetch(:unicorn_pid_path)}`"
88
+ end
89
+
90
+ def pid_oldbin
91
+ "`cat #{fetch(:unicorn_pid_path)}.oldbin`"
70
92
  end
@@ -1,26 +1,36 @@
1
1
  worker_processes <%= fetch(:unicorn_worker_processes) %>
2
2
  timeout <%= fetch(:unicorn_timeout) %>
3
+ app_path = "<%= fetch(:deploy_to) %>"
4
+ listen "<%= fetch(:unicorn_sock_path) %>"
5
+ pid "<%= fetch(:unicorn_pid_path) %>"
3
6
 
4
- listen "<%= fetch(:unicorn_sock_file) %>"
5
- pid "<%= fetch(:unicorn_pid_file) %>"
7
+ stderr_path "<%= fetch(:unicorn_stderr_path) %>"
8
+ stdout_path "<%= fetch(:unicorn_stdout_path %>"
6
9
 
7
- stderr_path "<%= fetch(:unicorn_error_log_file) %>"
8
- stdout_path "<%= fetch(:unicorn_log_file) %>"
10
+ preload_app true
11
+
12
+ # use correct Gemfile on restarts
13
+ before_exec do |server|
14
+ ENV['BUNDLE_GEMFILE'] = "#{app_path}/current/Gemfile"
15
+ end
9
16
 
10
17
  preload_app true
11
18
 
12
19
  before_fork do |server, worker|
13
- defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
20
+ # the following is highly recomended for Rails + "preload_app true"
21
+ # as there's no need for the master process to hold a connection
22
+ if defined?(ActiveRecord::Base)
23
+ ActiveRecord::Base.connection.disconnect!
24
+ end
14
25
 
15
- old_pid = "#{ server.config[:pid] }.oldbin"
16
- unless old_pid == server.pid
26
+ # Before forking, kill the master process that belongs to the .oldbin PID.
27
+ # This enables 0 downtime deploys.
28
+ old_pid = "#{server.config[:pid]}.oldbin"
29
+ if File.exists?(old_pid) && server.pid != old_pid
17
30
  begin
18
- Process.kill :QUIT, File.read(old_pid).to_i
31
+ Process.kill("QUIT", File.read(old_pid).to_i)
19
32
  rescue Errno::ENOENT, Errno::ESRCH
33
+ # someone else did our job for us
20
34
  end
21
35
  end
22
36
  end
23
-
24
- after_fork do |server, worker|
25
- defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
26
- end
@@ -11,11 +11,11 @@
11
11
  set -e
12
12
  HOME=<%= fetch(:user_home_path) %>
13
13
  APP_ROOT=<%= current_path %>
14
- PID=<%= fetch(:unicorn_pid_file) %>
14
+ PID=<%= fetch(:unicorn_pid_path) %>
15
15
  USER=<%= fetch(:user) %>
16
16
  ENV="<%= fetch(:stage) %>"
17
17
  RUBY_VERSION="<%= fetch(:ruby_version) %>"
18
- CONFIG_FILE="<%= fetch(:unicorn_config_file) %>"
18
+ CONFIG_FILE="<%= fetch(:unicorn_config_path) %>"
19
19
  CMD="cd $APP_ROOT && export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION=$RUBY_VERSION; $HOME/.rbenv/bin/rbenv exec bundle exec unicorn -c $CONFIG_FILE -E $ENV -D"
20
20
 
21
21
  set -u
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-simple-unicorn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - truongkma