Forking non web processes

Eric Wong normalperson at yhbt.net
Fri Oct 25 20:44:35 UTC 2013


Sam Saffron <sam.saffron at gmail.com> wrote:
> The only reason for this level of crazy is to get the 30% memory
> saving you do when forking off a master in Ruby 2.0s CoW friendly GC.
> I understand this is less traditional, but perfectly acceptable for
> low cost VPS hosting.
> 
> The unicorn master does "reap/terminate" the child fork on startup, so
> you are forced to hold off on forking.

You can probably replace a worker process in the after_fork hook

(totally untested)

after_fork do |server, worker|
  if worker.nr == 1
    server.listeners = [] # drop listen sockets

    # keep ticking
    Thread.new { worker.tick = Time.now.to_i while sleep(server.timeout/2) }
    begin
      run_your_own_thing
    ensure
      exit
    end
  end
end

Or even just fork off via your Rack app.  This will be more portable in
case you want to try a different server.  unicorn workers do nothing
special with SIGCHLD, so your app can do whatever it wants with it.


More information about the mongrel-unicorn mailing list