[Win32utils-devel] Problems with custom service and webrick

Daniel Berger djberg96 at yahoo.com
Sat May 14 01:00:22 EDT 2005


Hi all,

Windows XP Pro
Ruby 1.8.2

I'm having a couple of problems with the following
service.  The first issue is that the code in
service_start doesn't seem to fire off.  The second
problem is that any attempt to do 'require "webrick"'
within webrick_daemon.rb causes the service to fail on
start.

Any ideas?

# webrickctl.rb
###############################################
# webrickctl.rb
#
# Control script for the Webrick Service
###############################################
require "optparse"
require "win32/service"
include Win32

OPTIONS = {}

ARGV.options do |opts|
   opts.on("-d", "--delete", "Delete the service"){
OPTIONS[:delete] = true }
   opts.on("-s", "--start",  "Start the service"){
OPTIONS[:start] = true }
   opts.on("-x", "--stop",   "Stop the service"){
OPTIONS[:stop] = true }
   opts.on("-i", "--install","Install the service"){
OPTIONS[:install] = true }
      
   opts.on("-p", "--port",   "Port that Webrick shall
use"){ |port|
      OPTIONS[:port] = port
   }
   
   opts.on("-m", "--home",   "Home directory for
app"){ |home|
      OPTIONS[:home] = home
   }
   
   opts.on("-r", "--docroot", "Document root
directory"){ |root|
      OPTIONS[:docroot] = root
   }
   
   opts.on("-t", "--path",   "Path to Ruby
executable"){ |path|
      OPTIONS[:path] = path + ' '
   }
   
   opts.on("-h", "--help",   "Show this help
message"){ puts opts; exit }
   
   opts.parse!
end

unless OPTIONS[:start] || OPTIONS[:stop] ||
OPTIONS[:install] || OPTIONS[:delete]
   STDERR.puts "Must specify start, stop, delete or
install"
   exit!
end

path = OPTIONS[:path]      || 'C:\ruby\bin\ruby '
port = OPTIONS[:port]      || 8080
root = OPTIONS[:docroot]   || 'C:\www\webroot'
home = OPTIONS[:home]      ||
'C:\Progra~1\RubyServices\Webrick'

prog = home + '\webrick_daemon.rb '
prog += "-p #{port} -r #{root} -m #{home}"
full_path = path + prog

unless File.exists?(home + '\webrick_daemon.rb')
   STDERR.puts "Error: webrick_daemon.rb not found in
#{home}.  Exiting..."
   exit!
end

service_name = 'WebrickSvc'
display_name = 'Webrick'

# Install the service
if OPTIONS[:install]  
   svc = Service.new
   svc.create_service{ |s|
      s.service_name     = service_name
      s.display_name     = display_name
      s.binary_path_name = 'c:\ruby\bin\ruby
c:\progra~1\rubyservices\webrick\webrick_daemon.rb'
   }
   svc.close
   puts "#{display_name} service installed"
end

# Start the service
if OPTIONS[:start]
   started = false
   if Service.status(service_name).current_state ==
"running"
      puts "#{service_name} is already running"
   else
      Service.start(service_name)
      while started == false
         s = Service.status(service_name)
         started = true if s.current_state ==
"running"
         break if started == true
         puts "One moment, " + s.current_state
         sleep 1
      end
      puts "#{display_name} service started"
   end
end

# Stop the service
if OPTIONS[:stop]
   stopped = false
   if Service.status(service_name).current_state ==
"stopped"
      puts "#{service_name} already stopped"
   else
      Service.stop(service_name)
      while stopped == false
         s = Service.status(service_name)
         stopped = true if s.current_state ==
"stopped"
         break if stopped == true
         puts "One moment, " + s.current_state
         sleep 1
      end
      puts "#{display_name} service stopped"
   end
end

# Delete the service.  Stop it first.
if OPTIONS[:delete]
   Service.stop(service_name)
   stopped = false
   while stopped == false
      s = Service.status(service_name)
      stopped = true if s.current_state == "stopped"
      break if stopped == true
      puts "One moment, status is still " +
Service.status(service_name).current_state
      sleep 1
   end
   Service.delete(service_name)
   puts "#{display_name} service deleted"
end

# END webrickctl.rb

# webrick_daemon.rb
require "win32/service"
include Win32

class WebrickDaemon < Daemon
   def initialize
      @log = "C:\\log.txt"
   end
   
   def service_start
      File.open(@log,"a+"){ |fh| fh.puts "Service
started" }
   end
   
   def service_main
      while state == RUNNING
         File.open(@log, "a+"){ |fh| fh.puts "Service
running" }
         sleep 5
      end
   end
   
   def service_stop
      File.open(@log, "a+"){ |fh| fh.puts "Service
stopped" }
      exit
   end
end

web_daemon = WebrickDaemon.new
web_daemon.mainloop

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the win32utils-devel mailing list