2008-07-08

Service is now reliable

(2008-07-17) The service is now running well.  The code crashes or timesout every few days but the error handling below handles the exception and it just keeps on going. My Windows service is still not reliable.  After a few days it seems to just stop in a normal fashion.  I suppose the only thing to do is to keep monitoring.  It currently looks like this (today I have added an error message to exception handling and moved the sleep to be inside the exception handler):

#
# This is the file to run as a daemon.

LOG_FILE = 'C:\\daemon_error.log'

begin
  require 'win32/daemon'
  require 'process_files'

  include Win32

  class DemoDaemon < Daemon
    def service_main
      File.open(LOG_FILE, "a"){ |f| f.puts "Service 2008-07-07 is running #{Time.now}" }
      s = ProcessFiles.new()
      while running?
        begin
         s.getAndProcessOnce
         sleep 10

        rescue Exception => ex
           File.open(LOG_FILE, "a"){ |f| f.puts "Service had an exception #{Time.now} #{ex.class}:#{ex.message}" } 
        end
        #File.open(LOG_FILE, "a"){ |f| f.puts "Service is running #{Time.now}" }
      end
    end

    def service_stop
      File.open(LOG_FILE, "a"){ |f| f.puts "***Service responded to stop and stopped #{Time.now}" }
      exit!
    end
  end

  DemoDaemon.mainloop

rescue Exception => err
  File.open(LOG_FILE,'a+'){ |f| f.puts "***Service Failed #{Time.now} due to #{err.class}:#{err.message}" }
end

I will just have to wait and see if this works more reliably. I have waited and it is looking good! I have some plans to put in a drb client and then allow me to have a local ruby shoes interface to see how it is working.

2 comments:

Daniel Berger said...

Definitely let us know how it goes on the Win32Utils forum. :)

RubyPane said...

Hi Daniel - will do. The code is now working reliably and I will update the comments.