Detecting unicorn / defining after_fork after master startup

Matt Sanders matt at modal.org
Sat Jul 21 19:42:57 UTC 2012


Eric,

Thanks! I definitely agree in general with that pattern. In this case
I'd ideally like some activity on the forked child before the next
request cycle - that is, if the forked child doesn't serve any
requests for a little bit, I'd still like my behavior to be
initialized.

Do you know of a way to get this behavior without a direct after fork hook?

Thanks for your help!
-Matt

On Thu, Jul 19, 2012 at 6:13 PM, Eric Wong <normalperson at yhbt.net> wrote:
> Matt Sanders <matt at modal.org> wrote:
>> 1. What is the best way to determine whether the app is indeed running
>> inside a unicorn server?
>
> Unicorn.listener_names will return a non-empty array of listener
> addresses.
>
>> 2. Is there an established way for adding an after_fork hook after
>> Unicorn has already started up?
>
> Nothing that I know of.
>
>> I'm aware of the ability to do this via the config file but I don't
>> want my users to have to add something to their unicorn config file.
>> Is there an equivalent to passenger's
>> PhusionPassenger.on_event(:starting_worker_process) method?
>
> Lately, I've been favoring the following pattern instead:
>
>   def initialize
>     @init_pid = $$
>     ...
>   end
>
>   def initialize_child_fork
>     @init_pid = $$
>     ...
>   end
>
>   def call(env)
>     initialize_child_fork if @init_pid != $$
>     ...
>   end
>
> The overhead is negligible and works regardless of server (even in
> non-HTTP servers somebody may write).  I really don't like code
> that would need to special case for all sorts of servers:
>
>   foo_for_passenger if defined?(PhusionPassenger)
>   foo_for_unicorn if defined?(Unicorn)
>   foo_for_something_fastcgi if defined?(SomethingFastCGI)
>   foo_for_something_scgi if defined?(SomethingSCGI)
>   foo_for_something_else_that_forks if defined?(YetAnotherForkingRackServer)
>   foo_for_something_else if defined?(SomethingElse)
>   ...
> _______________________________________________
> Unicorn mailing list - mongrel-unicorn at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mongrel-unicorn
> Do not quote signatures (like this one) or top post when replying


More information about the mongrel-unicorn mailing list