server-starter 0.1.0 → 0.1.1

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: 6d6ac01d0b1513979bb1b7d44518ad974250ac2e
4
- data.tar.gz: 5b5cf5cc27f280472e5c3008127141b665cb56b9
3
+ metadata.gz: 2f0ef5edbfbb9ba17014f1db7a34876b5b06b687
4
+ data.tar.gz: 0e38e80a7d760ea3c774f7c677a7f259bf9c7133
5
5
  SHA512:
6
- metadata.gz: 2afe47513bde1c810d8c9d573cfb1f72c7a7cbbb62374afa4c378a73808d763161be574a5618e83a7d9628557608ffa3bb8e3bc49eb918725ca7af95d8b0520f
7
- data.tar.gz: 7d384eb0e55be2d485f0739b323297ee69e610392769197750019ecf56300662458b17d450b445bfbe65a22353e494f2983b51190a9906ab3fd75f1a1464b4b6
6
+ metadata.gz: d91568f35236855ce8d0a7a77cefb05ca73543d60af9d241886233a49e1479a13665aea0c7219bfb79ee078f2e207562ce755aad4fffa7eb0a0f8de2cbfcfafc
7
+ data.tar.gz: 2c8f16573b6cab8c5cc834881b7001c566d47236572b3a0c5c0de731d5d0390a927a62833ddb3ffc33b6d416f5fc2cf0145363f07ebcbfc49d4691adc2e16319
data/CHANGELOG.md CHANGED
@@ -1,4 +1,11 @@
1
+ ## 0.1.1 (2015/03/16)
2
+
3
+ Enhancements:
4
+
5
+ * Add Server::Starter::UnicornListener
6
+
1
7
  ## 0.1.0 (2015/03/16)
2
8
 
3
9
  First version
4
10
 
11
+
data/README.md CHANGED
@@ -32,20 +32,18 @@ bundle exec start_server.rb --status-file=/path/to/app/log/unicorn.stat \
32
32
  An example of unicorn.conf:
33
33
 
34
34
  ```ruby
35
- worker_processes 4
35
+ require 'server/starter/unicorn_listener'
36
+ listener = Server::Starter::UnicornListener
37
+
38
+ worker_processes 2
36
39
  preload_app true
37
40
 
38
41
  APP_ROOT = File.expand_path('../..', __FILE__)
39
42
  status_file = File.join(APP_ROOT, 'log/unicorn.stat')
40
-
41
- if ENV.key?('SERVER_STARTER_PORT')
42
- fds = ENV['SERVER_STARTER_PORT'].split(';').map { |x|
43
- path_or_port, fd = x.split('=', 2)
44
- fd
45
- }
46
- ENV['UNICORN_FD'] = fds.join(',')
47
- ENV.delete('SERVER_STARTER_PORT')
48
- else
43
+
44
+ fd = listener.listen
45
+ unless fd
46
+ # Fallback if not running under Server::Starter
49
47
  listen ENV['PORT'] || '10080'
50
48
  end
51
49
 
@@ -61,22 +59,15 @@ end
61
59
 
62
60
  after_fork do |server, worker|
63
61
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
64
-
65
- begin
66
- # This allows a new master process to incrementally
67
- # phase out the old master process with SIGTTOU to avoid a
68
- # thundering herd (especially in the "preload_app false" case)
69
- # when doing a transparent upgrade. The last worker spawned
70
- # will then kill off the old master process with a SIGQUIT.
71
- pids = File.readlines(status_file).map {|_| _.chomp.split(':') }.to_h
72
- old_gen = ENV['SERVER_STARTER_GENERATION'].to_i - 1
73
- if old_pid = pids[old_gen.to_s]
74
- sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
75
- Process.kill(sig, old_pid.to_i)
76
- end
77
- rescue Errno::ENOENT, Errno::ESRCH => e
78
- $stderr.puts "#{e.class} #{e.message}"
79
- end
62
+
63
+ # This is optional
64
+ #
65
+ # This allows a new master process to incrementally
66
+ # phase out the old master process with SIGTTOU to avoid a
67
+ # thundering herd (especially in the "preload_app false" case)
68
+ # when doing a transparent upgrade. The last worker spawned
69
+ # will then kill off the old master process with a SIGQUIT.
70
+ listener.slow_start(server, worker, status_file)
80
71
  end
81
72
  ```
82
73
 
