server-starter 0.3.0 → 0.3.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: 1d53d56660d67da4edc311de419917bccb44f413
4
- data.tar.gz: 15df36c0c0bcd646dcaa1f370ebc8fbe1c38d0c8
3
+ metadata.gz: 9d1306d9f95c35e64ad0e04bd294bee7ce9299ad
4
+ data.tar.gz: cb63d773356e83d97c79fe1db9473b9bda6e9706
5
5
  SHA512:
6
- metadata.gz: d3111c0c60634c6f3f7ab2d9ad842f6d541ec76cdb2c1c93398c744afef2deb0ffd5bbeefebc1c46e1a33eb337a32a1081ba0e4444e6e03b58ea0dc9f82bceab
7
- data.tar.gz: f70b10675013db13f9972902c35e8441317f681fe8b9b8521bd43e5fe58c13899b40ee6f7a9c666913f82abf5d46bc0db86f055c1fe5a29b0964a78a92ac4698
6
+ metadata.gz: 0423e31d4a72dad4759d7889e050e7d41a1317e85cf7b553869d28262e3fda52f2bc22ed865ccde2ea334741ccd477aafef05e0c46df65a621353e4287cdd520
7
+ data.tar.gz: 23f27dba0752d06bcf0b10b08a87f998bf8f667cdb54eaf02e9ff3cb65c61937a7303339a4883822b140b084161f48282b71049e52449f7777fc3093ae6c835e
@@ -1,3 +1,9 @@
1
+ ## 0.3.1 (2016/08/26)
2
+
3
+ Enhancements:
4
+
5
+ * Support [resque_starter](https://github.com/sonots/resque_starter)
6
+
1
7
  ## 0.3.0 (2016/04/03)
2
8
 
3
9
  Fixes:
data/README.md CHANGED
@@ -24,8 +24,13 @@ Following is an example to run unicorn server under ```Server::Starter```.
24
24
  The command line example:
25
25
 
26
26
  ```
27
- bundle exec start_server.rb --port=10080 --signal-on-hup=CONT --dir=/path/to/app \
28
- --status-file=/path/to/app/log/start_server.stat --pid-file=/path/to/app/log/start_server.pid -- \
27
+ bundle exec start_server.rb \
28
+ --port=10080 \
29
+ --signal-on-hup=CONT \
30
+ --dir=/path/to/app \
31
+ --status-file=/path/to/app/log/start_server.stat \
32
+ --pid-file=/path/to/app/log/start_server.pid \
33
+ -- \
29
34
  bundle exec --keep-file-descriptors unicorn -c config/unicorn.conf.rb config.ru
30
35
  ```
31
36
 
@@ -71,6 +76,91 @@ after_fork do |server, worker|
71
76
  end
72
77
  ```
73
78
 
79
+ # Puma
80
+
81
+ Following is an example to run puma server under ```Server::Starter```.
82
+
83
+ The command line example:
84
+
85
+ ```
86
+ bundle exec start_server.rb \
87
+ --port=0.0.0.0:10080 \
88
+ --dir=/path/to/app \
89
+ --interval=1 \
90
+ --signal-on-hup=TERM \
91
+ --signal-on-TERM=TERM \
92
+ --pid-file=/path/to/app/log/start_server.pid \
93
+ --status-file=/path/to/app/log/start_server.stat \
94
+ --envdir=env \
95
+ --enable-auto-restart \
96
+ --auto-restart-interval=100 \
97
+ --kill-old-delay=10 \
98
+ --backlog=100 \
99
+ -- \
100
+ bundle exec --keep-file-descriptors start_puma.rb puma -C config/puma.rb config.ru
101
+ ```
102
+
103
+ An example of config/puma.rb:
104
+
105
+ ```ruby
106
+
107
+ require 'server/starter/puma_listener'
108
+ listener = ::Server::Starter::PumaListener
109
+
110
+ APP_ROOT = File.expand_path('../..', __FILE__)
111
+ status_file = File.join(APP_ROOT, 'log/start_server.stat')
112
+
113
+ pidfile File.join(APP_ROOT, 'log/puma.pid')
114
+ state_path File.join(APP_ROOT, 'log/puma.state')
115
+
116
+ # prune_bundler # need to tweak lib/puma/launher to add { close_others: false } opts to Kernel.exec
117
+ preload_app!
118
+
119
+ # Configure "min" to be the minimum number of threads to use to answer
120
+ # requests and "max" the maximum.
121
+ # The default is "0, 16".
122
+ threads 0, 16
123
+
124
+ # How many worker processes to run. The default is "0".
125
+ workers 2
126
+
127
+ # Run puma via start_puma.rb to configure PUMA_INHERIT_\d ENV from SERVER_STARTER_PORT ENV as
128
+ # $ bundle exec --keep-file-descriptors start_puma.rb puma -C config/puma.conf.rb config.ru
129
+ if ENV['SERVER_STARTER_PORT']
130
+ puma_inherits = listener.listen
131
+ puma_inherits.each do |puma_inherit|
132
+ bind puma_inherit[:url]
133
+ end
134
+ else
135
+ puts '[WARN] Fallback to 0.0.0.0:10080 since not running under Server::Starter'
136
+ bind 'tcp://0.0.0.0:10080'
137
+ end
138
+
139
+ # Code to run before doing a restart. This code should
140
+ # close log files, database connections, etc.
141
+ # This can be called multiple times to add code each time.
142
+ on_restart do
143
+ puts 'On restart...'
144
+ end
145
+
146
+ # Code to run when a worker boots to setup the process before booting
147
+ # the app. This can be called multiple times to add hooks.
148
+ on_worker_boot do
149
+ defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
150
+ end
151
+
152
+ # Code to run when a worker boots to setup the process after booting
153
+ # the app. This can be called multiple times to add hooks.
154
+ after_worker_boot do
155
+ defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
156
+ end
157
+
158
+ # Code to run when a worker shutdown.
159
+ on_worker_shutdown do
160
+ puts 'On worker shutdown...'
161
+ end
162
+ ```
163
+
74
164
  ## See Also
75
165
 
76
166
  * [「Server::Starterに対応するとはどういうことか」の補足](http://blog.livedoor.jp/sonots/archives/40248661.html) (Japanese)
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'server-starter', path: '../../'
4
+ gem 'resque_starter', git: 'https://github.com/sonots/resque_starter'
5
+ gem 'pry'
6
+ gem 'pry-nav'
@@ -0,0 +1,30 @@
1
+ require 'server/starter/resque_listener'
2
+ listener = Server::Starter::ResqueListener
3
+ APP_ROOT = File.expand_path('../..', __FILE__)
4
+
5
+ concurrency 2
6
+ queues ['test']
7
+ pid_file File.join(APP_ROOT, 'log/start_resque.pid')
8
+ status_file File.join(APP_ROOT, 'log/start_resque.stat')
9
+
10
+ # preload rails app
11
+ # require File.join(APP_ROOT, 'config/environment')
12
+ # Rails.application.eager_load!
13
+
14
+ start_server_status_file = File.join(APP_ROOT, 'log/start_server.stat')
15
+
16
+ before_fork do |starter, worker, worker_nr|
17
+ defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
18
+ end
19
+
20
+ after_fork do |starter, worker, worker_nr|
21
+ defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
22
+
23
+ # Graceful restart
24
+ #
25
+ # This allows a new master process to incrementally
26
+ # phase out the old master process with SIGTTOU.
27
+ # The last worker spawned # will then kill off the old master
28
+ # process with a SIGQUIT.
29
+ listener.graceful_restart(starter, worker, worker_nr, start_server_status_file)
30
+ end
@@ -0,0 +1 @@
1
+ foo
File without changes
@@ -0,0 +1,7 @@
1
+ class MyJob
2
+ @queue = :test
3
+
4
+ def self.perform(params)
5
+ puts params
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+ # sends SIGHUP
3
+ bundle exec start_server.rb \
4
+ --restart \
5
+ --dir=$(pwd) \
6
+ --pid-file=$(pwd)/log/start_server.pid \
7
+ --status-file=$(pwd)/log/start_server.stat
@@ -0,0 +1,14 @@
1
+ #!/bin/sh
2
+ bundle exec start_server.rb \
3
+ --dir=$(pwd) \
4
+ --interval=1 \
5
+ --signal-on-hup=CONT \
6
+ --signal-on-TERM=TERM \
7
+ --pid-file=$(pwd)/log/start_server.pid \
8
+ --status-file=$(pwd)/log/start_server.stat \
9
+ --envdir=env \
10
+ --enable-auto-restart \
11
+ --auto-restart-interval=100 \
12
+ --kill-old-delay=1 \
13
+ -- \
14
+ bundle exec --keep-file-descriptors start_resque.rb -c config/resque.conf.rb 2>&1
@@ -13,4 +13,4 @@ bundle exec start_server.rb \
13
13
  --kill-old-delay=1 \
14
14
  --backlog=100 \
15
15
  -- \
16
- bundle exec --keep-file-descriptors unicorn -c config/unicorn.conf config.ru 2>&1
16
+ bundle exec --keep-file-descriptors unicorn -c config/unicorn.conf.rb config.ru 2>&1
@@ -0,0 +1,28 @@
1
+ require 'server/starter/version'
2
+
3
+ class Server::Starter
4
+ class ResqueListener
5
+ # This allows a new master process to incrementally
6
+ # phase out the old master process with SIGTTOU.
7
+ # The last worker spawned will then kill off the old master
8
+ # process with a SIGQUIT.
9
+ #
10
+ # @param starter [ResqueStarter]
11
+ # @param worker [Resque::Worker]
12
+ # @param worker_nr [Integer] worker number
13
+ # @param status_file [String] path to Server::Starter status file (--status-file)
14
+ def self.graceful_restart(starter, worker, worker_nr, status_file)
15
+ pids = File.readlines(status_file).map {|_| _.chomp.split(':') }.to_h
16
+ old_gen = ENV['SERVER_STARTER_GENERATION'].to_i - 1
17
+ if old_pid = pids[old_gen.to_s]
18
+ sig = (worker_nr + 1) >= starter.num_workers ? :QUIT : :TTOU
19
+ Process.kill(sig, old_pid.to_i)
20
+ while starter.old_workers.size > starter.num_workers
21
+ sleep 0.1
22
+ end
23
+ end
24
+ rescue Errno::ENOENT, Errno::ESRCH => e
25
+ $stderr.puts "#{e.class} #{e.message}"
26
+ end
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  class Server
2
2
  class Starter
3
- VERSION = '0.3.0'
3
+ VERSION = '0.3.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: server-starter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-03 00:00:00.000000000 Z
11
+ date: 2016-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -48,9 +48,16 @@ files:
48
48
  - example/puma/log/.gitkeep
49
49
  - example/puma/restart_server
50
50
  - example/puma/start_server
51
+ - example/resque/Gemfile
52
+ - example/resque/config/resque.conf.rb
53
+ - example/resque/env/TEST
54
+ - example/resque/log/.gitkeep
55
+ - example/resque/my_job.rb
56
+ - example/resque/restart_server
57
+ - example/resque/start_server
51
58
  - example/unicorn/Gemfile
52
59
  - example/unicorn/config.ru
53
- - example/unicorn/config/unicorn.conf
60
+ - example/unicorn/config/unicorn.conf.rb
54
61
  - example/unicorn/env/TEST
55
62
  - example/unicorn/log/.gitkeep
56
63
  - example/unicorn/restart_server
@@ -58,6 +65,7 @@ files:
58
65
  - lib/server/starter.rb
59
66
  - lib/server/starter/helper.rb
60
67
  - lib/server/starter/puma_listener.rb
68
+ - lib/server/starter/resque_listener.rb
61
69
  - lib/server/starter/unicorn_listener.rb
62
70
  - lib/server/starter/version.rb
63
71
  - server-starter.gemspec