Multiple rack applications on the same server with unicorn

Eric Wong normalperson at yhbt.net
Fri Sep 30 13:16:08 EDT 2011


John Joseph Bachir <johnjosephbachir at gmail.com> wrote:
> If I'm running two rails apps on the same server using Unicorn, I have
> to run two instances of Unicorn, right?

I'm not sure if Rails (or any other frameworks) has issues, but plain
Rack applications will work.  Rack lets you do something like this in
config.ru:

  --------------------- config.ru ------------------
  map("http://foo.example.com/") do
    run FooApp.new
  end
  map("http://bar.example.com/") do
    run BarApp.new
  end
  ----------------------- 8< -----------------------

Both applications need to be able to work from the same working
directory, though.

> If so, then here's a place where passenger might win in terms of
> memory use, as the rails code will be loaded into memory twice, right?

It's all dependent on whether Rails lets you run two apps in the same
process or not (in which case Unicorn can do it).

The following example is pretty ugly, but lets you split your workers
between two applications:

  ----------------- unicorn.conf.rb -----------------
  preload_app false
  worker_processes 2 # need more than one
  after_fork do |server,worker|
    $unicorn_worker_nr = worker.nr
  end
  ---------------------------------------------------

  --------------------- config.ru ------------------
  if ($unicorn_worker_nr % 2) == 0
    run FooApp.new
  else
    run BarApp.new
  end
  ----------------------- 8< -----------------------

-- 
Eric Wong


More information about the mongrel-unicorn mailing list