Bráulio Bhavamitra <[email protected]> wrote:
> I think the hook is needed because I took too much time to figure out
> the problem and much more time to figure out the solution (this
> master_run variable). Also, I don't think this master_run solution is
> elegant.
A guard variable is fairly common practice for initialization.
It's not always nice, but I do not consider the existing hooks
to be elegant, either; they're only unfortunately necessary.
I consider having redundant features to be even worse.
How about the following documentation change instead?
--- a/examples/unicorn.conf.rb
+++ b/examples/unicorn.conf.rb
@@ -54,12 +54,23 @@ GC.respond_to?(:copy_on_write_friendly=) and
# fast LAN.
check_client_connection false
+# local variable to guard against running a hook multiple times
+run_once = true
+
before_fork do |server, worker|
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
+ # Occasionally, it may be necessary to run non-idempotent code in the
+ # master before forking. Keep in mind the above disconnect! example
+ # is idempotent and does not need a guard.
+ if run_once
+ # do_something_once_here ...
+ run_once = false # prevent from firing again
+ end
+
# The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house
# twice as many worker_processes as you have configured.