puma 4.3.7 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +80 -8
  3. data/LICENSE +23 -20
  4. data/README.md +26 -13
  5. data/docs/architecture.md +3 -3
  6. data/docs/deployment.md +9 -3
  7. data/docs/fork_worker.md +31 -0
  8. data/docs/jungle/README.md +13 -0
  9. data/docs/signals.md +7 -6
  10. data/docs/systemd.md +1 -63
  11. data/ext/puma_http11/PumaHttp11Service.java +2 -4
  12. data/ext/puma_http11/extconf.rb +3 -2
  13. data/ext/puma_http11/mini_ssl.c +15 -2
  14. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  15. data/ext/puma_http11/org/jruby/puma/Http11.java +3 -3
  16. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +77 -18
  17. data/ext/puma_http11/puma_http11.c +6 -38
  18. data/lib/puma/app/status.rb +14 -1
  19. data/lib/puma/binder.rb +88 -68
  20. data/lib/puma/cli.rb +7 -15
  21. data/lib/puma/client.rb +38 -5
  22. data/lib/puma/cluster.rb +191 -74
  23. data/lib/puma/commonlogger.rb +2 -2
  24. data/lib/puma/configuration.rb +31 -42
  25. data/lib/puma/const.rb +3 -3
  26. data/lib/puma/control_cli.rb +29 -17
  27. data/lib/puma/detect.rb +17 -0
  28. data/lib/puma/dsl.rb +144 -70
  29. data/lib/puma/error_logger.rb +97 -0
  30. data/lib/puma/events.rb +35 -31
  31. data/lib/puma/io_buffer.rb +9 -2
  32. data/lib/puma/jruby_restart.rb +0 -58
  33. data/lib/puma/launcher.rb +49 -31
  34. data/lib/puma/minissl/context_builder.rb +0 -3
  35. data/lib/puma/minissl.rb +60 -18
  36. data/lib/puma/null_io.rb +1 -1
  37. data/lib/puma/plugin.rb +1 -10
  38. data/lib/puma/rack/builder.rb +0 -4
  39. data/lib/puma/reactor.rb +9 -4
  40. data/lib/puma/runner.rb +8 -36
  41. data/lib/puma/server.rb +110 -186
  42. data/lib/puma/single.rb +7 -64
  43. data/lib/puma/state_file.rb +6 -3
  44. data/lib/puma/thread_pool.rb +94 -49
  45. data/lib/puma.rb +17 -0
  46. data/lib/rack/handler/puma.rb +1 -3
  47. metadata +18 -20
  48. data/docs/tcp_mode.md +0 -96
  49. data/ext/puma_http11/io_buffer.c +0 -155
  50. data/ext/puma_http11/org/jruby/puma/IOBuffer.java +0 -72
  51. data/lib/puma/tcp_logger.rb +0 -41
  52. data/tools/jungle/README.md +0 -19
  53. data/tools/jungle/init.d/README.md +0 -61
  54. data/tools/jungle/init.d/puma +0 -421
  55. data/tools/jungle/init.d/run-puma +0 -18
  56. /data/{tools → docs}/jungle/rc.d/README.md +0 -0
  57. /data/{tools → docs}/jungle/rc.d/puma +0 -0
  58. /data/{tools → docs}/jungle/rc.d/puma.conf +0 -0
  59. /data/{tools → docs}/jungle/upstart/README.md +0 -0
  60. /data/{tools → docs}/jungle/upstart/puma-manager.conf +0 -0
  61. /data/{tools → docs}/jungle/upstart/puma.conf +0 -0
  62. /data/tools/{docker/Dockerfile → Dockerfile} +0 -0
data/lib/puma/server.rb CHANGED
@@ -9,10 +9,8 @@ require 'puma/null_io'
9
9
  require 'puma/reactor'
10
10
  require 'puma/client'
11
11
  require 'puma/binder'
12
- require 'puma/accept_nonblock'
13
12
  require 'puma/util'
14
-
15
- require 'puma/puma_http11'
13
+ require 'puma/io_buffer'
16
14
 
