-
Notifications
You must be signed in to change notification settings - Fork 1
/
unicorn.rb.erb
67 lines (56 loc) · 1.84 KB
/
unicorn.rb.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
working_directory "<%= current_path %>"
pid "<%= unicorn_pid %>"
stderr_path "<%= unicorn_log %>"
stdout_path "<%= unicorn_log %>"
listen "/tmp/<%= application %>.sock"
worker_processes <%= unicorn_workers %>
timeout 90
preload_app true
before_exec do |server|
ENV["BUNDLE_GEMFILE"] = "<%= current_path %>/Gemfile"
end
before_fork do |server, worker|
# Disconnect since the database connection will not carry over
if defined? ActiveRecord::Base
ActiveRecord::Base.connection.disconnect!
end
# Quit the old unicorn process
old_pid = "#{server.config[: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
# The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house
# twice as many worker_processes as you have configured.
#
# # This allows a new master process to incrementally
# # phase out the old master process with SIGTTOU to avoid a
# # thundering herd (especially in the "preload_app false" case)
# # when doing a transparent upgrade. The last worker spawned
# # will then kill off the old master process with a SIGQUIT.
#old_pid = "#{server.config[:pid]}.oldbin"
#if old_pid != server.pid
#begin
##sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
#Process.kill(sig, File.read(old_pid).to_i)
#rescue Errno::ENOENT, Errno::ESRCH
#end
#end
if defined?(Resque)
Resque.redis.quit
end
sleep 1
end
after_fork do |server, worker|
# Start up the database connection again in the worker
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
if defined?(Resque)
Resque.redis.client.reconnect
end
end