2011-01-05 87 views
0

我寫了一個ruby NFC讀寫器腳本並使用守護程序gem守護它。這一切的偉大工程,除了腳本只運行一次......Ruby守護程序腳本只運行一次

Daemon.rb

require 'rubygems' 
require 'daemons' 

pwd = File.dirname(File.expand_path(__FILE__)) 
file = pwd + '/touchatag.rb' 

Daemons.run_proc(
    'touchatag_project_daemon', # name of daemon 
    :dir_mode => :normal, 
    :dir => File.join(pwd, 'tmp/pids'), # directory where pid file will be stored 
    :backtrace => true, 
    :monitor => true, 
    :log_output => true 
) do 
    exec "ruby #{file}" 
end 

touchatag.rb

quire 'rubygems' 
require 'nfc' 
require 'httparty' 

class TagAssociator 
    include HTTParty 
    base_uri 'localhost:3000' 
end 

NFC.instance.find do |tag| 
    puts "Processing tag..." 
    TagAssociator.post('/answers/answer', :query => {:uid => tag.uid}) 
end 

這個偉大的工程,我收到tag.uid在我的應用程序但當我掃描另一個RFID標籤腳本不會再運行...

有誰知道如何調整腳本,它運行「永遠」,並停止守護進程停止時?

感謝

UPDATE

我更新了我的daemon.rb腳本:

require 'rubygems' 
require 'daemons' 

options = { 
    :app_name => "touchatag_project_daemon", 
    :ARGV  => ['start', '-f', '--', 'param_for_myscript'], 
    :dir_mode => :script, 
    :dir  => 'tmp/pids', 
    :multiple => true, 
    :ontop  => true, 
    # :mode  => :exec, 
    :backtrace => true, 
    :monitor => true 
} 

Daemons.run(File.join(File.dirname(__FILE__), '/touchatag.rb'), options) 

,但它只是運行一次......我不明白,任何其他建議?

回答

1

你幾乎肯定想要使用Daemon.run。如果您想將代碼從touchtag.rb移動到Daemon.rb,那麼run_proc會很有用。

http://daemons.rubyforge.org/classes/Daemons.html#M000004

+0

我更新我的帖子你的建議,但它仍然只運行一次...... – 2011-01-05 18:37:33

+0

對不起,我忘了提,你需要用你的touchtag代碼在一個循環(可能與睡眠或東西那裏)。請注意守護進程的例子總是出現在帶有「睡眠5」等的'loop {...}'中。 – cam 2011-01-05 20:39:18

+0

太棒了!它的工作就像一個魅力!謝謝你的幫助! – 2011-01-05 21:17:22