17
15
  require 'socket'
18
16
  require 'forwardable'
@@ -36,6 +34,7 @@ module Puma
36
34
 
37
35
  attr_reader :thread
38
36
  attr_reader :events
37
+ attr_reader :requests_count # @version 5.0.0
39
38
  attr_accessor :app
40
39
 
41
40
  attr_accessor :min_threads
@@ -57,8 +56,7 @@ module Puma
57
56
  @app = app
58
57
  @events = events
59
58
 
60
- @check, @notify = Puma::Util.pipe
61
-
59
+ @check, @notify = nil
62
60
  @status = :stop
63
61
 
64
62
  @min_threads = 0
@@ -85,24 +83,34 @@ module Puma
85
83
  @mode = :http
86
84
 
87
85
  @precheck_closing = true
86
+
87
+ @requests_count = 0
88
88
  end
89
89
 
90
90
  attr_accessor :binder, :leak_stack_on_error, :early_hints
91
91
 
92
- def_delegators :@binder, :add_tcp_listener, :add_ssl_listener, :add_unix_listener, :connected_port
92
+ def_delegators :@binder, :add_tcp_listener, :add_ssl_listener, :add_unix_listener, :connected_ports
93
93
 
94
94
  def inherit_binder(bind)
95
95
  @binder = bind
96
96
  end
97
97
 
98
- def tcp_mode!
99
- @mode = :tcp
98
+ class << self
99
+ # :nodoc:
100
+ # @version 5.0.0
101
+ def tcp_cork_supported?
102
+ RbConfig::CONFIG['host_os'] =~ /linux/ &&
103
+ Socket.const_defined?(:IPPROTO_TCP) &&
104
+ Socket.const_defined?(:TCP_CORK) &&
105
+ Socket.const_defined?(:TCP_INFO)
106
+ end
107
+ private :tcp_cork_supported?
100
108
  end
101
109
 
102
110
  # On Linux, use TCP_CORK to better control how the TCP stack
103
111
  # packetizes our stream. This improves both latency and throughput.
104
112
  #
105
- if RUBY_PLATFORM =~ /linux/
113
+ if tcp_cork_supported?
106
114
  UNPACK_TCP_STATE_FROM_TCP_INFO = "C".freeze
107
115
 
108
116
  # 6 == Socket::IPPROTO_TCP
@@ -110,7 +118,7 @@ module Puma
110
118
  # 1/0 == turn on/off
111
119
  def cork_socket(socket)
112
120
  begin
113
- socket.setsockopt(6, 3, 1) if socket.kind_of? TCPSocket
121
+ socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_CORK, 1) if socket.kind_of? TCPSocket
114
122
  rescue IOError, SystemCallError
115
123
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
116
124
  end
@@ -118,7 +126,7 @@ module Puma
118
126
 
119
127
  def uncork_socket(socket)
120
128
  begin
121
- socket.setsockopt(6, 3, 0) if socket.kind_of? TCPSocket
129
+ socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_CORK, 0) if socket.kind_of? TCPSocket
122
130
  rescue IOError, SystemCallError
123
131
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
124
132
  end
@@ -129,7 +137,7 @@ module Puma
129
137
  return false unless @precheck_closing
130
138
 
131
139
  begin
132
- tcp_info = socket.getsockopt(Socket::SOL_TCP, Socket::TCP_INFO)
140
+ tcp_info = socket.getsockopt(Socket::IPPROTO_TCP, Socket::TCP_INFO)
133
141
  rescue IOError, SystemCallError
134
142
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
135
143
  @precheck_closing = false
@@ -172,107 +180,6 @@ module Puma
172
180
  @thread_pool and @thread_pool.pool_capacity
173
181
  end
174
182
 
