Daniel Berger
2005-May-14 00:53 UTC
[Win32utils-devel] Problems with custom service and webrick
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
Park Heesob
2005-May-14 02:29 UTC
[Win32utils-devel] Problems with custom service and webrick
Hi Dan, ----- Original Message ----- From: "Daniel Berger" <djberg96@yahoo.com> To: <win32utils-devel@rubyforge.org> Sent: Saturday, May 14, 2005 2:00 PM Subject: [Win32utils-devel] Problems with custom service and webrick> 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? >First, "service_start" is not defined as event handler from the first. :) If you want service_start, call it in service_main like this: def service_main service_start while state == RUNNING File.open(@log, "a+"){ |fh| fh.puts "Service running" } sleep 5 end end Second, try load webrick befre loading win32/service like this: require "webrick.rb" require "win32/service" Regards, Park Heesob
Daniel Berger
2005-May-14 09:05 UTC
[Win32utils-devel] Problems with custom service and webrick
On 5/14/05, Park Heesob <phasis@nownuri.net> wrote:> Hi Dan,> First, "service_start" is not defined as event handler from the first. :)Gah! I was too tired while working on this! Sorry.> Second, try load webrick befre loading win32/service like this: > require "webrick.rb" > require "win32/service"Hm...why should that matter? Dan
Park Heesob
2005-May-14 09:56 UTC
[Win32utils-devel] Problems with custom service and webrick
Hi, ----- Original Message ----- From: "Daniel Berger" <djberg96@gmail.com> To: "Development and ideas for win32utils projects" <win32utils-devel@rubyforge.org> Sent: Saturday, May 14, 2005 10:11 PM Subject: Re: [Win32utils-devel] Problems with custom service and webrick> On 5/14/05, Park Heesob <phasis@nownuri.net> wrote: >> Hi Dan, > >> First, "service_start" is not defined as event handler from the first. :) > > Gah! I was too tired while working on this! Sorry. > >> Second, try load webrick befre loading win32/service like this: >> require "webrick.rb" >> require "win32/service" > > Hm...why should that matter? >Don''t ask me why :) Just trial and error. I guess it is related with Hash initialization. Regards, Park Heesob
Daniel Berger
2005-May-16 11:19 UTC
[Win32utils-devel] Problems with custom service and webrick
On 5/15/05, Park Heesob <phasis@nownuri.net> wrote:> Hi, > > > > If you think you could hunt down the cause of that and fix it, that > > would be great. :) > > > I found something :) > > The error is due to line 1576 of service.c: > EventHookHash = rb_hash_new(); > > I guess the EventHookHash is freed during loading ''webrick'' or other libraries. > > So, the EventHookHash initialization must be located at Daemon initialization(daemon_allocate function) like this: > static VALUE daemon_allocate(VALUE klass){ > EventHookHash = rb_hash_new(); > return Data_Wrap_Struct(klass, 0, 0, 0); > } > > In short words, move line 1576 to line 165 of service.c. > > Regards, > > Park HeesobThank you very much! Dan PS - I meant to CC the devel list before and forgot.