【问题标题】:Managing unicorn instances / rails deployment管理独角兽实例/rails 部署
【发布时间】:2023-03-20 13:39:01
【问题描述】:

我今天头疼! :)

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

我从 cherokee 迁移到 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

它就像一个魅力。我试图把这个命令放在 supervisord 中,但是嘿嘿,我要求太多了。

所以,通过一些研究我发现了上帝,所以我选择了 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 时收到权限错误。另外我把“开发”放在一边,因为它只是一个“rails new hello”

好的,这样就可以了:

god -c config/god.rb -D

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

所以问题...

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

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

【问题讨论】:

    标签: ruby-on-rails nginx unicorn god


    【解决方案1】:

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

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

    如果您打算托管大量应用程序,请考虑使用 Emperor

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

    【讨论】:

    • 确实是个好主意。但是在 uWSGI 上使用 rails 兼容性有点问题:P
    • 有没有办法在 uwsgi 中使用 gemset?我将 用于 django。
    • 你可以使用 GEM_PATH 和 GEM_HOME 环境变量,用 --env 选项指向包含 gemset 的目录
    • 还是不行。我试过: uwsgirails -s :40510 -M -p 4 -m --rack /srv/http/hello/config.ru --post-buffering 4096 --env RAILS_ENV=development GEM_PATH=/usr/local/rvm/gems /ruby-1.9.3-p0@hello/gems/ GEM_HOME=/usr/local/rvm/gems/ruby-1.9.3-p0@hello/gems/ 这不起作用,它试图在“通用“红宝石,不在宝石组内。如果我手动输入该 gemset,它就可以工作。你有一个可行的例子吗?
    • nvm,在尝试部署 hello world 50 小时后,我回到了我可爱的 ​​django。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 2013-07-08
    • 2014-05-22
    相关资源
    最近更新 更多