This adds a hook that is called after the application has
been loaded by the worker process, directly before it starts
accepting requests. This hook is necessary if your application
needs to get gain access to resources during initialization,
and then drop privileges before serving requests.
This is especially useful in conjunction with chroot support
so the app can load all the normal ruby libraries it needs
to function, and then chroot before accepting requests.
If you are preloading the app, it's possible to drop privileges
or chroot in after_fork, but if you are not preloading the app,
there is not currently a properly place to handle this, hence
the reason for this feature.
---
lib/unicorn/configurator.rb | 17 ++++++++++++++++-
lib/unicorn/http_server.rb | 4 ++--
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb
index 5bad925..6df5a30 100644
--- a/lib/unicorn/configurator.rb
+++ b/lib/unicorn/configurator.rb
@@ -49,6 +49,9 @@ class Unicorn::Configurator
server.logger.error(m)
end
},
+ :after_worker_ready => lambda { |server, worker|
+ server.logger.info("worker=#{worker.nr} ready")
+ },
:pid => nil,
:preload_app => false,
:check_client_connection => false,
@@ -162,7 +165,7 @@ def after_fork(*args, &block)
# sets after_worker_exit hook to a given block. This block will be called
# by the master process after a worker exits:
#
- # after_fork do |server,worker,status|
+ # after_worker_exit do |server,worker,status|
# # status is a Process::Status instance for the exited worker process
# unless status.success?
# server.logger.error("worker process failure: #{status.inspect}")
@@ -172,6 +175,18 @@ def after_worker_exit(*args, &block)
set_hook(:after_worker_exit, block_given? ? block : args[0], 3)
end
+ # sets after_worker_ready hook to a given block. This block will be called
+ # by a worker process after it has been fully loaded, directly before it
+ # starts responding to requests:
+ #
+ # after_worker_ready do |server,worker|
+ # server.logger.info("worker #{worker.nr} ready, dropping privileges")
+ # worker.user('username', 'groupname')
+ # end
+ def after_worker_ready(*args, &block)
+ set_hook(:after_worker_ready, block_given? ? block : args[0])
+ end
+
# sets before_fork got be a given Proc object. This Proc
# object will be called by the master process before forking
# each worker.
diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
index c2086cb..ef897ad 100644
--- a/lib/unicorn/http_server.rb
+++ b/lib/unicorn/http_server.rb
@@ -15,7 +15,7 @@ class Unicorn::HttpServer
:before_fork, :after_fork, :before_exec,
:listener_opts, :preload_app,
:orig_app, :config, :ready_pipe, :user
- attr_writer :after_worker_exit
+ attr_writer :after_worker_exit, :after_worker_ready
attr_reader :pid, :logger
include Unicorn::SocketHelper
@@ -644,7 +644,7 @@ def worker_loop(worker)
trap(:USR1) { nr = -65536 }
ready = readers.dup
- @logger.info "worker=#{worker.nr} ready"
+ @after_worker_ready.call(self, worker)
begin
nr < 0 and reopen_worker_logs(worker.nr)
--
2.11.0
--
unsubscribe: [email protected]
archive: https://bogomips.org/unicorn-public/