第一次使用神宝石,而不是关闭守护进程

时间:2011-09-05 19:35:24

标签: ruby-on-rails-3 resque

这是我第一次使用神宝石。当我运行命令god terminate时,它不会关闭现有任务。所以下次再次运行神剧本时,它只会翻倍。

我看到Resque用于终止Rogue生成的示例脚本,但是不完全了解如何修改我的脚本。

命令我运行

god -c config/resque.god
god -c config/stale.god

我的resque.god文件:

RAILS_ROOT = File.dirname(File.dirname(__FILE__))

God.watch do |w|
    w.name          = "android_msg"
    w.group         = "resque-group"
    w.interval      = 30.seconds
    w.start         = "cd #{RAILS_ROOT} && rake resque:work QUEUE='android_message'"
    w.start_grace   = 10.seconds

    # restart if memory gets too high
    w.transition(:up, :restart) do |on|
        on.condition(:memory_usage) do |c|
        c.above = 350.megabytes
        c.times = 2
        end
    end

    # determine the state on startup
    w.transition(:init, { true => :up, false => :start }) do |on|
      on.condition(:process_running) do |c|
        c.running = true
      end
    end

    # determine when process has finished starting
    w.transition([:start, :restart], :up) do |on|
      on.condition(:process_running) do |c|
        c.running = true
        c.interval = 5.seconds
      end

      # failsafe
      on.condition(:tries) do |c|
        c.times = 5
        c.transition = :start
        c.interval = 5.seconds
      end
    end

    # start if process is not running
    w.transition(:up, :start) do |on|
        on.condition(:process_running) do |c|
        c.running = false
    end
  end
end


God.watch do |w|
    w.name          = "apn_sender"
    w.group         = "resque-group"
    w.interval      = 30.seconds
    w.start         = "cd #{RAILS_ROOT} && rake apn:sender"
    w.start_grace   = 10.seconds

    # restart if memory gets too high
    w.transition(:up, :restart) do |on|
        on.condition(:memory_usage) do |c|
        c.above = 350.megabytes
        c.times = 2
        end
    end

    # determine the state on startup
    w.transition(:init, { true => :up, false => :start }) do |on|
      on.condition(:process_running) do |c|
        c.running = true
      end
    end

    # determine when process has finished starting
    w.transition([:start, :restart], :up) do |on|
      on.condition(:process_running) do |c|
        c.running = true
        c.interval = 5.seconds
      end

      # failsafe
      on.condition(:tries) do |c|
        c.times = 5
        c.transition = :start
        c.interval = 5.seconds
      end
    end

    # start if process is not running
    w.transition(:up, :start) do |on|
        on.condition(:process_running) do |c|
        c.running = false
    end
  end
end

stale.god

# This will ride alongside god and kill any rogue stale worker
# processes. Their sacrifice is for the greater good.

WORKER_TIMEOUT = 60 * 10 # 10 minutes

Thread.new do
  loop do
    begin
      `ps -e -o pid,command | grep [r]esque`.split("\n").each do |line|
        parts   = line.split(' ')
        next if parts[-2] != "at"
        started = parts[-1].to_i
        elapsed = Time.now - Time.at(started)

        if elapsed >= WORKER_TIMEOUT
          ::Process.kill('USR1', parts[0].to_i)
        end
      end
    rescue
      # don't die because of stupid exceptions
      nil
    end

    sleep 30
  end
end

2 个答案:

答案 0 :(得分:0)

我认为你需要这样做:

god -c config/resque.god ;; starts god with the given config
god load config/stale.god ;; loads the given config into an already running god instance

答案 1 :(得分:0)

以下命令对我不起作用:

god load config/stale.god

在挖掘之后,我发现它可能只需要resque.god

的顶部
require 'stale.god'