mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Add hook's and wire up a state hook
This commit is contained in:
parent
1d01e5ef79
commit
b24920d3ed
3 changed files with 59 additions and 0 deletions
|
@ -23,10 +23,32 @@ module Puma
|
||||||
@debug = ENV.key? 'PUMA_DEBUG'
|
@debug = ENV.key? 'PUMA_DEBUG'
|
||||||
|
|
||||||
@on_booted = []
|
@on_booted = []
|
||||||
|
|
||||||
|
@hooks = Hash.new { |h,k| h[k] = [] }
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :stdout, :stderr
|
attr_reader :stdout, :stderr
|
||||||
|
|
||||||
|
# Fire callbacks for the named hook
|
||||||
|
#
|
||||||
|
def fire(hook, *args)
|
||||||
|
@hooks[hook].each { |t| t.call(*args) }
|
||||||
|
end
|
||||||
|
|
||||||
|
# Register a callbock for a given hook
|
||||||
|
#
|
||||||
|
def register(hook, obj=nil, &blk)
|
||||||
|
if obj and blk
|
||||||
|
raise "Specify either an object or a block, not both"
|
||||||
|
end
|
||||||
|
|
||||||
|
h = obj || blk
|
||||||
|
|
||||||
|
@hooks[hook] << h
|
||||||
|
|
||||||
|
h
|
||||||
|
end
|
||||||
|
|
||||||
# Write +str+ to +@stdout+
|
# Write +str+ to +@stdout+
|
||||||
#
|
#
|
||||||
def log(str)
|
def log(str)
|
||||||
|
|
|
@ -156,6 +156,8 @@ module Puma
|
||||||
client.close unless env['detach']
|
client.close unless env['detach']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@events.fire :state, :running
|
||||||
|
|
||||||
if background
|
if background
|
||||||
@thread = Thread.new { handle_servers_lopez_mode }
|
@thread = Thread.new { handle_servers_lopez_mode }
|
||||||
return @thread
|
return @thread
|
||||||
|
@ -194,6 +196,8 @@ module Puma
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@events.fire :state, @status
|
||||||
|
|
||||||
graceful_shutdown if @status == :stop || @status == :restart
|
graceful_shutdown if @status == :stop || @status == :restart
|
||||||
|
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
|
@ -207,6 +211,8 @@ module Puma
|
||||||
@binder.close
|
@binder.close
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@events.fire :state, :done
|
||||||
end
|
end
|
||||||
# Runs the server.
|
# Runs the server.
|
||||||
#
|
#
|
||||||
|
@ -217,6 +223,8 @@ module Puma
|
||||||
def run(background=true)
|
def run(background=true)
|
||||||
BasicSocket.do_not_reverse_lookup = true
|
BasicSocket.do_not_reverse_lookup = true
|
||||||
|
|
||||||
|
@events.fire :state, :booting
|
||||||
|
|
||||||
@status = :run
|
@status = :run
|
||||||
|
|
||||||
if @mode == :tcp
|
if @mode == :tcp
|
||||||
|
@ -255,6 +263,8 @@ module Puma
|
||||||
@thread_pool.auto_trim!(@auto_trim_time)
|
@thread_pool.auto_trim!(@auto_trim_time)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@events.fire :state, :running
|
||||||
|
|
||||||
if background
|
if background
|
||||||
@thread = Thread.new { handle_servers }
|
@thread = Thread.new { handle_servers }
|
||||||
return @thread
|
return @thread
|
||||||
|
@ -293,6 +303,8 @@ module Puma
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@events.fire :state, @status
|
||||||
|
|
||||||
graceful_shutdown if @status == :stop || @status == :restart
|
graceful_shutdown if @status == :stop || @status == :restart
|
||||||
@reactor.clear! if @status == :restart
|
@reactor.clear! if @status == :restart
|
||||||
|
|
||||||
|
@ -308,6 +320,8 @@ module Puma
|
||||||
@binder.close
|
@binder.close
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@events.fire :state, :done
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
|
|
|
@ -266,4 +266,27 @@ class TestPumaServer < Test::Unit::TestCase
|
||||||
|
|
||||||
assert_equal "HTTP/1.0 200 OK\r\nContent-Type: application/pdf\r\nContent-Length: 4242\r\n\r\n", data
|
assert_equal "HTTP/1.0 200 OK\r\nContent-Type: application/pdf\r\nContent-Length: 4242\r\n\r\n", data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_status_hook_fires_when_server_changes_states
|
||||||
|
|
||||||
|
states = []
|
||||||
|
|
||||||
|
@events.register(:state) { |s| states << s }
|
||||||
|
|
||||||
|
@server.app = proc { |env| [200, {}, [""]] }
|
||||||
|
|
||||||
|
@server.add_tcp_listener @host, @port
|
||||||
|
@server.run
|
||||||
|
|
||||||
|
sock = TCPSocket.new @host, @port
|
||||||
|
sock << "HEAD / HTTP/1.0\r\n\r\n"
|
||||||
|
|
||||||
|
sock.read
|
||||||
|
|
||||||
|
assert_equal [:booting, :running], states
|
||||||
|
|
||||||
|
@server.stop(true)
|
||||||
|
|
||||||
|
assert_equal [:booting, :running, :stop, :done], states
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue