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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07223bd2cd439dbd81d9e6c7682dbef99d13af05
4
- data.tar.gz: c75917973a88533513a353c7f548095c02071f1d
3
+ metadata.gz: d6c6c6c34675d90e10f42b3edd377a4c7201df27
4
+ data.tar.gz: 043e1bbcdaf37e1eb17772969a2d8a5c394d20c9
5
5
  SHA512:
6
- metadata.gz: 66ae9fced46e50425249002ed43216eded8ccc76c40174b20e70ccfe70b3c7f10d0fac5d470f8d653314d1ae11550695a73f10b6c14222d9bbe60f26cc6fc301
7
- data.tar.gz: 8ac25f925bcb4b6c01a3f6ac4827e693c1df510972f2ecc20910098f31e18e88e801589a5b5b454316a307c2ab68d3cd7cdf0e21d5d551c888ee58592e47934e
6
+ metadata.gz: 717e78358e2ba9d8d6a75f1848423a6ee7265712f900a1c7a05edc3baf88de9c24835200378f672cfab8ec7ddccbcb4eaeee430f39de852b726805e17fcb60fd
7
+ data.tar.gz: 8c8358d531f80106ddba1461b044c8f91ffade248f79bfc459989a21803583593cbe6a85f259163ab9668278149f19e9cb0e57707f1d8165def51bbeafc13c15
@@ -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
@@ -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 unicorn_running?
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
- Process.spawn(command, pgroup: true)
24
- wait_for { unicorn_running? }
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
- exit unless unicorn_running?
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
- puts "unicorn-wrangler #{Process.pid} received #{signal} (managing #{unicorn_pid})"
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
- original_pid = unicorn_pid
46
- Process.kill "USR2", unicorn_pid
47
- wait_for { unicorn_pid != original_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
- Process.kill "TERM", original_pid
64
+ signal "TERM", unicorn_pid
65
+ self.unicorn_pid = pidfile_contents
50
66
  end
51
67
 
52
- def unicorn_pid
68
+ def pidfile_contents
53
69
  File.exist?(pidfile) && File.read(pidfile).to_i
54
70
  end
55
71
 
56
- def unicorn_running?
57
- unicorn_pid && Process.getpgid(unicorn_pid)
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 unicorn_running?
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
- Process.kill :TERM, unicorn_pid if unicorn_running?
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
@@ -1,5 +1,5 @@
1
1
  module Unicorn
2
2
  class Wrangler
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unicorn-wrangler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Ward