175
- # Lopez Mode == raw tcp apps
176
-
177
- def run_lopez_mode(background=true)
178
- @thread_pool = ThreadPool.new(@min_threads,
179
- @max_threads,
180
- Hash) do |client, tl|
181
-
182
- io = client.to_io
183
- addr = io.peeraddr.last
184
-
185
- if addr.empty?
186
- # Set unix socket addrs to localhost
187
- addr = "127.0.0.1:0"
188
- else
189
- addr = "#{addr}:#{io.peeraddr[1]}"
190
- end
191
-
192
- env = { 'thread' => tl, REMOTE_ADDR => addr }
193
-
194
- begin
195
- @app.call env, client.to_io
196
- rescue Object => e
197
- STDERR.puts "! Detected exception at toplevel: #{e.message} (#{e.class})"
198
- STDERR.puts e.backtrace
199
- end
200
-
201
- client.close unless env['detach']
202
- end
203
-
204
- @events.fire :state, :running
205
-
206
- if background
207
- @thread = Thread.new do
208
- Puma.set_thread_name "server"
209
- handle_servers_lopez_mode
210
- end
211
- return @thread
212
- else
213
- handle_servers_lopez_mode
214
- end
215
- end
216
-
217
- def handle_servers_lopez_mode
218
- begin
219
- check = @check
220
- sockets = [check] + @binder.ios
221
- pool = @thread_pool
222
-
223
- while @status == :run
224
- begin
225
- ios = IO.select sockets
226
- ios.first.each do |sock|
227
- if sock == check
228
- break if handle_check
229
- else
230
- begin
231
- if io = sock.accept_nonblock
232
- client = Client.new io, nil
233
- pool << client
234
- end
235
- rescue SystemCallError
236
- # nothing
237
- rescue Errno::ECONNABORTED
238
- # client closed the socket even before accept
239
- begin
240
- io.close
241
- rescue
242
- Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
243
- end
244
- end
245
- end
246
- end
247
- rescue Object => e
248
- @events.unknown_error self, e, "Listen loop"
249
- end
250
- end
251
-
252
- @events.fire :state, @status
253
-
254
- graceful_shutdown if @status == :stop || @status == :restart
255
-
256
- rescue Exception => e
257
- STDERR.puts "Exception handling servers: #{e.message} (#{e.class})"
258
- STDERR.puts e.backtrace
259
- ensure
260
- begin
261
- @check.close
262
- rescue
263
- Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
264
- end
265
-
266
- # Prevent can't modify frozen IOError (RuntimeError)
267
- begin
268
- @notify.close
269
- rescue IOError
270
- # no biggy
271
- end
272
- end
273
-
274
- @events.fire :state, :done
275
- end
276
183
  # Runs the server.
277
184
  #
278
185
  # If +background+ is true (the default) then a thread is spun
@@ -286,15 +193,9 @@ module Puma
286
193
 
287
194
  @status = :run
288
195
 
289
- if @mode == :tcp
290
- return run_lopez_mode(background)
291
- end
292
-
293
- queue_requests = @queue_requests
294
-
295
196
  @thread_pool = ThreadPool.new(@min_threads,
296
197
  @max_threads,
297
- IOBuffer) do |client, buffer|
198
+ ::Puma::IOBuffer) do |client, buffer|
298
199
 
299
200
  # Advertise this server into the thread
300
201
  Thread.current[ThreadLocalKey] = self
@@ -302,10 +203,10 @@ module Puma
302
203
  process_now = false
303
204
 
304
205
  begin
305
- if queue_requests
206
+ if @queue_requests
306
207
  process_now = client.eagerly_finish
307
208
  else
308
- client.finish
209
+ client.finish(@first_data_timeout)
309
210
  process_now = true
310
211
  end
311
212
  rescue MiniSSL::SSLError => e
@@ -315,14 +216,16 @@ module Puma
315
216
 
316
217
  client.close
317
218
 
318
- @events.ssl_error self, addr, cert, e
219
+ @events.ssl_error e, addr, cert
319
220
  rescue HttpParserError => e
320
221
  client.write_error(400)
321
222
  client.close
322
223
 
323
- @events.parse_error self, client.env, e
324
- rescue ConnectionError, EOFError
224
+ @events.parse_error e, client
225
+ rescue ConnectionError, EOFError => e
325
226
  client.close
227
+
228
+ @events.connection_error e, client
326
229
  else
