0

我目前将以下内容作为 Windows 服务的“可执行文件路径”。

我想知道如何将它转换为命令行应用程序,以便我可以交互式地调试它。

Windows 服务可执行文件路径:“C:\LONG_PATH_1\ruby\bin\rubyXXXX_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
4

1 回答 1

1

安装 ruby​​ 调试器 gem

gem install debugger

或者将 gem 添加到您的 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 从 Rails 2.0 开始就内置了对 ruby​​-debug 的支持。在任何 Rails 应用程序中,您都可以通过调用调试器方法来调用调试器。

于 2013-05-06T14:26:15.933 回答