开发者

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?


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.


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 }


Looking at the code in https://github.com/dougal/daemon_generator/blob/master/lib/generators/daemon/templates/script.rb it appears they load things in the wrong order...

Looking at my delayed_job daemon script and config.ru they load config/environment.rb (which in turn loads application.rb and initializes the app)

So a possible fix would be to edit the generated script and make it only require 'config/environment.rb'

I tried this:

 #!/usr/bin/env ruby

 # You might want to change this
 ENV["RAILS_ENV"] ||= "development"

 require File.dirname(__FILE__) + "/../config/environment"

 $running = true
 Signal.trap("TERM") do
   $running = false
 end

 while($running) do

   # Replace this with your code
   Rails.logger.auto_flushing = true
   o = Order.last
   Rails.logger.info "The latest order is #{o.id}"

   sleep 10
 end

and it yielded no errors... (Tried both Rails 3.0.3 and 3.0.5)


I had problems running daemon as is on my staging server (Rails 3.0.7, ruby 1.8.7, passenger 3.0.0). Neither

require File.dirname(FILE) + "/../../config/application" Rails.application.require_environment!

nor

require File.dirname(FILE) + "/../config/environment"

Worked.

I fixed it by re-installing the standard config.ru in rails root (which I had de-installed to integrate w passenger... not sure now how I will get daemons & passenger to work together now ...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