2011-07-13 86 views
2

我正在嘗試創建一個自定義守護進程,每60秒運行一次並啓動/停止並使用bluepill監控它。Ruby守護進程和bluepill監控

有人可以告訴我如何做到這一點更容易或請你能告訴我我做錯了什麼嗎?

當前bluepill正在啓動守護程序,以便它運行,但似乎不知道它的運行,因爲不斷嘗試停止並啓動它。

這裏是我的.pill文件我的代碼

app.process("get_dropboxes") do |process| 

    process.working_dir = RAILS_ROOT 
    process.pid_file = File.join(RAILS_ROOT, "tmp", "pids", "get_dropbox.pid") 
    process.start_command = "/usr/bin/env RAILS_ENV=#{RAILS_ENV} bundle exec lib/daemons/get_dropbox_ctl start" 
    process.stop_command = "/usr/bin/env RAILS_ENV=#{RAILS_ENV} bundle exec lib/daemons/get_dropbox_ctl stop" 

    process.start_grace_time = 10.seconds 
    process.stop_grace_time = 10.seconds 
    process.restart_grace_time = 10.seconds 

    process.checks :cpu_usage, :every => 30.seconds, :below => 20, :times => [3,5] 
    process.checks :mem_usage, :every => 30.seconds, :below => 350.megabytes, :times => [3,5] 

end 

這裏是我的get_dropbox_ctl文件

#!/usr/bin/env ruby 
require 'rubygems' 
require "daemons" 
require 'yaml' 
require 'erb' 

gem 'activesupport', '>=3.0.0' 
require 'active_support' 

# For some reason, ActiveSupport 3.0.0 doesn't load. 
# Load needed extension directly for now. 
require "active_support/core_ext/object" 
require "active_support/core_ext/hash" 

options = YAML.load(
    ERB.new(
    IO.read(
     File.dirname(__FILE__) + "/../../config/daemons.yml" 
)).result).with_indifferent_access 

options[:dir_mode] = options[:dir_mode].to_sym 

Daemons.run File.dirname(__FILE__) + "/get_dropbox.rb", options 

這裏是我的get_dropbox.rb文件

#!/usr/bin/env ruby 

# You might want to change this 
ENV["RAILS_ENV"] ||= "production" 

require 'net/pop' 
require File.dirname(__FILE__) + "/../../config/application" 
Rails.application.require_environment! 

$running = true 
Signal.trap("TERM") do 
    $running = false 
end 

while($running) do 


    # do stuff ....... 

    sleep 60 
end 

日誌我得到

[2011-07-13T16:55:00.464202 #32257] WARN -- : [domain.com:get_dropboxes] pid_file /var/www/domain.com/current/tmp/pids/get_dropbox.pid does not exist or cannot be read 
W, [2011-07-13T16:55:00.464315 #32257] WARN -- : [domain.com:get_dropboxes] pid_file /var/www/domain.com/current/tmp/pids/get_dropbox.pid does not exist or cannot be read 
W, [2011-07-13T16:55:00.464505 #32257] WARN -- : [domain.com:get_dropboxes] Executing start command: /usr/bin/env RAILS_ENV=production bundle exec lib/daemons/get_dropbox_ctl start 
I, [2011-07-13T16:55:01.602210 #32257] INFO -- : [domain.com:get_dropboxes] Going from down => starting 

當然有比這更簡單的方法嗎?

+0

你有沒有一個原因,你不這樣做的cron工作? – moritz

+0

我需要監視它,並自動重新啓動它,如果它失敗了,我正在使用bluepill來監視其他事情,所以只是認爲這是最好的選擇?還是我錯了? – rick

+0

這取決於我認爲:當你想每60秒完成一項工作時,cron是一個不錯的選擇,因爲如果你的實際工作需要長時間的加息(比如5秒),它不會「漂移」。 60秒的睡眠表明這比你真實的守護進程更接近你的場景? – moritz

回答

0

我不確定這是不是您要找的,但據我所知,您可以跳過守護程序文件並使用bluepill。只要你指定一個pid文件,你可以設置process.daemonize = true,它會爲你創建一個守護進程。

+0

謝謝so什麼是process.start_command命令來啓動rails環境下的ruby腳本?或者這只是一個耙子任務? – rick

+0

只需使用rake任務,它應該可以工作。如果您不指定停止任務,它將通過使用過程信號結束過程(通常只是起作用)。 – namxam

+0

是的,對於在前臺運行的任何進程,指定'process.daemonize'進行藍屏並且事情會變得很甜美。如果進程自我守護進程,那麼進程*必須*生成它自己的PID文件,並且必須*告訴bluepill在'grace_start_time'已經通過後可以找到該pid文件的位置。 – d11wtq