管理unicorn实例/ rails部署

时间:2012-02-05 23:33:36

标签: ruby-on-rails nginx unicorn god

今天我的头疼! :)

我需要一些有关rails部署的帮助。

我从切诺基迁移到nginx,我很容易迁移我的django应用程序。

我只需要启动uwsgi来获取tcp套接字并运行我的应用程序。所以我使用supervisord为每个应用程序启动/停止uwsgi套接字。

我想要类似于rails的东西。我刚开始使用rails,但我希望能够立即部署,以便将来不会出现问题。

我读了几乎所有的互联网,我在这里要问:)

我的应用程序位于“/ srv / http / hello /”

我使用unicorn和花哨的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

我刚刚为互联网改编了一些例子。

如果我运行的话:

unicorn_rails -c config/unicorn.rb -D

它就像一个魅力。我试图把这个命令放在监督中,但是,我对此问得太多了。

所以,通过一些研究,我发现了上帝,所以我选择了github的例子,我把它放在“config / god.rb”(这是个好地方?)

# 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

注意:我评论了uid和gid,因为我从http用户启动它,或者我写了pid的权限错误。我也把“开发”放在了“因为只是一个”新的你好“

好的,这样可行:

god -c config/god.rb -D
上帝发动独角兽很好,在另一个终端,我可以做“上帝停止独角兽”,它的确有效。

所以问题......

1 - 这是正确的方法吗? 2 - 我是否需要为每个项目配置一个神配置并为每个项目启动一个神过程? 3 - 我如何管理那些上帝的过程?像supervisord“supervisorctl restart djangoproject”之类的东西 4 - 如果我连续3次放入“killall god”,我会死吗? :P 5 - 新问题:我太过分了,如果我说我只需要一个神配置和所有独角兽实例,以某种形式启动它并与上帝管理它?上帝开始等等,上帝开始...... ...

非常感谢,我只需要通过良好的系统管理来启动rails开发。

1 个答案:

答案 0 :(得分:0)

如果您已经有使用uWSGI的经验,为什么不将它用于rails呢?

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

如果您打算主持许多应用程序,请考虑使用Emperor

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