327
230
  if process_now
328
231
  process_client client, buffer
@@ -331,11 +234,14 @@ module Puma
331
234
  @reactor.add client
332
235
  end
333
236
  end
237
+
238
+ process_now
334
239
  end
335
240
 
241
+ @thread_pool.out_of_band_hook = @options[:out_of_band]
336
242
  @thread_pool.clean_thread_locals = @options[:clean_thread_locals]
337
243
 
338
- if queue_requests
244
+ if @queue_requests
339
245
  @reactor = Reactor.new self, @thread_pool
340
246
  @reactor.run_in_thread
341
247
  end
@@ -362,6 +268,7 @@ module Puma
362
268
  end
363
269
 
364
270
  def handle_servers
271
+ @check, @notify = Puma::Util.pipe unless @notify
365
272
  begin
366
273
  check = @check
367
274
  sockets = [check] + @binder.ios
@@ -385,51 +292,49 @@ module Puma
385
292
  if sock == check
386
293
  break if handle_check
387
294
  else
388
- begin
389
- if io = sock.accept_nonblock
390
- client = Client.new io, @binder.env(sock)
391
- if remote_addr_value
392
- client.peerip = remote_addr_value
393
- elsif remote_addr_header
394
- client.remote_addr_header = remote_addr_header
395
- end
396
-
397
- pool << client
398
- busy_threads = pool.wait_until_not_full
399
- if busy_threads == 0
400
- @options[:out_of_band].each(&:call) if @options[:out_of_band]
401
- end
402
- end
403
- rescue SystemCallError
404
- # nothing
405
- rescue Errno::ECONNABORTED
406
- # client closed the socket even before accept
407
- begin
408
- io.close
409
- rescue
410
- Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
411
- end
295
+ pool.wait_until_not_full
296
+ pool.wait_for_less_busy_worker(
297
+ @options[:wait_for_less_busy_worker].to_f)
298
+
299
+ io = begin
300
+ sock.accept_nonblock
301
+ rescue IO::WaitReadable
302
+ next
412
303
  end
304
+ client = Client.new io, @binder.env(sock)
305
+ if remote_addr_value
306
+ client.peerip = remote_addr_value
307
+ elsif remote_addr_header
308
+ client.remote_addr_header = remote_addr_header
309
+ end
310
+ pool << client
413
311
  end
414
312
  end
415
313
  rescue Object => e
416
- @events.unknown_error self, e, "Listen loop"
314
+ @events.unknown_error e, nil, "Listen loop"
417
315
  end
418
316
  end
419
317
 
420
318
  @events.fire :state, @status
421
319
 
422
- graceful_shutdown if @status == :stop || @status == :restart
423
320
  if queue_requests
321
+ @queue_requests = false
424
322
  @reactor.clear!
425
323
  @reactor.shutdown
426
324
  end
325
+ graceful_shutdown if @status == :stop || @status == :restart
427
326
  rescue Exception => e
428
- STDERR.puts "Exception handling servers: #{e.message} (#{e.class})"
429
- STDERR.puts e.backtrace
327
+ @events.unknown_error e, nil, "Exception handling servers"
430
328
  ensure
431
- @check.close
329
+ begin
330
+ @check.close unless @check.closed?
331
+ rescue Errno::EBADF, RuntimeError
332
+ # RuntimeError is Ruby 2.2 issue, can't modify frozen IOError
333
+ # Errno::EBADF is infrequently raised
334
+ end
432
335
  @notify.close
336
+ @notify = nil
337
+ @check = nil
433
338
  end
434
339
 
435
340
  @events.fire :state, :done
@@ -476,7 +381,6 @@ module Puma
476
381
  close_socket = false
477
382
  return
478
383
  when true
479
- return unless @queue_requests
480
384
  buffer.reset
481
385
 
482
386
  ThreadPool.clean_thread_locals if clean_thread_locals
@@ -494,6 +398,7 @@ module Puma
494
398
  end
495
399
 
496
400
  unless client.reset(check_for_more_data)
401
+ return unless @queue_requests
497
402
  close_socket = false
