[Win32utils-devel] win32-daemon 0.6.1 problem

Berger, Daniel Daniel.Berger at qwest.com
Fri Oct 31 12:12:19 EDT 2008


> -----Original Message-----
> From: win32utils-devel-bounces at rubyforge.org 
> [mailto:win32utils-devel-bounces at rubyforge.org] On Behalf Of 
> Heesob Park
> Sent: Friday, October 31, 2008 4:52 AM
> To: Development and ideas for win32utils projects
> Subject: Re: [Win32utils-devel] win32-daemon 0.6.1 problem
> 
> 2008/10/31 Berger, Daniel <Daniel.Berger at qwest.com>:
> > Hi,
> >
> > Ruby 1.8.6-p114 built with VC++ 8
> > win32-service 0.6.1 (also built with VC++ 8) Windows XP Pro SP 2
> >
> > I'm having trouble getting even this simple daemon to work. I can't 
> > even get it to log an error to the file. Any ideas?
> >
> > # demon.rb
> > begin
> >   LOG_FILE   = "C:\\demon_test.log"
> >   LOG_HANDLE = File.open(LOG_FILE, 'a')
> >
> >   STDIN.close
> >   STDERR.reopen(LOG_HANDLE)
> >   STDOUT.reopen(LOG_HANDLE)
> >
> >   require 'win32/daemon'
> >   include Win32
> >
> >   class Daemon
> >      def service_main
> >         while running?
> >            sleep 3
> >            puts "service is running"
> >         end
> >      end
> >
> >      def service_stop
> >         exit!
> >      end
> >   end
> >
> >   Daemon.mainloop
> > rescue Exception => err
> >   puts "ERROR: #{err}"
> >   puts
> >   puts "Our Daemon blowed up: " + err.backtrace.join("\n")
> >   raise
> > ensure
> >   LOG_HANDLE.close if LOG_HANDLE && !LOG_HANDLE.closed?
> > end
> >
> > # demo_ctl.rb
> > require 'win32/service'
> > require 'rbconfig'
> > include Config
> > include Win32
> >
> > case ARGV.first.chomp
> >   when 'install'
> >      # Some path handling to deal with spaces and slashes
> >      path = File.join(CONFIG['bindir'], 'ruby.exe') + ' '
> >      path += '"' + File.expand_path('demon.rb') + '"'
> >      path = path.tr("/", "\\")
> >
> >      Service.create('DemoService', nil,
> >         :display_name       => 'DemoService',
> >         :service_type       =>  Service::WIN32_OWN_PROCESS,
> >         :description        => 'Demo Service',
> >         :binary_path_name   => path,
> >         :service_start_name => 'my_domain\my_userid',
> >         :password           => 'my_password'
> >      )
> >      puts "Service created"
> >   when 'stop'
> >      Service.stop('DemoService')
> >      puts "Service stopped"
> >   when 'start'
> >      Service.start('DemoService')
> >      puts "Service started"
> >   when 'uninstall'
> >      Service.delete('DemoService')
> >      puts "Service uninstalled"
> >   else
> >      STDERR.puts "Valid options are 'install', 'uninstall', 
> 'stop' and 
> > 'start'"
> > end
> >
> > In other news, it doesn't seem to like the default 
> service_type. If I 
> > don't explicitly pass it an option the CreateService() call at line 
> > 332 will die. Maybe I should remove "interactive" as part 
> of the default.
> >
> > Regards,
> >
> > Dan
> >
> In my test, I found two problems in the above demon.rb .
> First, your environment may has RUBYOPT and raised following error.
> ruby: no such file to load -- ubygems (LoadError)

I tried unsetting RUBYOPT on the command line and setting it to nil from
within the daemon itself, but it still didn't work. I also don't get any
output to the log file.
 
> I have workarounded by modifing "RUBYOPT" to "RUBYOPTX" in 
> ruby.c #755.

Eep! That seems a bit extreme. I wouldn't think we would need to.
 
> Second, STDIN.close in demon.rb raise error.
> In my thought, STDIN.close is just a meaningless code.

Yeah, it was just me experimenting mostly. Removing it didn't help,
though.

On a related note, I've been thinking that perhaps Daemon.new should
take an optional log file. If present, have STDERR and STDOUT reopen on
that file instead of NUL. It would be the users job to properly close
the handle in the service_stop method, although perhaps we could setup a
finalizer.

Does that sound like a good idea?

Anyway, still no luck with running the Daemon itself. Now that we have
callbacks working better in win32-api, I'm hoping to revisit the pure
Ruby Win32::Daemon class again. :)

Regards,

Dan


This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly 
prohibited and may be unlawful.  If you have received this communication 
in error, please immediately notify the sender by reply e-mail and destroy 
all copies of the communication and any attachments.


More information about the win32utils-devel mailing list