2011-03-26 74 views
14

我正在尝试创建一个加载Rails环境的自定义守护进程。 我的环境如下: 红宝石1.9.2-P180 轨3.0.5用Rails 3自定义守护进程

我做了以下内容:

-Installed守护进程宝石

-Installed daemon_generator插件这里找到: https://github.com/dougal/daemon_generator

- 生成一个守护进程:轨生成守护进程监听

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

但是,只要尝试访问活动记录对象就像试图检索用户一样,就会炸毁。

*** 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> 

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

+0

我在这里有同样的错误。我甚至不能像现在那样运行守护进程。我正在使用ree-1.8.7-2011.03。对不起,没有帮助。 – Jaryl 2011-04-03 21:02:27

+0

我收到这个错误使用rvm ruby​​-1.9.2-p136和rails 3.0.7。你有没有找到解决方案? – 2011-06-09 15:25:49

回答

16

我更愿意推出自己的轨道守护程序控制器。这里是一个为大多数情况下工作的一个简单的例子:

脚本/守护

#!/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") 

守护进程/ your_daemon_script.rb

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

你可以控制你的守护程序通过使用以下命令:

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

这使我可以轻松地添加新的守护进程,我可以轻松地加载ra如有必要,在每个脚本中都有。

+0

为什么rubygems是一个需求? – 2015-06-04 22:36:55

1

综观https://github.com/dougal/daemon_generator/blob/master/lib/generators/daemon/templates/script.rb代码看来他们装载的东西在错误的顺序...

在我的delayed_job守护程序的脚本和config.ru寻找它们加载到config/environment.rb(这反过来负载应用。 RB和初始化应用程序)

因此,一个可能的解决将是编辑生成的脚本,使其只需要“到config/environment.rb”

我尝试这样做:

#!/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 

它没有产生任何错误...(试过Rails 3.0.3和3.0.5)

+0

有趣的是,你运行的是哪个版本的Ruby?我尝试了这一切,并得到了原始的海报错误使用rvm ruby​​-1.9.2-p136和rails 3.0.7 – 2011-06-09 15:27:07

0

我在登台服务器上运行守护程序时出现问题(Rails 3.0.7,ruby 1.8.7,passenger 3.0.0)。无论

需要File.dirname(FILE)+ “/../../config/application” Rails.application.require_environment!

也不

要求File.dirname(FILE)+ “/../config/environment”

奏效。

我通过在rails根目录下重新安装标准config.ru修复了这个问题(我已经卸载以集成w乘客...现在不确定我现在怎么得到守护程序&乘客现在一起工作... )

1

我有很多问题试图让daemon_generator工作。我通过跳过daemon_generator并使用守护进程gem(v1.1.3)来让我的守护进程工作。

在urserver_control.rb(在根红宝石app目录)

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

    Daemons.run('urserver.rb') 
在urserver

。RB:

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

    --- insert your code here --- 

您可以通过直接运行,服务器ruby urserver.rbruby urserver_controller run 测试,然后一旦正在启动和停止控制器ruby urserver_control.rb {start | stop | run }

+0

这就是我所做的 – ckarbass 2011-06-14 21:40:12