2011-03-26 79 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