2011-01-05 45 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