Launch a daemon in a simple rail application

381 Views Asked by At

I have a simple controller and I want that controller to launch a daemon to do a background job (it read the sstdin data feeded by an Arduino board connected to the computer).

class DaemonController < ApplicationController
  def index
    require 'rubygems'
    require 'daemons'

    options = {
      :app_name => "collectTemperature",
      :multiple => true
    }
    readtty = Daemons.call(options) do
    loop {
      sleep 10
    }
    end
  end # def                                                                      
end # class       

It does create a process (from now it just sleeps but I will get into that once this simple code would work) but when I call the controller through the rails framework on the browser, I got a error message in the browser pointer to the line of the Daemon call (line 14).

SystemExit in DaemonController#index

daemons (1.1.9) lib/daemons/daemonize.rb:65:in `exit'
daemons (1.1.9) lib/daemons/daemonize.rb:65:in `call_as_daemon'
daemons (1.1.9) lib/daemons/application.rb:259:in `start_proc'
daemons (1.1.9) lib/daemons/application.rb:296:in `start'
daemons (1.1.9) lib/daemons.rb:252:in `call'
app/controllers/daemon_controller.rb:14:in `index'

I would greatly appreciate any help and am terribly sorry if that is a dumb question ;-)

1

There are 1 best solutions below

2
On BEST ANSWER

Daemons.call(options) begin rather than Daemons.call(options) do