server-starter 0.2.1 → 0.3.2

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
- SHA1:
3
- metadata.gz: 172f969ecca0954a1b3f5a44df94ad9e391893cf
4
- data.tar.gz: 414f7dc2679b28e65d4d8ebc289032540476d83f
2
+ SHA256:
3
+ metadata.gz: 5ee6c5c725642797b8cfe87cc099dd5182f2059b901b3ac494b41f80116dd297
4
+ data.tar.gz: 5bcdb4c9c064b5af13983c6fe174b2612f22d5bed975d412db19fe8ab6025e6a
5
5
  SHA512:
6
- metadata.gz: f5e0704849bf69e29e582cea3681bdf3f5578ead49cea74f87cb919ecbafafe975dd537f6e60dfdb42378dc124f98a14c0d8eb46bf76b32793d7b4628621dae8
7
- data.tar.gz: d2f77e9459789f62758b8f0a24bd090a295646e5e4b3d681e9cec992824234a661cf7f10737b8cad5810c7d1cf99de5ab3ee4dcaed56b09354912d91b59c3d82
6
+ metadata.gz: c875da7e9d71f1bb0cb8f25601248f046355690865ad5ce8336b34499602c5e330c50486b96f8b1f17d94be3008f8fc2903e6dfb7e4207ed8e2059a12e045600
7
+ data.tar.gz: 2d98c0cdb9aa6959e8a871032877f0ec3a41a525543c4fee416de38b687883a98789adbb6c7eed85843e4509ac77b7d7e6e459c70df20ebc66807dac123dc110
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## 0.3.2 (2022/05/09)
2
+
3
+ Enhancements:
4
+
5
+ * Support puma5 (thanks to limitusus)
6
+
7
+ ## 0.3.1 (2016/08/26)
8
+
9
+ Enhancements:
10
+
11
+ * Support [resque_starter](https://github.com/sonots/resque_starter)
12
+
13
+ ## 0.3.0 (2016/04/03)
14
+
15
+ Fixes:
16
+
17
+ * Fix a critial bug. It was not killing old workers on HUP :scream:
18
+
1
19
  ## 0.2.1 (2016/04/03)
2
20
 
3
21
  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,121 @@ 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
+
164
+ ## Options
165
+
166
+ To be written
167
+
168
+ ### --envdir [dir]
169
+
170
+ Configure environment variables from files on given directory, and reload on restarting.
171
+
172
+ The directory structure is inspired from [envdir](https://cr.yp.to/daemontools/envdir.html) of daemontools.
173
+ The filename is the environment variable name, and its content (first line) is the value of the environment variable.
174
+
175
+ Example)
176
+
177
+ ```
178
+ $ find env
179
+ env/RAILS_ENV
180
+ env/LANG
181
+ ```
182
+
183
+ ```
184
+ $ cat env/RAILS_ENV
185
+ production
186
+ $ cat env/LANG
187
+ en_US.UTF-8
188
+ ```
189
+
190
+ which are equivalent with `env RAILS_ENV=production LANG=en_US.UTF-8` in shell.
191
+
192
+ Please note that environment variables are updated on restarting, which means deleted files are not affected.
193
+
74
194
  ## See Also
75
195
 
76
196
  * [「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,34 @@
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
18
+ ActiveRecord::Base.connection.disconnect!
19
+
20
+ sleep 1
21
+ end
22
+
23
+ after_fork do |starter, worker, worker_nr|
24
+ defined?(ActiveRecord::Base) and
25
+ ActiveRecord::Base.establish_connection
26
+
27
+ # Graceful restart
28
+ #
29
+ # This allows a new master process to incrementally
30
+ # phase out the old master process with SIGTTOU.
31
+ # The last worker spawned # will then kill off the old master
32
+ # process with a SIGQUIT.
33
+ listener.graceful_restart(starter, worker, worker_nr, start_server_status_file)
34
+ 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
@@ -14,7 +14,8 @@ unless fd
14
14
  end
15
15
 
16
16
  before_fork do |server, worker|
17
- defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
17
+ defined?(ActiveRecord::Base) and
18
+ ActiveRecord::Base.connection.disconnect!
18
19
 
19
20
  # Throttle the master from forking too quickly by sleeping. Due
20
21
  # to the implementation of standard Unix signal handlers, this
@@ -24,7 +25,8 @@ before_fork do |server, worker|
24
25
  end
25
26
 
26
27
  after_fork do |server, worker|
27
- defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
28
+ defined?(ActiveRecord::Base) and
29
+ ActiveRecord::Base.establish_connection
28
30
 
29
31
  # This is optional
30
32
  #
@@ -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
@@ -1,4 +1,5 @@
1
1
  require 'server/starter/version'
2
+ require 'puma/const'
2
3
 
3
4
  class Server::Starter
4
5
  class PumaListener
@@ -11,7 +12,12 @@ class Server::Starter
11
12
  else
12
13
  url = "unix://#{path_or_port}"
13
14
  end
14
- ENV["PUMA_INHERIT_#{i}"] = "#{fd}:#{url}"
15
+ if Gem::Version.new(Puma::Const::PUMA_VERSION) < Gem::Version.new('5')
16
+ ENV["PUMA_INHERIT_#{i}"] = "#{fd}:#{url}"
17
+ else
18
+ ENV['LISTEN_FDS'] = '1'
19
+ ENV['LISTEN_PID'] = Process.pid.to_s
20
+ end
15
21
  { fd: fd, url: url }
16
22
  end
17
23
  end
@@ -0,0 +1,25 @@
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
+ end
21
+ rescue Errno::ENOENT, Errno::ESRCH => e
22
+ $stderr.puts "#{e.class} #{e.message}"
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  class Server
2
2
  class Starter
3
- VERSION = '0.2.1'
3
+ VERSION = '0.3.2'
4
4
  end
5
5
  end
@@ -313,7 +313,7 @@ class Server::Starter
313
313
  sleep kill_old_delay
314
314
  end
315
315
  $stderr.puts "killing old workers"
316
- @old_workers.keys.sort {|pid| Process.kill(opts[:signal_on_hup], pid) }
316
+ @old_workers.keys.sort.each {|pid| Process.kill(opts[:signal_on_hup], pid) }
317
317
  end
318
318
  end
319
319
  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.2.1
4
+ version: 0.3.2
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: 2022-05-09 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
@@ -80,8 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
88
  - !ruby/object:Gem::Version
81
89
  version: '0'
82
90
  requirements: []
83
- rubyforge_project:
84
- rubygems_version: 2.5.1
91
+ rubygems_version: 3.0.3
85
92
  signing_key:
86
93
  specification_version: 4
87
94
  summary: a superdaemon for hot-deploying server programs (ruby port of p5-Server-Starter)