498
403
  client.set_timeout @persistent_timeout
499
404
  @reactor.add client
@@ -516,7 +421,7 @@ module Puma
516
421
 
517
422
  close_socket = true
518
423
 
519
- @events.ssl_error self, addr, cert, e
424
+ @events.ssl_error e, addr, cert
520
425
 
521
426
  # The client doesn't know HTTP well
522
427
  rescue HttpParserError => e
@@ -524,7 +429,7 @@ module Puma
524
429
 
525
430
  client.write_error(400)
526
431
 
527
- @events.parse_error self, client.env, e
432
+ @events.parse_error e, client
528
433
 
529
434
  # Server error
530
435
  rescue StandardError => e
@@ -532,8 +437,7 @@ module Puma
532
437
 
533
438
  client.write_error(500)
534
439
 
535
- @events.unknown_error self, e, "Read"
536
-
440
+ @events.unknown_error e, nil, "Read"
537
441
  ensure
538
442
  buffer.reset
539
443
 
@@ -543,7 +447,7 @@ module Puma
543
447
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
544
448
  # Already closed
545
449
  rescue StandardError => e
546
- @events.unknown_error self, e, "Client"
450
+ @events.unknown_error e, nil, "Client"
547
451
  end
548
452
  end
549
453
  end
@@ -579,7 +483,7 @@ module Puma
579
483
 
580
484
  env[PATH_INFO] = env[REQUEST_PATH]
581
485
 
582
- # From http://www.ietf.org/rfc/rfc3875 :
486
+ # From https://www.ietf.org/rfc/rfc3875 :
583
487
  # "Script authors should be aware that the REMOTE_ADDR and
584
488
  # REMOTE_HOST meta-variables (see sections 4.1.8 and 4.1.9)
585
489
  # may not identify the ultimate source of the request.
@@ -626,6 +530,8 @@ module Puma
626
530
  #
627
531
  # Finally, it'll return +true+ on keep-alive connections.
628
532
  def handle_request(req, lines)
533
+ @requests_count +=1
534
+
629
535
  env = req.env
630
536
  client = req.io
631
537
 
@@ -666,7 +572,8 @@ module Puma
666
572
  end
667
573
 
668
574
  fast_write client, "\r\n".freeze
669
- rescue ConnectionError
575
+ rescue ConnectionError => e
576
+ @events.debug_error e
670
577
  # noop, if we lost the socket we just won't send the early hints
671
578
  end
672
579
  }
@@ -724,17 +631,14 @@ module Puma
724
631
  return :async
725
632
  end
726
633
  rescue ThreadPool::ForceShutdown => e
727
- @events.log "Detected force shutdown of a thread, returning 503"
728
- @events.unknown_error self, e, "Rack app"
729
-
730
- status = 503
731
- headers = {}
732
- res_body = ["Request was internally terminated early\n"]
634
+ @events.unknown_error e, req, "Rack app"
635
+ @events.log "Detected force shutdown of a thread"
733
636
 
637
+ status, headers, res_body = lowlevel_error(e, env, 503)
734
638
  rescue Exception => e
735
- @events.unknown_error self, e, "Rack app", env
639
+ @events.unknown_error e, req, "Rack app"
736
640
 
737
- status, headers, res_body = lowlevel_error(e, env)
641
+ status, headers, res_body = lowlevel_error(e, env, 500)
738
642
  end
739
643
 
740
644
  content_length = nil
@@ -749,10 +653,10 @@ module Puma
749
653
  line_ending = LINE_END
750
654
  colon = COLON
751
655
 
752
- http_11 = if env[HTTP_VERSION] == HTTP_11
656
+ http_11 = env[HTTP_VERSION] == HTTP_11
657
+ if http_11
753
658
  allow_chunked = true
754
659
  keep_alive = env.fetch(HTTP_CONNECTION, "").downcase != CLOSE
755
- include_keepalive_header = false
756
660
 
757
661
  # An optimization. The most common response is 200, so we can
758
662
  # reply with the proper 200 status without having to compute
@@ -766,11 +670,9 @@ module Puma
766
670
 
