Managing unicorn instances / rails deployment

1.7k views Asked by At

my head hurts today! :)

I need some help with rails deployment.

I migrated from cherokee to nginx and well, I migrated my django apps easily.

I just have to launch uwsgi to get a tcp socket and run my app. So I use supervisord to start / stop uwsgi sockets for every app.

I want something similar for rails. I just started with rails but I want to be able to deploy now so I won't have problems in a future.

I read all almost all internet and well, I have to ask here :)

My app lives in "/srv/http/hello/"

I use unicorn with a fancy config/unicorn.rb

worker_processes 2
working_directory "/srv/http/hello/"

# This loads the application in the master process before forking
# worker processes
# Read more about it here:
# http://unicorn.bogomips.org/Unicorn/Configurator.html
preload_app true

timeout 30

# This is where we specify the socket.
# We will point the upstream Nginx module to this socket later on
listen "/srv/http/hello/tmp/sockets/unicorn.sock", :backlog => 64

pid "/srv/http/hello/tmp/pids/unicorn.pid"

# Set the path of the log files inside the log folder of the testapp
stderr_path "/var/log/unicorn/hello-stderr.log"
stdout_path "/var/log/unicorn/hello-stdout.log"

before_fork do |server, worker|
# This option works in together with preload_app true setting
# What is does is prevent the master process from holding
# the database connection
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
# Here we are establishing the connection after forking worker
# processes
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

I just adapted some example for internet.

If I run something like:

unicorn_rails -c config/unicorn.rb -D

It works like a charm. I tried to put that command in supervisord but hehe, I asked too much for it.

So, with some research I discovered god, so I picked the example of github and I put it on "config/god.rb" (which is the good place?)

# http://unicorn.bogomips.org/SIGNALS.html

rails_env = ENV['RAILS_ENV'] || 'development'
rails_root = ENV['RAILS_ROOT'] || "/srv/http/hello"

God.watch do |w|
  w.name = "unicorn"
  w.interval = 30.seconds # default

  # unicorn needs to be run from the rails root
  w.start = "cd #{rails_root} && /srv/http/.rvm/gems/ruby-1.9.3-p0@hello/bin/unicorn_rails -c #{rails_root}/config/unicorn.rb -E #{rails_env} -D"

  # QUIT gracefully shuts down workers
  w.stop = "kill -QUIT `cat #{rails_root}/tmp/pids/unicorn.pid`"

  # USR2 causes the master to re-create itself and spawn a new worker pool
  w.restart = "kill -USR2 `cat #{rails_root}/tmp/pids/unicorn.pid`"

  w.start_grace = 10.seconds
  w.restart_grace = 10.seconds
  w.pid_file = "#{rails_root}/tmp/pids/unicorn.pid"

  #w.uid = 'http'
  #w.gid = 'webgroup'

  w.behavior(:clean_pid_file)

  w.start_if do |start|
    start.condition(:process_running) do |c|
      c.interval = 5.seconds
      c.running = false
    end
  end

  w.restart_if do |restart|
    restart.condition(:memory_usage) do |c|
      c.above = 300.megabytes
      c.times = [3, 5] # 3 out of 5 intervals
    end

    restart.condition(:cpu_usage) do |c|
      c.above = 50.percent
      c.times = 5
    end
  end

  # lifecycle
  w.lifecycle do |on|
    on.condition(:flapping) do |c|
      c.to_state = [:start, :restart]
      c.times = 5
      c.within = 5.minute
      c.transition = :unmonitored
      c.retry_in = 10.minutes
      c.retry_times = 5
      c.retry_within = 2.hours
    end
  end
end

NOTE: I commented the uid and gid since I launch it from the http user or I get a permission error writing the pid. Also I put "development" because is just a "rails new hello"

Ok, so this works:

god -c config/god.rb -D

god launch unicorn good and in another terminal and I can do "god stop unicorn" and it works.

So questions...

1 - Is this the correct way? 2 - Do I need one god config for every project and launch a god process for every project? 3 - How can I manage those god's process? Something like supervisord "supervisorctl restart djangoproject" 4 - Will I die if I put "killall god" 3 times in a row? :P 5 - NEW QUESTION: Im too far if I say that I just need 1 god config with ALL unicorn instances, launch it in some form and just manage it with god? god start blah, god start bleh...

Thanks a lot, I just need to start rails development with a good system administration.

1

There are 1 answers

5
roberto On BEST ANSWER

If you already have experience with uWSGI why not using it for rails too ?

http://projects.unbit.it/uwsgi/wiki/RubyOnRails

If you plan to host a lot of apps consider using the Emperor

http://projects.unbit.it/uwsgi/wiki/Emperor