1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Added on_worker_shutdown hook mechanism

This commit is contained in:
Daniel Shelton 2014-08-01 11:04:39 +01:00
parent 55b9ce01e5
commit e4ea4fb46d
3 changed files with 19 additions and 1 deletions

View file

@ -99,6 +99,7 @@ module Puma
:binds => [], :binds => [],
:workers => 0, :workers => 0,
:daemon => false, :daemon => false,
:before_worker_shutdown => [],
:before_worker_boot => [], :before_worker_boot => [],
:after_worker_boot => [] :after_worker_boot => []
} }
@ -228,7 +229,7 @@ module Puma
cfg = @config.dup cfg = @config.dup
[ :logger, :before_worker_boot, :after_worker_boot, :on_restart ].each { |o| cfg.options.delete o } [ :logger, :before_worker_shutdown, :before_worker_boot, :after_worker_boot, :on_restart ].each { |o| cfg.options.delete o }
state["config"] = cfg state["config"] = cfg

View file

@ -216,6 +216,11 @@ module Puma
server.run.join server.run.join
# Invoke any worker shutdown hooks so they can prevent the worker
# exiting until any background operations are completed
hooks = @options[:before_worker_shutdown]
hooks.each { |h| h.call(index) }
ensure ensure
@worker_write.close @worker_write.close
end end

View file

@ -21,6 +21,7 @@ module Puma
@options[:mode] ||= :http @options[:mode] ||= :http
@options[:binds] ||= [] @options[:binds] ||= []
@options[:on_restart] ||= [] @options[:on_restart] ||= []
@options[:before_worker_shutdown] ||= []
@options[:before_worker_boot] ||= [] @options[:before_worker_boot] ||= []
@options[:after_worker_boot] ||= [] @options[:after_worker_boot] ||= []
@options[:worker_timeout] ||= DefaultWorkerTimeout @options[:worker_timeout] ||= DefaultWorkerTimeout
@ -301,6 +302,17 @@ module Puma
@options[:workers] = count.to_i @options[:workers] = count.to_i
end end
# *Cluster mode only* Code to run immediately before a worker shuts
# down (after it has finished processing HTTP requests). These hooks
# can block if necessary to wait for background operations unknown
# to puma to finish before the process terminates.
#
# This can be called multiple times to add hooks.
#
def on_worker_shutdown(&block)
@options[:before_worker_shutdown] << block
end
# *Cluster mode only* Code to run when a worker boots to setup # *Cluster mode only* Code to run when a worker boots to setup
# the process before booting the app. # the process before booting the app.
# #