【问题标题】:God starts too many processes上帝启动了太多的过程
【发布时间】:2012-10-24 22:42:27
【问题描述】:

我有一个上帝脚本,它应该监视两个跟踪进程。 问题是 24 小时后它启动了太多进程。

这是神脚本。

rails_root = File.expand_path("../..", __FILE__)

2.times do |n|
  God.watch do |w|
    w.group = "app-scripts"
    w.name  = "run-#{n}"
    w.interval = 30.seconds
    w.dir      = File.dirname(__FILE__)

    w.env = {
      "BUNDLE_GEMFILE" => "#{rails_root}/Gemfile",
      "RAILS_ENV" => "production",
      "BEANSTALK_URL" => "beanstalk://127.0.0.1:54132"
    }

    w.start = "bbundle exec stalk #{File.join(rails_root, "config/jobs.rb")}"

    w.start_grace = 5.seconds
    w.stop_grace  = 5.seconds

    w.start_if do |start|
      start.condition(:process_running) { |c| c.running = false }
    end

    w.restart_if do |restart|
      restart.condition(:memory_usage) do |c|
        c.above = 200.megabytes
        c.times = [3, 5]
      end

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

    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
end

ps aux | grep stalk 返回以下内容。

root      3178  0.2  2.7 417580 117284 ?       Sl   Oct28   2:22 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root      3179  0.2  3.3 506068 138740 ?       Sl   Oct28   2:26 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root      4588  0.2  2.9 497932 121664 ?       Sl   Oct25  16:10 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root      4794  0.2  3.0 497792 128084 ?       Sl   Oct25  15:57 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     10391  0.2  2.8 496784 121388 ?       Sl   Oct25  15:44 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     10392  0.2  2.8 497624 121528 ?       Sl   Oct25  15:31 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     18874 75.0  2.0 214116 83948 ?        Rl   15:49   0:09 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     18875 75.0  2.0 214944 84868 ?        Rl   15:49   0:09 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     20649  0.2  2.6 410636 110012 ?       Sl   Oct28   2:44 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     20650  0.2  3.0 439284 128996 ?       Sl   Oct28   2:47 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     23272  0.2  2.7 414452 115772 ?       Sl   Oct28   2:44 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     23273  0.2  2.7 417728 117152 ?       Sl   Oct28   2:44 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     25919  0.2  3.1 436276 131876 ?       Sl   Oct28   2:28 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     25920  0.2  3.3 503236 138676 ?       Sl   Oct28   2:29 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     28782  0.2  2.8 431836 121108 ?       Sl   Oct25  16:58 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     30687  0.2  2.7 415908 117008 ?       Sl   Oct28   2:39 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb
root     30688  0.2  2.6 476184 111844 ?       Sl   Oct28   2:37 ruby /opt/www/myapp/shared/bundle/ruby/1.9.1/bin/stalk /opt/www/myapp/current/config/jobs.rb

这是/usr/bin/bbundle 脚本。

#!/usr/bin/env bash

if [[ -s "/home/webmaster/.rvm/environments/ruby-1.9.2-p320@webmaster" ]]
then
  source "/home/webmaster/.rvm/environments/ruby-1.9.2-p320@webmaster"
  bundle  "$@"
else
  echo "ERROR: Missing RVM environment file: '/home/webmaster/.rvm/environments/ruby-1.9.2-p320@webmaster'" >&2
  exit 1
fi
  • 运行sudo god stop app-scripts 不会杀死任何进程。

  • 我已尝试将w.uid = "webmaster" 添加到上帝脚本中,但问题仍然存在。

  • 我正在运行god版本0.12.1,ruby版本1.9.3p286和stalker版本0.9.0

我做错了什么?

【问题讨论】:

    标签: ruby beanstalkd god


    【解决方案1】:

    似乎god 正在尝试关注bbundle 而不是stalk。您需要让god 知道在哪里可以找到您想要使用w.pid_file 跟踪的实际进程的PID。如果标准kill 无法解决问题,您可能还需要告诉它如何终止进程。为此,您可以将 w.stop_signal 用于不同的信号(如 simonmenke 建议的那样)或使用 w.stop 用于整个其他命令。

    日志文件应该更清楚地说明正在发生的事情。致电god -D 打印到stdoutgod -l /var/log/god.log

    【讨论】:

    • 这是上帝的输出。 gist.github.com/dcac99caec1b712c1817 是的,看起来上帝正在杀死/usr/bin/bbundle 而不是ruby stalk。我猜我必须指定 pid 文件的放置位置。
    • 使用 stalker 时似乎无法指定 pid 文件。我现在该怎么办 ?我不确定如何继续。
    • 好吧,我们可以发挥创意。您可以让第一个作业将 PID 写入文件并将god 引用到该文件。或者只是编写您自己的 bin/stalk 版本,在运行作业队列之前写入 PID 文件。
    • 一种解决方案是让 stalk 运行 File.open("pid-file.pid", "w") { |file| file.write($$.to_s) }。我会做一些研究。
    • 感谢 mpapis @ rvm irc,我解决了这个问题。这是新的bbundle 脚本gist.github.com/8eca8688f2caef15c595。我将第 6 行从 bundle "$@" 更改为 exec bundle "$@"
    【解决方案2】:

    堆栈器在收到INT 信号(不是TERM 信号)时停止。 尝试添加停止信号:

    # ...
    w.stop_signal = 'INT'
    # ...
    

    【讨论】:

    • 那行不通。我尝试运行sudo god restart myapp,但它只启动了一个新进程,并没有杀死旧进程。
    • 跟踪者进程是否会自行分离(妖魔化)它们?如果是这样,您还需要告诉上帝在哪里可以找到 pid。
    • 不,stalker 不会自己妖魔化它。那应该没有必要。
    【解决方案3】:

    这应该有助于解决您的问题:Monitor a Rake task with God

    简而言之,您可以在您的上帝配置中存储对您的 PID 文件的引用:

     God.watch do |w|
       w.dir = "#{rails_root}"
       w.name = "my_task"
       w.interval = 10.seconds
       w.pid_file = "#{rails_root}/tmp/pids/#{w.name}.pid"
       w.env = {"RAILS_ENV"=>rails_env, 'PIDFILE' => w.pid_file}
       w.start = "bundle exec rake my_task &"
       ...
     end
    

    并且在您的运行过程中,您将您的 PID(在本例中为 rake)写入此文件:

     task :my_task => :environment do
       File.open(ENV['PIDFILE'], 'w') { |f| f << Process.pid } if ENV['PIDFILE']
       Model.perform_task!
     end
    

    我们将上帝正在监视的 PID 文件的路径传递给实际被监视的进程,并将其 PID 写入该文件(然后由上帝监视)。希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      感谢 mpapis @ rvm irc,我解决了这个问题。我不得不将 bbundle 脚本中的第 6 行从 bundle "$@" 更改为 exec bundle "$@"

      #!/usr/bin/env bash
      
      if [[ -s "/home/webmaster/.rvm/environments/ruby-1.9.2-p320@webmaster" ]]
      then
        source "/home/webmaster/.rvm/environments/ruby-1.9.2-p320@webmaster"
        exec bundle  "$@"
      else
        echo "ERROR: Missing RVM environment file: '/home/webmaster/.rvm/environments/ruby-1.9.2-p320@webmaster'" >&2
        exit 1
      fi
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多