1

I currently have the following as the "path to executable" for a Windows service.

I would like to find out how I can convert it to a command-line application so that I can debug it interactively.

Windows service Path to executable: "C:\LONG_PATH_1\ruby\bin\rubyXXXX_console.exe" "C:\LONGPATH_2\windows_script.rb"

windows_script.rb is as follows:

# 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
chuacw
  • 1,685
  • 1
  • 23
  • 35
  • propably you can require the debugger gem in your windows script and after that simple write "debugger" in your methods..if the code calls such a method it will stop the code and you can debug in the normal rails console. – Matthias May 06 '13 at 11:11
  • Mattherick, thanks! I'm a newbie in Rails/Ruby. Can you provide specific steps to do this please? – chuacw May 06 '13 at 12:21

1 Answers1

2

Install ruby debugger gem

gem install debugger

Or add the gem to your Gemfile, for example if the script is in your /lib folder or something like that.

If you have problems during installation, maybe these answers help you further: Cannot install ruby-debug gem on Windows

Require debugger in your script

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

Now if you run your script, and the "second_method" gets called, your script will stop at the line you wrote "debugger". Now you can type in "irb" and the normal console will be started. You will have access to all local stuff in the console, in this example you can type in "foo" and => {} will be shown as result.

I must add here, that I never installed the gem on windows, only on Linux and Mac. But I think with these steps you can get an idea. Do you understand?

More information about debugging can be find here: http://guides.rubyonrails.org/debugging_rails_applications.html Check this out, there are more about debugging in Rails and with Ruby 2.0.

Since Rails has had built-in support for ruby-debug since Rails 2.0. Inside any Rails application you can invoke the debugger by calling the debugger method.

Community
  • 1
  • 1
Matthias
  • 4,355
  • 2
  • 25
  • 34