1
0
Fork 0
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:
Evan Phoenix 2013-09-13 09:56:39 -07:00
parent 1d01e5ef79
commit b24920d3ed
3 changed files with 59 additions and 0 deletions

View file

@ -23,10 +23,32 @@ module Puma
@debug = ENV.key? 'PUMA_DEBUG'
@on_booted = []
@hooks = Hash.new { |h,k| h[k] = [] }
end
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+
#
def log(str)

View file

@ -156,6 +156,8 @@ module Puma
client.close unless env['detach']
end
@events.fire :state, :running
if background
@thread = Thread.new { handle_servers_lopez_mode }
return @thread
@ -194,6 +196,8 @@ module Puma
end
end
@events.fire :state, @status
graceful_shutdown if @status == :stop || @status == :restart
rescue Exception => e
@ -207,6 +211,8 @@ module Puma
@binder.close
end
end
@events.fire :state, :done
end
# Runs the server.
#
@ -217,6 +223,8 @@ module Puma
def run(background=true)
BasicSocket.do_not_reverse_lookup = true
@events.fire :state, :booting
@status = :run
if @mode == :tcp
@ -255,6 +263,8 @@ module Puma
@thread_pool.auto_trim!(@auto_trim_time)
end
@events.fire :state, :running
if background
@thread = Thread.new { handle_servers }
return @thread
@ -293,6 +303,8 @@ module Puma
end
end
@events.fire :state, @status
graceful_shutdown if @status == :stop || @status == :restart
@reactor.clear! if @status == :restart
@ -308,6 +320,8 @@ module Puma
@binder.close
end
end
@events.fire :state, :done
end
# :nodoc:

View file

@ -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
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