自定义守护程序与Rails 3

cka*_*ass 14 ruby daemon ruby-on-rails ruby-on-rails-3

我正在尝试创建一个加载Rails环境的自定义守护程序.我的环境如下:ruby-1.9.2-p180 rails 3.0.5

我做了以下事情:

- 安装守护进程宝石

- 安装在这里的daemon_generator插件:https: //github.com/dougal/daemon_generator

-Generated守护进程:rails生成守护进程监听器

这一切都很好.当我运行守护进程时,它可以工作.

但是,只要我尝试访问活动记录对象(如尝试检索用户),它就会爆炸.

*** 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>
Run Code Online (Sandbox Code Playgroud)

有关如何创建加载Rails 3.0.5的守护进程的任何想法?

Ped*_*der 16

我更喜欢滚动自己的rails守护程序控制器.这是一个适用于大多数情况的简单示例:

脚本/守护

#!/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") 
Run Code Online (Sandbox Code Playgroud)

守护进程/ your_daemon_script.rb

require ENV["RAILS_ENV_PATH"]
loop { 
  ... your code ...
}
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令控制您的守护程序:

script/daemon run your_daemon_script.rb
script/daemon start your_daemon_script.rb
script/daemon stop your_daemon_script.rb
Run Code Online (Sandbox Code Playgroud)

这使我能够轻松添加新的守护进程,如果需要,我可以轻松地在每个脚本中加载rails.