unicorn-wrangler 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/unicorn-wrangler +4 -0
- data/lib/unicorn/wrangler.rb +45 -15
- data/lib/unicorn/wrangler/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6c6c6c34675d90e10f42b3edd377a4c7201df27
|
4
|
+
data.tar.gz: 043e1bbcdaf37e1eb17772969a2d8a5c394d20c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 717e78358e2ba9d8d6a75f1848423a6ee7265712f900a1c7a05edc3baf88de9c24835200378f672cfab8ec7ddccbcb4eaeee430f39de852b726805e17fcb60fd
|
7
|
+
data.tar.gz: 8c8358d531f80106ddba1461b044c8f91ffade248f79bfc459989a21803583593cbe6a85f259163ab9668278149f19e9cb0e57707f1d8165def51bbeafc13c15
|
data/bin/unicorn-wrangler
CHANGED
@@ -16,6 +16,10 @@ OptionParser.new do |opts|
|
|
16
16
|
opts.on("-g", "--grace-period PERIOD", "Time to wait before shutting down server") do |time|
|
17
17
|
options[:grace_period] = time.to_i
|
18
18
|
end
|
19
|
+
|
20
|
+
opts.on("-v", "--verbose", "Verbose output") do
|
21
|
+
options[:verbose] = true
|
22
|
+
end
|
19
23
|
end.parse!(ARGV[0...command_index])
|
20
24
|
|
21
25
|
launcher = Unicorn::Wrangler.new command, options
|
data/lib/unicorn/wrangler.rb
CHANGED
@@ -3,11 +3,13 @@ require "unicorn/wrangler/version"
|
|
3
3
|
module Unicorn
|
4
4
|
class Wrangler
|
5
5
|
attr_reader :command, :pidfile, :grace_period
|
6
|
+
attr_accessor :unicorn_pid
|
6
7
|
|
7
8
|
def initialize(command, options = {})
|
8
9
|
@command = command
|
9
10
|
@pidfile = File.expand_path(options[:pidfile] || 'unicorn.pid')
|
10
11
|
@grace_period = options[:grace_period] || 60
|
12
|
+
@verbose = options[:verbose]
|
11
13
|
end
|
12
14
|
|
13
15
|
def start
|
@@ -17,15 +19,27 @@ module Unicorn
|
|
17
19
|
exit
|
18
20
|
end
|
19
21
|
|
20
|
-
if
|
22
|
+
if old_unicorn = old_pidfile_contents && process_running?(old_unicorn)
|
23
|
+
signal :QUIT, old_unicorn
|
24
|
+
end
|
25
|
+
|
26
|
+
self.unicorn_pid = pidfile_contents
|
27
|
+
|
28
|
+
if unicorn_pid && process_running?(unicorn_pid)
|
29
|
+
debug "running unicorn found at #{unicorn_pid}"
|
21
30
|
restart_unicorn
|
22
31
|
else
|
23
|
-
|
24
|
-
|
32
|
+
debug "no running unicorn found, starting new instance"
|
33
|
+
self.unicorn_pid = Process.spawn(command, pgroup: true)
|
34
|
+
wait_for { process_running?(unicorn_pid) }
|
35
|
+
debug "new instance started with PID #{unicorn_pid}"
|
25
36
|
end
|
26
37
|
|
27
38
|
loop do
|
28
|
-
|
39
|
+
unless process_running?(unicorn_pid)
|
40
|
+
debug "unicorn(#{unicorn_pid}) no longer running, quitting."
|
41
|
+
exit
|
42
|
+
end
|
29
43
|
sleep 1
|
30
44
|
end
|
31
45
|
end
|
@@ -35,26 +49,38 @@ module Unicorn
|
|
35
49
|
def trap_signals(*signals, &block)
|
36
50
|
signals.map(&:to_s).each do |signal|
|
37
51
|
trap(signal) do
|
38
|
-
|
52
|
+
debug "received #{signal} (managing #{unicorn_pid})"
|
39
53
|
block.call signal
|
40
54
|
end
|
41
55
|
end
|
42
56
|
end
|
43
57
|
|
44
58
|
def restart_unicorn
|
45
|
-
|
46
|
-
|
47
|
-
wait_for { unicorn_pid !=
|
59
|
+
debug "restarting unicorn process at #{unicorn_pid}"
|
60
|
+
signal "USR2", unicorn_pid
|
61
|
+
wait_for { pidfile_contents && unicorn_pid != pidfile_contents }
|
62
|
+
debug "new master process started at #{pidfile_contents}"
|
48
63
|
sleep grace_period
|
49
|
-
|
64
|
+
signal "TERM", unicorn_pid
|
65
|
+
self.unicorn_pid = pidfile_contents
|
50
66
|
end
|
51
67
|
|
52
|
-
def
|
68
|
+
def pidfile_contents
|
53
69
|
File.exist?(pidfile) && File.read(pidfile).to_i
|
54
70
|
end
|
55
71
|
|
56
|
-
def
|
57
|
-
|
72
|
+
def old_pidfile_contents
|
73
|
+
File.exist?(pidfile + ".oldbin") && File.read(pidfile + ".oldbin").to_i
|
74
|
+
end
|
75
|
+
|
76
|
+
def signal(name, pid)
|
77
|
+
debug "signalling #{pid} with #{name}"
|
78
|
+
Process.kill name, pid
|
79
|
+
rescue Errno::ESRCH
|
80
|
+
end
|
81
|
+
|
82
|
+
def process_running?(pid)
|
83
|
+
pid && Process.getpgid(pid)
|
58
84
|
rescue Errno::ESRCH
|
59
85
|
false
|
60
86
|
end
|
@@ -66,14 +92,18 @@ module Unicorn
|
|
66
92
|
end
|
67
93
|
|
68
94
|
def kill_unicorn_after_delay
|
69
|
-
if
|
70
|
-
puts "Preparing to kill unicorn in #{grace_period} seconds"
|
95
|
+
if process_running?(unicorn_pid)
|
71
96
|
unless fork
|
97
|
+
debug "preparing to kill unicorn #{unicorn_pid} in #{grace_period} seconds"
|
72
98
|
Process.setsid
|
73
99
|
sleep grace_period
|
74
|
-
|
100
|
+
signal :TERM, unicorn_pid if process_running?(unicorn_pid)
|
75
101
|
end
|
76
102
|
end
|
77
103
|
end
|
104
|
+
|
105
|
+
def debug(message)
|
106
|
+
puts message if @verbose
|
107
|
+
end
|
78
108
|
end
|
79
109
|
end
|