767
671
  no_body ||= status < 200 || STATUS_WITH_NO_ENTITY_BODY[status]
768
672
  end
769
- true
770
673
  else
771
674
  allow_chunked = false
772
675
  keep_alive = env.fetch(HTTP_CONNECTION, "").downcase == KEEP_ALIVE
773
- include_keepalive_header = keep_alive
774
676
 
775
677
  # Same optimization as above for HTTP/1.1
776
678
  #
@@ -782,9 +684,12 @@ module Puma
782
684
 
783
685
  no_body ||= status < 200 || STATUS_WITH_NO_ENTITY_BODY[status]
784
686
  end
785
- false
786
687
  end
787
688
 
689
+ # regardless of what the client wants, we always close the connection
690
+ # if running without request queueing
691
+ keep_alive &&= @queue_requests
692
+
788
693
  response_hijack = nil
789
694
 
790
695
  headers.each do |k, vs|
@@ -811,10 +716,15 @@ module Puma
811
716
  end
812
717
  end
813
718
 
814
- if include_keepalive_header
815
- lines << CONNECTION_KEEP_ALIVE
816
- elsif http_11 && !keep_alive
817
- lines << CONNECTION_CLOSE
719
+ # HTTP/1.1 & 1.0 assume different defaults:
720
+ # - HTTP 1.0 assumes the connection will be closed if not specified
721
+ # - HTTP 1.1 assumes the connection will be kept alive if not specified.
722
+ # Only set the header if we're doing something which is not the default
723
+ # for this protocol version
724
+ if http_11
725
+ lines << CONNECTION_CLOSE if !keep_alive
726
+ else
727
+ lines << CONNECTION_KEEP_ALIVE if keep_alive
818
728
  end
819
729
 
820
730
  if no_body
@@ -941,19 +851,21 @@ module Puma
941
851
 
942
852
  # A fallback rack response if +@app+ raises as exception.
943
853
  #
944
- def lowlevel_error(e, env)
854
+ def lowlevel_error(e, env, status=500)
945
855
  if handler = @options[:lowlevel_error_handler]
946
856
  if handler.arity == 1
947
857
  return handler.call(e)
948
- else
858
+ elsif handler.arity == 2
949
859
  return handler.call(e, env)
860
+ else
861
+ return handler.call(e, env, status)
950
862
  end
951
863
  end
952
864
 
953
865
  if @leak_stack_on_error
