-
Notifications
You must be signed in to change notification settings - Fork 24
/
unicorn.conf.erb
87 lines (68 loc) · 3.2 KB
/
unicorn.conf.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
worker_processes <%= @deploy[:worker_processes] || node[:unicorn][:worker_processes] %>
user "<%= @deploy[:user]%>"
working_directory "<%= @deploy[:deploy_to]%>/current"
listen "<%= @deploy[:deploy_to]%>/shared/sockets/unicorn.sock", :backlog => <%= node[:unicorn][:backlog] %>, :tcp_nodelay => <%= node[:unicorn][:tcp_nodelay] %>, :tcp_nopush => <%= node[:unicorn][:tcp_nopush] %>, :tries => <%= node[:unicorn][:tries] %>, :delay => <%= node[:unicorn][:delay] %>, :accept_filter => <%= node[:unicorn][:accept_filter].inspect %>
timeout <%= node[:unicorn][:timeout] %>
pid "<%= @deploy[:deploy_to]%>/shared/pids/unicorn.pid"
stderr_path "<%= @deploy[:deploy_to]%>/shared/log/unicorn.stderr.log"
stdout_path "<%= @deploy[:deploy_to]%>/shared/log/unicorn.stdout.log"
# combine REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app <%= node[:unicorn][:preload_app] %>
GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)
# ensure Unicorn doesn't use a stale Gemfile when restarting
# more info: https://willj.net/2011/08/02/fixing-the-gemfile-not-found-bundlergemfilenotfound-error/
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "<%= @deploy[:deploy_to]%>/current/Gemfile"
# Reload ENV variables.
if defined?(Dotenv) && Dotenv.respond_to?(:overload)
Dotenv.overload ".env.<%= @deploy[:rails_env] %>", ".env"
end
end
# before_fork may be run multiple times, we only want to perform
# warming/preloading the first time, before the activerecord connection is
# disconnected.
initialized = false
before_fork do |server, worker|
# Based on samsaffron's optimizations of discourse's startup
# https://github.com/discourse/discourse/blob/master/config/unicorn.conf.rb
unless initialized
initialized = true
# Preload locales
I18n.t('foobar')
# Warm models, load schema
(ActiveRecord::Base.connection.tables - %w[schema_migrations]).each do |table|
table.classify.constantize.first rescue nil
end
# Warm the router
Rails.application.routes.recognize_path('abc') rescue nil
# Run GC
GC.start
end
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
end
# Before forking, kill the master process that belongs to the .oldbin PID.
# This enables 0 downtime deploys.
old_pid = "<%= @deploy[:deploy_to]%>/shared/pids/unicorn.pid.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
# the following is *required* for Rails + "preload_app true",
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
# if preload_app is true, then you may also want to check and
# restart any other shared sockets/descriptors such as Memcached,
# and Redis. TokyoCabinet file handles are safe to reuse
# between any number of forked children (assuming your kernel
# correctly implements pread()/pwrite() system calls)
end