2013-05-06 24 views
0

我目前擁有以下內容作爲Windows服務的「可執行文件路徑」。如何將Ruby/Rails Windows守護程序轉換爲可調試的應用程序?

我想了解如何將其轉換爲命令行應用程序,以便我可以交互式調試它。

Windows服務路徑爲可執行文件: 「C:\ LONG_PATH_1 \紅寶石\ BIN \ ruby​​XXXX_console.exe」 「C:\ LONGPATH_2 \ windows_script.rb」

windows_script.rb如下:

# console_windows.rb 
# 
# Windows-specific service code. 

# redirect stdout/stderr to file, else Windows Services will crash on output 
$stdout.reopen File.open(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 
     'log', 'XXXX_console.output')), 'a') 
$stderr.reopen $stdout 
$stdout.sync = true 
$stderr.sync = true 

require File.join(File.dirname(File.expand_path(__FILE__)),"script_helper.rb") 

require 'rubygems' 
require 'win32/daemon' 
include Win32 

class BackgroundWindowsDaemon < Daemon 

    def service_init 
    # Do startup in service_main so that Windows Services doesn't timeout during startup 
    end 

    def service_main 
    BackgroundScriptHelper.request_start 
    BackgroundScriptHelper.monitor 
    end 

    def service_stop 
    BackgroundScriptHelper.request_stop 
    end 

end 

BackgroundWindowsDaemon.new.mainloop 
+0

顯然,你可以在你的windows腳本中需要調試器gem,然後在你的方法中簡單地寫入「調試器」。如果代碼調用這種方法,它將停止代碼,你可以在普通的rails控制檯中進行調試。 – Mattherick 2013-05-06 11:11:37

+0

Mattherick,謝謝!我是Rails/Ruby中的新手。你能提供具體步驟來做到這一點嗎? – chuacw 2013-05-06 12:21:16

回答

1

安裝Ruby調試寶石

gem install debugger 

或者寶石添加到您的Gemfile,例如,如果腳本是在你的/ lib文件夾或類似的東西。

如果您在安裝過程中出現問題,也許這些答案可以幫助你進一步:Cannot install ruby-debug gem on Windows

要求調試器在腳本

require 'rubygems' 
require 'win32/daemon' 
include Win32 
require "debugger" 

class BackgroundWindowsDaemon < Daemon 

    def first_method 
    puts "i am doing something" 
    end 

    def second_method 
    puts "this is something I want debug now" 
    # your code is here.. 
    foo = {} 
    debugger 
    foo = { :bar => "foobar" } 
    end 

    # more code and stuff... 

end 

現在,如果你運行你的腳本,而「second_method」被調用,你的腳本將停止在你寫的「調試器」的那一行。現在你可以輸入「irb」,普通控制檯就會啓動。您將可以訪問控制檯中的所有本地內容,在本例中,您可以輸入「foo」並且=> {}將顯示爲結果。

我必須在這裏添加,我從來沒有在Windows上安裝gem,只在Linux和Mac上安裝。但我認爲通過這些步驟你可以得到一個想法。你明白嗎?

有關調試的更多信息可以在這裏找到:http://guides.rubyonrails.org/debugging_rails_applications.html檢查了這一點,還有更多關於在Rails和Ruby 2.0中進行調試的信息。

由於自Rails 2.0以來,Rails已經內置了對ruby-debug的支持。在任何Rails應用程序中,您都可以通過調用調試器方法來調用調試器。

相關問題