Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Daemon with Rails 3

I'm trying to create a custom daemon that loads up the Rails environment. My environment is as follows: ruby-1.9.2-p180 rails 3.0.5

I did the following:

-Installed the daemons gem

-Installed daemon_generator plugin found here: https://github.com/dougal/daemon_generator

-Generated a daemon: rails generate daemon listener

All this worked fine. When I run the daemon, it works.

However, as soon as I try to access an active record object like trying to retrieve a user, it blows up.

*** below you find the most recent exception thrown, this will be likely (but not certainly) the exception that made the application exit abnormally ***
#<NameError: method `recognize' not defined in Rack::Mount::RouteSet>
*** below you find all exception objects found in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***
#<NoMemoryError: failed to allocate memory>
#<SystemStackError: stack level too deep>
#<fatal: exception reentered>
#<NoMethodError: undefined method `eq' for nil:NilClass>
#<NameError: method `recognize' not defined in Rack::Mount::RouteSet>

Any thoughts on how to create a Daemon that loads up Rails 3.0.5?

like image 677
ckarbass Avatar asked Mar 26 '11 08:03

ckarbass


2 Answers

I prefer to roll my own rails daemon controllers. Here is a simple example that works for most cases:

script/daemon

#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'

ENV["APP_ROOT"] ||= File.expand_path("#{File.dirname(__FILE__)}/..")
ENV["RAILS_ENV_PATH"] ||= "#{ENV["APP_ROOT"]}/config/environment.rb"

script = "#{ENV["APP_ROOT"]}/daemons/#{ARGV[1]}"

Daemons.run(script, dir_mode: :normal, dir: "#{ENV["APP_ROOT"]}/tmp/pids") 

daemons/your_daemon_script.rb

require ENV["RAILS_ENV_PATH"]
loop { 
  ... your code ...
}

You can control your deamons by using the following commands:

script/daemon run your_daemon_script.rb
script/daemon start your_daemon_script.rb
script/daemon stop your_daemon_script.rb

This enables me to easily add new daemons and I can easily load rails in each script if necessary.

like image 101
Peder Avatar answered Nov 05 '22 12:11

Peder


I had a lot of problems trying get daemon_generator working. I got my daemon working by skipping the daemon_generator all together and just using the daemons gem (v1.1.3).

in urserver_control.rb (in root ruby app directory):

    #!/usr/bin/env ruby
    require 'rubygems'
    require 'daemons'
    require 'TweetMsg'

    Daemons.run('urserver.rb')

in urserver.rb:

#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), 'config', 'environmen
t'))
require "rubygems"

  --- insert your code here ---

You can test by running your server directly ruby urserver.rb or ruby urserver_controller run And then once that is working starting and stopping the controllerruby urserver_control.rb {start | stop | run }

like image 2
Craig Avatar answered Nov 05 '22 13:11

Craig