954
- [500, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{e.backtrace.join("\n")}"]]
866
+ [status, {}, ["Puma caught this error: #{e.message} (#{e.class})\n#{e.backtrace.join("\n")}"]]
955
867
  else
956
- [500, {}, ["An unhandled lowlevel error occurred. The application logs may have details.\n"]]
868
+ [status, {}, ["An unhandled lowlevel error occurred. The application logs may have details.\n"]]
957
869
  end
958
870
  end
959
871
 
@@ -1011,9 +923,10 @@ module Puma
1011
923
  end
1012
924
 
1013
925
  def notify_safely(message)
926
+ @check, @notify = Puma::Util.pipe unless @notify
1014
927
  begin
1015
928
  @notify << message
1016
- rescue IOError
929
+ rescue IOError, NoMethodError, Errno::EPIPE
1017
930
  # The server, in another thread, is shutting down
1018
931
  Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
1019
932
  rescue RuntimeError => e
@@ -1040,8 +953,9 @@ module Puma
1040
953
  @thread.join if @thread && sync
1041
954
  end
1042
955
 
1043
- def begin_restart
956
+ def begin_restart(sync=false)
1044
957
  notify_safely(RESTART_COMMAND)
958
+ @thread.join if @thread && sync
1045
959
  end
1046
960
 
1047
961
  def fast_write(io, str)
@@ -1079,5 +993,15 @@ module Puma
1079
993
  HTTP_INJECTION_REGEX =~ header_value.to_s
1080
994
  end
1081
995
  private :possible_header_injection?
996
+
997
+ # List of methods invoked by #stats.
998
+ # @version 5.0.0
999
+ STAT_METHODS = [:backlog, :running, :pool_capacity, :max_threads, :requests_count].freeze
1000
+
1001
+ # Returns a hash of stats about the running server for reporting purposes.
1002
+ # @version 5.0.0
1003
+ def stats
1004
+ STAT_METHODS.map {|name| [name, send(name) || 0]}.to_h
1005
+ end
1082
1006
  end
1083
1007
  end
data/lib/puma/single.rb CHANGED
@@ -14,11 +14,9 @@ module Puma
14
14
  # that this inherits from.
15
15
  class Single < Runner
16
16
  def stats
17
- b = @server.backlog || 0
18
- r = @server.running || 0
19
- t = @server.pool_capacity || 0
20
- m = @server.max_threads || 0
21
- %Q!{ "started_at": "#{@started_at.utc.iso8601}", "backlog": #{b}, "running": #{r}, "pool_capacity": #{t}, "max_threads": #{m} }!
17
+ {
18
+ started_at: @started_at.utc.iso8601
19
+ }.merge(@server.stats)
22
20
  end
23
21
 
24
22
  def restart
@@ -39,64 +37,10 @@ module Puma
39
37
  @server.stop(true) if @server
40
38
  end
41
39
 
42
- def jruby_daemon?
43
- daemon? and Puma.jruby?
44
- end
45
-
46
- def jruby_daemon_start
47
- require 'puma/jruby_restart'
48
- JRubyRestart.daemon_start(@restart_dir, @launcher.restart_args)
49
- end
50
-
51
40
  def run
52
- already_daemon = false
53
-
54
- if jruby_daemon?
55
- require 'puma/jruby_restart'
56
-
57
- if JRubyRestart.daemon?
58
- # load and bind before redirecting IO so errors show up on stdout/stderr
59
- load_and_bind
60
- redirect_io
61
- end
62
-
63
- already_daemon = JRubyRestart.daemon_init
64
- end
65
-
66
41
  output_header "single"
67
42
 
68
- if jruby_daemon?
69
- if already_daemon
70
- JRubyRestart.perm_daemonize
71
- else
72
- pid = nil
73
-
74
- Signal.trap "SIGUSR2" do
75
- log "* Started new process #{pid} as daemon..."
76
-
77
- # Must use exit! so we don't unwind and run the ensures
78
- # that will be run by the new child (such as deleting the
79
- # pidfile)
80
- exit!(true)
81
- end
82
-
83
- Signal.trap "SIGCHLD" do
84
- log "! Error starting new process as daemon, exiting"
85
- exit 1
86
- end
87
-
88
- jruby_daemon_start
89
- sleep
90
- end
91
- else
92
- if daemon?
93
- log "* Daemonizing..."
94
- Process.daemon(true)
95
- redirect_io
96
- end
97
-
98
- load_and_bind
99
- end
43
+ load_and_bind
100
44
 
101
45
  Plugins.fire_background
102
46
 
@@ -106,10 +50,9 @@ module Puma
106
50
 
107
51
  @server = server = start_server
108
52
 
109
- unless daemon?
110
- log "Use Ctrl-C to stop"
111
- redirect_io
112
- end
53
+
54
+ log "Use Ctrl-C to stop"
55
+ redirect_io
113
56
 
114
57
  @launcher.events.fire_on_booted!
115
58
 
@@ -8,15 +8,18 @@ module Puma
8
8
  @options = {}
9
9
  end
10
10
 
11
- def save(path)
12
- File.write path, YAML.dump(@options)
11
+ def save(path, permission = nil)
12
+ File.open(path, "w") do |file|
13
+ file.chmod(permission) if permission
14
+ file.write(YAML.dump(@options))
15
+ end
13
16
  end
14
17
 
15
18
  def load(path)
16
19
  @options = YAML.load File.read(path)
17
20
  end
18
21
 
19
- FIELDS = %w!control_url control_auth_token pid!
22
+ FIELDS = %w!control_url control_auth_token pid running_from!
20
23
 
21
24
  FIELDS.each do |f|
22
25
  define_method f do