@@ -1,17 +1,15 @@
1
+ require 'server/starter/unicorn_listener'
2
+ listener = Server::Starter::UnicornListener
3
+
1
4
  worker_processes 2
2
5
  preload_app true
3
6
 
4
7
  APP_ROOT = File.expand_path('../..', __FILE__)
5
8
  status_file = File.join(APP_ROOT, 'log/unicorn.stat')
6
-
7
- if ENV.key?('SERVER_STARTER_PORT')
8
- fds = ENV['SERVER_STARTER_PORT'].split(';').map { |x|
9
- path_or_port, fd = x.split('=', 2)
10
- fd
11
- }
12
- ENV['UNICORN_FD'] = fds.join(',')
13
- ENV.delete('SERVER_STARTER_PORT')
14
- else
9
+
10
+ fd = listener.listen
11
+ unless fd
12
+ # Fallback if not running under Server::Starter
15
13
  listen ENV['PORT'] || '10080'
16
14
  end
17
15
 
@@ -27,20 +25,13 @@ end
27
25
 
28
26
  after_fork do |server, worker|
29
27
  defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
30
-
31
- begin
32
- # This allows a new master process to incrementally
33
- # phase out the old master process with SIGTTOU to avoid a
34
- # thundering herd (especially in the "preload_app false" case)
35
- # when doing a transparent upgrade. The last worker spawned
36
- # will then kill off the old master process with a SIGQUIT.
37
- pids = File.readlines(status_file).map {|_| _.chomp.split(':') }.to_h
38
- old_gen = ENV['SERVER_STARTER_GENERATION'].to_i - 1
39
- if old_pid = pids[old_gen.to_s]
40
- sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
41
- Process.kill(sig, old_pid.to_i)
42
- end
43
- rescue Errno::ENOENT, Errno::ESRCH => e
44
- $stderr.puts "#{e.class} #{e.message}"
45
- end
28
+
29
+ # This is optional
30
+ #
31
+ # This allows a new master process to incrementally
32
+ # phase out the old master process with SIGTTOU to avoid a
33
+ # thundering herd (especially in the "preload_app false" case)
34
+ # when doing a transparent upgrade. The last worker spawned
35
+ # will then kill off the old master process with a SIGQUIT.
36
+ listener.slow_start(server, worker, status_file)
46
37
  end
@@ -0,0 +1,33 @@
1
+ class Server::Starter
2
+ class UnicornListener
3
+ # Map ENV['SERVER_STARTER_PORT'] to ENV['UNICORN_FD']
4
+ def self.listen
5
+ return nil unless ENV.key?('SERVER_STARTER_PORT')
6
+ fds = ENV['SERVER_STARTER_PORT'].split(';').map { |x|
7
+ path_or_port, fd = x.split('=', 2)
8
+ fd
9
+ }
10
+ ENV['UNICORN_FD'] = fds.join(',')
11
+ end
12
+
13
+ # This allows a new master process to incrementally
14
+ # phase out the old master process with SIGTTOU to avoid a
15
+ # thundering herd (especially in the "preload_app false" case)
16
+ # when doing a transparent upgrade. The last worker spawned
17
+ # will then kill off the old master process with a SIGQUIT.
18
+ #
19
+ # @param server [Unicorn::HttpServer]
20
+ # @param worker [Unicorn::Worker]
21
+ # @param status_file [String] path to Server::Starter status file (--status-file)
22
+ def self.slow_start(server, worker, status_file)
23
+ pids = File.readlines(status_file).map {|_| _.chomp.split(':') }.to_h
24
+ old_gen = ENV['SERVER_STARTER_GENERATION'].to_i - 1
25
+ if old_pid = pids[old_gen.to_s]
26
+ sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
27
+ Process.kill(sig, old_pid.to_i)
28
+ end
29
+ rescue Errno::ENOENT, Errno::ESRCH => e
30
+ $stderr.puts "#{e.class} #{e.message}"
31
+ end
32
+ end
33
+ end
@@ -1,5 +1,5 @@
1
1
  class Server
2
2
  class Starter
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: server-starter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
@@ -48,6 +48,7 @@ files:
48
48
  - example/start_server
49
49
  - lib/server/starter.rb
50
50
  - lib/server/starter/helper.rb
51
+ - lib/server/starter/unicorn_listener.rb
51
52
  - lib/server/starter/version.rb
52
53
  - server-starter.gemspec
53
54
  homepage: https://github.com/sonots/ruby-server-starter