Question

I've written a ruby NFC reader script and daemonised it with the daemons gem. It all works great except the script only runs once...

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

This works great and i'm receiving the tag.uid in my app. But when i scan another RFID tag the script wont run again...

Does anyone know how to adjust the script that it runs "forever" and stops when the daemon is stopped?

Thanks

UPDATE

i updated my daemon.rb script:

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)

But it just runs once... i don't get it any other suggestions?

Was it helpful?

Solution

You almost certainly want to be using Daemon.run. run_proc would be useful if you wanted to move the code from touchtag.rb into Daemon.rb.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top